Python3 發(fā)送郵件
使用第三方庫 yagmail
更新: 第三種方式的隱藏用戶名和密碼的方式红且,目前不再支持
簡單介紹
目標(biāo)是盡可能簡單邑茄,無痛地發(fā)送電子郵件漩怎。
最終的代碼如下:
import yagmail
yag = yagmail.SMTP()
contents = ['This is the body, and here is just text http://somedomain/image.png',
'You can find an audio file attached.', '/local/path/song.mp3']
yag.send('to@someone.com', '郵件標(biāo)題', contents)
或者在一行中實現(xiàn):
yagmail.SMTP('mygmailusername').send('to@someone.com', 'subject', 'This is the body')
當(dāng)然, 以上操作需要從你自己系統(tǒng)的密鑰環(huán)中讀取你的郵箱賬戶和對應(yīng)的密碼糊秆。關(guān)于密鑰環(huán)稍后會提到如何實現(xiàn)鹏往。
安裝模塊
pip3 install yagmail # linux / Mac
pip install yagmail # windows
這樣會安裝最新的版本,并且會支持所有最新功能覆旱,主要是支持從密鑰環(huán)中獲取到郵箱的賬戶和密碼蘸朋。
關(guān)于賬戶和密碼
開通自己郵箱的 SMTP
功能,并獲取到授權(quán)碼
這個賬戶是你要使用此郵箱發(fā)送郵件的賬戶扣唱,密碼不是平時登錄郵箱的密碼藕坯,而是開通 POP3/SMTP
功能后設(shè)置的客戶端授權(quán)密碼。
這里以 126
郵箱為例:
方式一:不使用系統(tǒng)的密鑰環(huán)
不使用系統(tǒng)的密鑰環(huán)画舌,可以直接暴露賬戶和密碼在腳本里
import yagmail
yag = yagmail.SMTP(
user='自己的賬號',
password='賬號的授權(quán)碼',
host='smtp.qq.com', # 郵局的 smtp 地址
port='端口號', # 郵局的 smtp 端口
smtp_ssl=False)
yag.send(to='收件箱賬號',
subject='郵件主題',
contents='郵件內(nèi)容')
方式二: 使用系統(tǒng)的密鑰環(huán)管理賬戶和授權(quán)碼
模塊支持從當(dāng)前系統(tǒng)環(huán)境中的密鑰環(huán)中獲取賬戶和密碼堕担,要想實現(xiàn)這個功能,需要依賴模塊 keyring
曲聂。之后把賬戶和密碼注冊到系統(tǒng)的密鑰環(huán)中即可實現(xiàn)霹购。
1. 安裝依賴模塊
pip3 install keyring
# CentOS7.3 還需要安裝下面的模塊
pip3 install keyrings.alt
2. 開始向密鑰環(huán)注冊
import yagmail
yagmail.register('你的賬號', '你的授權(quán)密碼')
注冊賬戶和密碼,只需要執(zhí)行一次即可朋腋。
3. 發(fā)送郵件
import yagmail
yag = yagmail.SMTP('自己的賬號',
host='smtp.qq.com', # 郵局的 smtp 地址
port='端口號', # 郵局的 smtp 端口
smtp_ssl=False # 不使用加密傳輸
)
yag.send(
to='收件箱賬號',
subject='郵件主題',
contents='郵件內(nèi)容')
示例展示
下面是以我的 126 郵箱為例, 使用系統(tǒng)密鑰環(huán)的方式齐疙,向我的 163郵箱發(fā)送了一封郵件。
import yagmail
yag = yagmail.SMTP(user='shark@126.com',
host='smtp.126.com',
port=25,
smtp_ssl=False)
yag.send(to='docker@163.com',
subject='from shark',
contents='test')
這樣就愉快的發(fā)送了一封測試郵件到 docker@163.com
的郵箱旭咽。
當(dāng)然前提是:
- 126 郵箱開通了
SMTP
功能贞奋。 - 把 126 郵箱的賬號和密碼已經(jīng)注冊到自己系統(tǒng)的密鑰環(huán)中。
發(fā)送附件
發(fā)送
發(fā)送附件只需要給 send
方法傳遞 attachments
關(guān)鍵字參數(shù)
比如我在系統(tǒng)的某一個目錄下有一張圖片穷绵,需要發(fā)送給 docker@163.com
import yagmail
yag = yagmail.SMTP(user='shark@126.com',
host='smtp.126.com',
port=25,
smtp_ssl=False)
yag.send(to='docker@163.com',
subject='from shark',
contents='test',
attachments='./松鼠.jpeg')
收到的郵件和附件
使用 ssl
發(fā)送加密郵件
要發(fā)送加密郵件轿塔,只需要把 smtp_ssl
關(guān)鍵字參數(shù)去掉即可,因為默認(rèn)就是采用的加密方式 smtp_ssl=True
仲墨。
不傳遞 stmp_ssl
關(guān)鍵字參數(shù)的同時勾缭,需要設(shè)置端口為郵箱服務(wù)提供商的加密端口,這里還是以 126 郵箱為例目养,端口是 465
俩由。
import yagmail
yag = yagmail.SMTP(user='shark@126.com',
host='smtp.126.com',
port=465)
yag.send(to='docker@163.com',
subject='from sharkyunops',
contents='test',
attachments='./松鼠.jpeg')
發(fā)送 帶 html 標(biāo)記語言的郵件內(nèi)容
在實際的生產(chǎn)環(huán)境中,經(jīng)常會發(fā)送郵件溝通相關(guān)事宜癌蚁,往往會有表格之類的內(nèi)容幻梯,但是又不想以附件的形式發(fā)送,就可以利用 html 標(biāo)記語言的方式組織數(shù)據(jù)努释。
import yagmail
yag = yagmail.SMTP(user='shark@126.com',
host='smtp.126.com',
port=465)
html="""<table border="1">
<thead>
<tr>
<th>姓名</th>
<th>年齡</th>
</tr>
</thead>
<tbody>
<tr>
<td>shak</td>
<td>18</td>
</tr>
<tr>
<td>西瓜甜</td>
<td>28</td>
</tr>
</tbody>
</table>
"""
yag.send(to='docker@163.com',
subject='from sharkyunops',
contents=['test',html])
更多
如果不指定to
參數(shù)碘梢,則發(fā)送給自己
如果to
參數(shù)是一個列表,則將該郵件發(fā)送給列表中的所有用戶
attachments
參數(shù)的值可以是列表伐蒂,表示發(fā)送多個附件