使用Python發(fā)送各種形式的郵件的方法匯總
我們平時需要使用 Python 發(fā)送各類郵件,這個需求怎么來實現(xiàn)?答案其實很簡單,smtplib 和 email 庫可以幫忙實現(xiàn)這個需求。smtplib 和 email 的組合可以用來發(fā)送各類郵件:普通文本,HTML 形式,帶附件,群發(fā)郵件,帶圖片的郵件等等。我們這里將會分幾節(jié)把發(fā)送郵件功能解釋完成。
smtplib 是 Python 用來發(fā)送郵件的模塊,email 是用來處理郵件消息。
發(fā)送 HTML 形式的郵件
發(fā)送 HTML 形式的郵件,需要 email.mime.text 中的 MIMEText 的 _subtype 設(shè)置為 html,并且 _text 的內(nèi)容應(yīng)該為 HTML 形式。
import smtplib from email.mime.text import MIMEText sender = '***' receiver = '***' subject = 'python email test' smtpserver = 'smtp.163.com' username = '***' password = '***' msg = MIMEText(u'''<pre> <h1>你好</h1> </pre>''','html','utf-8') msg['Subject'] = subject smtp = smtplib.SMTP() smtp.connect(smtpserver) smtp.login(username, password) smtp.sendmail(sender, receiver, msg.as_string()) smtp.quit()
注意:這里的代碼并沒有把異常處理加入,需要讀者自己處理異常。
發(fā)送帶圖片的郵件
發(fā)送帶圖片的郵件是利用 email.mime.multipart 的 MIMEMultipart 以及 email.mime.image 的 MIMEImage:
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage sender = '***' receiver = '***' subject = 'python email test' smtpserver = 'smtp.163.com' username = '***' password = '***' msgRoot = MIMEMultipart('related') msgRoot['Subject'] = 'test message' msgText = MIMEText( '''<b> Some <i> HTML </i> text </b > and an image.<img alt="" src="cid:image1"/>good!''', 'html', 'utf-8') msgRoot.attach(msgText) fp = open('/Users/1.jpg', 'rb') msgImage = MIMEImage(fp.read()) fp.close() msgImage.add_header('Content-ID', '<image1>') msgRoot.attach(msgImage) smtp = smtplib.SMTP() smtp.connect(smtpserver) smtp.login(username, password) smtp.sendmail(sender, receiver, msgRoot.as_string()) smtp.quit()
發(fā)送帶附件的郵件
發(fā)送帶附件的郵件是利用 email.mime.multipart 的 MIMEMultipart 以及 email.mime.image 的 MIMEImage,重點是構(gòu)造郵件頭信息:
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText sender = '***' receiver = '***' subject = 'python email test' smtpserver = 'smtp.163.com' username = '***' password = '***' msgRoot = MIMEMultipart('mixed') msgRoot['Subject'] = 'test message' # 構(gòu)造附件 att = MIMEText(open('/Users/1.jpg', 'rb').read(), 'base64', 'utf-8') att["Content-Type"] = 'application/octet-stream' att["Content-Disposition"] = 'attachment; filename="1.jpg"' msgRoot.attach(att) smtp = smtplib.SMTP() smtp.connect(smtpserver) smtp.login(username, password) smtp.sendmail(sender, receiver, msgRoot.as_string()) smtp.quit()
相關(guān)文章
Python如何將給定字符串中的大寫英文字母按以下對應(yīng)規(guī)則替換
這篇文章主要介紹了Python如何將給定字符串中的大寫英文字母按以下對應(yīng)規(guī)則替換,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10在Python dataframe中出生日期轉(zhuǎn)化為年齡的實現(xiàn)方法
這篇文章主要介紹了在Python dataframe中出生日期轉(zhuǎn)化為年齡的實現(xiàn)方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-10-10Jupyter Notebook/VSCode導(dǎo)出PDF中文不顯示的解決
這篇文章主要介紹了Jupyter Notebook/VSCode導(dǎo)出PDF中文不顯示的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06解決Keras 與 Tensorflow 版本之間的兼容性問題
今天小編就為大家分享一篇解決Keras 與 Tensorflow 版本之間的兼容性問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02python3爬蟲獲取html內(nèi)容及各屬性值的方法
今天小編就為大家分享一篇python3爬蟲獲取html內(nèi)容及各屬性值的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12