国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

詳解python 發送郵件實例代碼

2020-02-23 04:12:43
字體:
來源:轉載
供稿:網友

python 發送郵件實例

文件形式的郵件

#!/usr/bin/env python3 #coding: utf-8 import smtplib from emailmimetext import MIMEText from emailheader import Header  sender = '***' receiver = '***' subject = 'python email test' smtpserver = 'smtpcom' username = '***' password = '***'  msg = MIMEText('你好','text','utf-8')#中文需參數‘utf-8',單字節字符不需要 msg['Subject'] = Header(subject, 'utf-8')  smtp = smtplibSMTP() smtpconnect('smtpcom') smtplogin(username, password) smtpsendmail(sender, receiver, msgas_string()) smtpquit() 

HTML形式的郵件

#!/usr/bin/env python3 #coding: utf-8 import smtplib from emailmimetext import MIMEText  sender = '***' receiver = '***' subject = 'python email test' smtpserver = 'smtpcom' username = '***' password = '***'  msg = MIMEText('<html><h1>你好</h1></html>','html','utf-8')  msg['Subject'] = subject  smtp = smtplibSMTP() smtpconnect('smtpcom') smtplogin(username, password) smtpsendmail(sender, receiver, msgas_string()) smtpquit() 

帶圖片的HTML郵件

#!/usr/bin/env python3 #coding: utf-8 import smtplib from emailmimemultipart import MIMEMultipart from emailmimetext import MIMEText from emailmimeimage import MIMEImage  sender = '***' receiver = '***' subject = 'python email test' smtpserver = 'smtpcom' username = '***' password = '***'  msgRoot = MIMEMultipart('related') msgRoot['Subject'] = 'test message'  msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image<br><img src="cid:image1"><br>good!','html','utf-8') msgRootattach(msgText)  fp = open('h://python//jpg', 'rb') msgImage = MIMEImage(fpread()) fpclose()  msgImageadd_header('Content-ID', '<image1>') msgRootattach(msgImage)  smtp = smtplibSMTP() smtpconnect('smtpcom') smtplogin(username, password) smtpsendmail(sender, receiver, msgRootas_string()) smtpquit() 

帶附件的郵件

#!/usr/bin/env python3 #coding: utf-8 import smtplib from emailmimemultipart import MIMEMultipart from emailmimetext import MIMEText from emailmimeimage import MIMEImage  sender = '***' receiver = '***' subject = 'python email test' smtpserver = 'smtpcom' username = '***' password = '***'  msgRoot = MIMEMultipart('related') msgRoot['Subject'] = 'test message'  #構造附件 att = MIMEText(open('h://python//jpg', 'rb')read(), 'base64', 'utf-8') att["Content-Type"] = 'application/octet-stream' att["Content-Disposition"] = 'attachment; filename="jpg"' msgRootattach(att)      smtp = smtplibSMTP() smtpconnect('smtpcom') smtplogin(username, password) smtpsendmail(sender, receiver, msgRootas_string()) smtpquit() 

群郵件

#!/usr/bin/env python3 #coding: utf-8 import smtplib from emailmimetext import MIMEText  sender = '***' receiver = ['***','****',……] subject = 'python email test' smtpserver = 'smtpcom' username = '***' password = '***'  msg = MIMEText('你好','text','utf-8')  msg['Subject'] = subject  smtp = smtplibSMTP() smtpconnect('smtpcom') smtplogin(username, password) smtpsendmail(sender, receiver, msgas_string()) smtpquit()             
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临沭县| 兴城市| 瑞安市| 满洲里市| 盘锦市| 吉水县| 绥芬河市| 蒙自县| 泸西县| 惠东县| 乌兰察布市| 乐平市| 大城县| 图们市| 夏河县| 胶州市| 辽阳县| 吉木萨尔县| 巴青县| 平湖市| 绿春县| 凤山市| 天等县| 永靖县| 五寨县| 沭阳县| 宁河县| 勃利县| 霞浦县| 龙海市| 当涂县| 安图县| 江达县| 重庆市| 文安县| 喀喇沁旗| 通城县| 循化| 东宁县| 崇仁县| 洪湖市|