Python自動發(fā)送郵件,使用到python內(nèi)置庫email和 smtplib協(xié)議庫。
"""
(1) MIMEMultipart類型基本格式
● MIMEMultipart(‘mixed’)類型
如果一封郵件中含有附件,那郵件的中必須定義multipart/mixed類型,郵件通過multipart/mixed
類型中定義的boundary標(biāo)識將附件內(nèi)容同郵件其它內(nèi)容分成不同的段?;靖袷饺缦拢?msg=MIMEMultipart(‘mixed’)
● MIMEMultipart(‘a(chǎn)lternative’)類型
MIME郵件可以傳送超文本內(nèi)容,但出于兼容性的考慮,一般在發(fā)送超文本格式內(nèi)容的同時會同時發(fā)送一
個純文本內(nèi)容的副本,如果郵件中同時存在純文本和超文本內(nèi)容,則郵件需要在Content-Type域中定義
multipart/alternative類型,郵件通過其boundary中的分段標(biāo)識將純文本、超文本和郵件的其它內(nèi)
容分成不同的段。基本格式如下:
msg=MIMEMultipart(‘a(chǎn)lternative’)
● MIMEMultipart(‘related’)類型
MIME郵件中除了可以攜帶各種附件外,還可以將其它內(nèi)容以內(nèi)嵌資源的方式存儲在郵件中。比如我們在
發(fā)送html格式的郵件內(nèi)容時,可能使用圖像作為 html的背景,html文本會被存儲在alternative段中,
而作為背景的圖像則會存儲在multipart/related類型定義的段中?;靖袷饺缦拢?msg=MIMEMultipart(‘related’)
"""
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
file_img = r"C:\Users\bingo\Desktop\The crawler\1.jpg"
file_xml = r"C:\Users\bingo\Desktop\The crawler\out.xml"
file_js = r"C:\Users\bingo\Desktop\The crawler\a.js"
qq_email = "1126878602@qq.com"
qq_smtp_no = "klsnipdnjptohade" # 不是郵箱密碼,是IMAP/SMTP或者POP3/SMTP服務(wù)授權(quán)碼,需自己開通
receive_email = "bingyl123@163.com"
# 加密,一般為465端口
# server = smtplib.SMTP_SSL("smtp.qq.com", 465)
# # 不加密,明文傳輸
server = smtplib.SMTP("smtp.qq.com", 25)
# 登陸服務(wù)器
server.login(qq_email, qq_smtp_no)
# 實例一個總的郵件對象,聲明郵件主題,收件人發(fā)件人信息
msg_root = MIMEMultipart("related") # 聲明有見類型
msg_root["From"] = Header("Bingo<{}>".format(qq_email)) # 自定義郵件發(fā)送人
msg_root["To"] = Header(receive_email) # 自定義有見郵件接收人
msg_root["Subject"] = Header("測試郵件Test Email", "utf8") # 自定義郵件主題
# 文本消息
# msg_root.attach(MIMEText("你好,請查收附件"))
# msg_txt = MIMEText("你好,請查收附件", "plain", "utf-8")
# msg_root.attach(msg_txt)
# 文本附件
f_obj = open(file_xml, "rb")
msg_xml = MIMEText(f_obj.read(), "base64", "utf-8")
f_obj.close()
msg_xml["Content-Type"] = 'application/octet-stream'
msg_xml['Content-Disposition']='attachment; filename="out.xml"'
msg_root.attach(msg_xml)
f_obj = open(file_js, "rb")
msg_js = MIMEText(f_obj.read(), "base64", "utf-8")
f_obj.close()
msg_js["Content-Type"] = 'application/octet-stream'
msg_js['Content-Disposition']='attachment; filename="a.js"'
msg_root.attach(msg_js)
# ++++++++++++ 超文本+圖片顯示 +++++++++++++++++++++
msg_alternative = MIMEMultipart("alternative")
mail_msg = """
<p>你好:</p>
<p>  請查收附件!</p>
<p><a >百度一下,你就知道</a></p>
<p>圖片演示:</p>
<p><img src="cid:image1" width=500 height=300></p>
"""
msg_html = MIMEText(mail_msg, "html", "utf-8")
msg_alternative.attach(msg_html)
msg_root.attach(msg_alternative)
f_obj = open(file_img, "rb")
msg_img = MIMEImage(f_obj.read())
f_obj.close()
msg_img.add_header("Content-ID", "image1")
msg_root.attach(msg_img)
# ++++++++++++++++++++++++++++++++++++++++++++++++++
# 圖片附件
f_obj = open(file_img, "rb")
msg_img = MIMEImage(f_obj.read(), "image")
f_obj.close()
msg_img["Content-Type"] = 'application/octet-stream'
msg_img['Content-Disposition']='attachment; filename="111.jpg"'
msg_root.attach(msg_img)
res = server.sendmail(qq_email, [receive_email], msg_root.as_string())
print("郵件發(fā)送成...")
server.quit() # 關(guān)閉服務(wù)
運(yùn)行結(jié)果:
G:\Anaconda\python.exe "C:/Users/bingo/Desktop/The crawler/學(xué)習(xí)/郵件.py" 郵件發(fā)送成... Process finished with exit code 0
接收到的郵件:

示例.png