Python發送SMTP郵件指南

 
SMTP(Simple Mail Transfer Protocol)簡單郵件傳輸協議,Python內置對SMTP的支持,可以發送純文本文件,HTML郵件以及附帶文件。
 

一、兩個模塊

Python使用SMTP發送郵件的兩個模塊:smtplib模塊email模塊
smtplib:負責發送郵件
email:負責構建郵件
 

二、SMTP端口

1)未加密端口,smtplib.SMTP接口,端口:25
2)使用SSL加密,smtplib.SMTP_SSL接口,端口:465
3)使用TLS加密,端口:587
 

三、四大步驟

1、構造郵件內容
 
# 純文本
msg = MIMEText(content)
 
# 附件
msg = MIMEMultipart()

 

2、連接郵件服務器
 
s = smtplib.SMTP("smtp.qq.com", 25)

 

3、登陸郵件服務器
 
s.login(msg_from, passwd) 

msg_from:指發送者的郵箱

passwd:指發送者的密碼,這個密碼不是你的QQ登陸密碼,而是你在QQ郵箱設置開啟SMTP之後的一個授權碼
 

 

4、發送郵件
 
s.sendmail(msg_from, msg_to, msg.as_string())
msg_from:發送方
msg_to:收件方
msg.as_string():要發送的消息
 

四、常用場景

1、純文本郵件
 
import smtplib
from email.mime.text import MIMEText
from email.header import Header
 
 
# 發送者
msg_from = "[email protected]"
 
# 這裡的密碼不是QQ郵箱的密碼,而是在設置里開啟SMTP服務器後的授權碼
passwd = "xxxxx"
 
# 接受者
msg_to = "[email protected]"
 
# 郵件文本
content = 'Python 郵件發送測試...'
 
# 郵件主題
subject = "test"
 
# 生成一個MIMEText對象(還有一些其它參數)
msg = MIMEText(content)
 
# 放入郵件主題
msg['Subject'] = Header(subject, 'utf-8')
 
# 放入發件人
msg['From'] = msg_from
 
try:
    # 連接郵件服務器
    s = smtplib.SMTP("smtp.qq.com", 25)
 
    # 登錄到郵箱
    s.login(msg_from, passwd)
 
    # 發送郵件:發送方,收件方,要發送的消息
    s.sendmail(msg_from, msg_to, msg.as_string())
    print('成功')
except s.SMTPException as e:
    print(e)
finally:
    s.quit()

 

2、發送html文本
 
import smtplib
from email.mime.text import MIMEText
from email.header import Header
 
 
# 發送者
msg_from = "[email protected]"
 
# 這裡的密碼不是QQ郵箱的密碼,而是在設置里開啟SMTP服務器後的授權碼
passwd = "xxxx"
 
# 接受者
msg_to = "[email protected]"
 
# 郵件文本
content = """
<p>Python 郵件發送測試...</p>
<p><a href="//www.baidu.com">這是一個鏈接</a></p>
"""
 
# 郵件主題
subject = "test"
 
# 生成一個MIMEText對象(
msg = MIMEText(content, 'html', 'utf-8')
 
# 放入郵件主題
msg['Subject'] = Header(subject, 'utf-8')
 
# 放入發件人
msg['From'] = msg_from
 
try:
    # 連接郵件服務器
    s = smtplib.SMTP("smtp.qq.com", 25)
 
    # 登錄到郵箱
    s.login(msg_from, passwd)
 
    # 發送郵件:發送方,收件方,要發送的消息
    s.sendmail(msg_from, msg_to, msg.as_string())
    print('成功')
except s.SMTPException as e:
    print(e)
finally:
    s.quit()

 

3、發送附件
 
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
 
 
# 發送者
msg_from = "[email protected]"
 
# 這裡的密碼不是QQ郵箱的密碼,而是在設置里開啟SMTP服務器後的授權碼
passwd = "xxxx"
 
# 接受者
msg_to = "[email protected]"
 
# 郵件主題
subject = "test"
 
# 生成一個MIMEMultipart對象(
msg = message = MIMEMultipart()
 
# 郵件文本
message.attach(MIMEText('這是菜鳥教程Python 郵件發送測試……', 'plain', 'utf-8'))
 
# 放入郵件主題
msg['Subject'] = Header(subject, 'utf-8')
 
# 放入發件人
msg['From'] = msg_from
 
# 添加附件
att1 = MIMEText(open('./wordcloud_singer.py', 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment; filename="test.txt"'
msg.attach(att1)
 
try:
    # 連接郵件服務器
    s = smtplib.SMTP("smtp.qq.com", 25)
 
    # 登錄到郵箱
    s.login(msg_from, passwd)
 
    # 發送郵件:發送方,收件方,要發送的消息
    s.sendmail(msg_from, msg_to, msg.as_string())
    print('成功')
except s.SMTPException as e:
    print(e)
finally:
    s.quit()

 

五、寫在最後

李先生(Lemon),高級運維工程師(自稱),SRE專家(目標),夢想在35歲買一輛保時捷。喜歡鑽研底層技術,認為底層基礎才是王道。一切新技術都離不開操作系統(CPU、內存、磁盤)、網絡等。堅持輸入輸出,記錄自己學習的點滴,在平凡中堅持前行,總有一天會遇見不一樣的自己。公眾號:運維汪(ID:Leeeee_Li)。

 

Tags: