python使用smtplib模塊發(fā)送郵件
使用smtplib模塊發(fā)送郵件,供大家參考,具體內(nèi)容如下
1)使用smtplib模塊發(fā)送簡單郵件
步驟:
1.連接SMTP服務(wù)器,并使用用戶名、密碼登陸服務(wù)器
2.創(chuàng)建EmailMessage對象,該對象代表了郵件本身
3.調(diào)用sendmail()方法發(fā)送郵件
示例:
- 我用自己的QQ郵箱(英文地址)給自己(原始地址)發(fā)一封郵件(QQ郵箱需要授權(quán)碼(詳見))
- smtplib.SMTP() 代表的普通SMTP連接(默認(rèn)端口21)
- smtplib.SMTP_SSL() 代表基于SSL的SMTP連接(默認(rèn)端口456,安全)
import smtplib
import email.message
fromaddr = 'wk_helloworld@qq.com' # 賬號
password = '****************' # QQ授權(quán)碼
conn = smtplib.SMTP_SSL('smtp.qq.com', 465) # 創(chuàng)建SMTP連接
conn.login(fromaddr, password) # 登錄郵件服務(wù)器
msg = email.message.EmailMessage() # 創(chuàng)建郵件對象
msg.set_content('您好,Python郵件') # 設(shè)置郵件內(nèi)容(普通郵件)
conn.sendmail(fromaddr, ['929667257@qq.com'], msg.as_string()) # 發(fā)送郵件
conn.quit() # 退出連接

2)發(fā)送內(nèi)容完整的郵件
- 為郵件設(shè)置標(biāo)題、發(fā)件人名字、收件人名(設(shè)置 EmailMessage 對象對應(yīng)的屬性)
- EmailMessage的set_content() 方法的第二個參數(shù)設(shè)置為 html 可將郵件內(nèi)容改為 HTML 格式
import smtplib
import email.message
fromaddr = 'wk_helloworld@qq.com'
password = '****************'
conn = smtplib.SMTP_SSL('smtp.qq.com', 465)
conn.login(fromaddr, password)
msg = email.message.EmailMessage()
msg.set_content('<h2>HTML郵件<h2>' + '<div style="border:1px:solid red">HTML郵件內(nèi)容</div>', 'html', 'UTF-8')
msg['subject'] = 'HTML郵件'
msg['from'] = '癡迷<%s>' % fromaddr
msg['to'] = '淡然<%s>' % '929667257@qq.com'
conn.sendmail(fromaddr, ['929667257@qq.com'], msg.as_string())
conn.quit()

3)發(fā)送圖文并茂的郵件
在郵件中插入圖片,需要先調(diào)用 EmailMessage 的 add_attachment() 方法來添加附件,該方法參數(shù):
- maintype:指定附件的主要類型
- subtype:指定附件的子類型
- filename:指定該附件的文件名
- cid=img:指定該附件的資源 ID
通過<img…/>元素來插入附件中的圖片(引用附件的cid屬性)
import smtplib
import email.message
import email.utils
fromaddr = 'wk_helloworld@qq.com'
password = '****************'
toaddr = '929667257@qq.com'
conn = smtplib.SMTP_SSL('smtp.qq.com', 465)
conn.login(fromaddr, password)
msg = email.message.EmailMessage()
first_id = email.utils.make_msgid()
msg.set_content('<h2>HTML郵件<h2>'
+ '<div style="border:1px:solid red">html郵件內(nèi)容</div>'
+ '<img src="cid:' + first_id[1:-1] + '">', 'html', 'UTF-8')
msg['subject'] = 'HTML郵件'
msg['from'] = 'wk<%s>' % fromaddr
msg['to'] = 'k<%s>' % toaddr
# 添加附件
with open('圖1.jpg', 'rb') as f:
# 附件指定cid后,郵件正文可通過該cid來引用該圖片
msg.add_attachment(f.read(), maintype='image', subtype='jepg', filename='test1.jpg', cid=first_id)
with open('圖2.jpg', 'rb') as f:
msg.add_attachment(f.read(), maintype='image', subtype='jepg', filename='test2.jpg')
# with open('圖3.gif', 'rb') as f:
# msg.add_attachement(f.read(), maintype='image', subtype='gif', filename='test.jpg')
conn.sendmail(fromaddr, [toaddr], msg.as_string())
conn.quit()

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python標(biāo)準(zhǔn)庫學(xué)習(xí)之operator.itemgetter函數(shù)的使用
operator.itemgetter是Python標(biāo)準(zhǔn)庫operator模塊中的一個函數(shù),本文主要介紹了Python標(biāo)準(zhǔn)庫學(xué)習(xí)之operator.itemgetter函數(shù)的使用,具有一定的參考價值,感興趣的可以了解一下2024-07-07
Python WEB應(yīng)用部署的實現(xiàn)方法
這篇文章主要介紹了Python WEB應(yīng)用部署的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01
Python使用scipy模塊實現(xiàn)一維卷積運算示例
這篇文章主要介紹了Python使用scipy模塊實現(xiàn)一維卷積運算,結(jié)合實例形式分析了scipy模塊的功能及使用scipy模塊進行一維卷積運算的相關(guān)操作技巧,需要的朋友可以參考下2019-09-09
tensorflow 模型權(quán)重導(dǎo)出實例
今天小編就為大家分享一篇tensorflow 模型權(quán)重導(dǎo)出實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01

