国产探花免费观看_亚洲丰满少妇自慰呻吟_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()             
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江陵县| 乌海市| 清涧县| 山东省| 伊金霍洛旗| 泾川县| 永康市| 瑞金市| 墨玉县| 岳西县| 永城市| 达州市| 巴林右旗| 康平县| 即墨市| 建瓯市| 商南县| 民乐县| 托克逊县| 宝应县| 丹阳市| 鹤庆县| 泰宁县| 四子王旗| 莒南县| 勃利县| 博湖县| 永吉县| 大足县| 静海县| 海淀区| 徐汇区| 阜南县| 福清市| 黄骅市| 英超| 鄂托克旗| 自贡市| 体育| 密山市| 满城县|