python郵件中附加文字、html、圖片、附件實(shí)現(xiàn)方法
關(guān)于python寫郵件各種功能我們已經(jīng)介紹過很多,大家有興趣可以參考:
python自動(dòng)化發(fā)送郵件實(shí)例講解
python實(shí)現(xiàn)發(fā)送QQ郵件(可加附件)
下面我們看下本次介紹的全部代碼實(shí)例
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.header import Header #設(shè)置smtplib所需的參數(shù)#下面的發(fā)件人,收件人是用于郵件傳輸?shù)摹mtpserver = ’smtp.163.com’username = ’XXX@163.com’password=’XXX’sender=’XXX@163.com’#receiver=’XXX@126.com’#收件人為多個(gè)收件人receiver=[’XXX@126.com’,’XXX@126.com’] subject = ’Python email test’#通過Header對(duì)象編碼的文本,包含utf-8編碼信息和Base64編碼信息。以下中文名測(cè)試ok#subject = ’中文標(biāo)題’#subject=Header(subject, ’utf-8’).encode() #構(gòu)造郵件對(duì)象MIMEMultipart對(duì)象#下面的主題,發(fā)件人,收件人,日期是顯示在郵件頁面上的。msg = MIMEMultipart(’mixed’) msg[’Subject’] = subjectmsg[’From’] = ’XXX@163.com <XXX@163.com>’#msg[’To’] = ’XXX@126.com’#收件人為多個(gè)收件人,通過join將列表轉(zhuǎn)換為以;為間隔的字符串msg[’To’] = ';'.join(receiver) #msg[’Date’]=’2012-3-16’ #構(gòu)造文字內(nèi)容 text = 'Hi!nHow are you?nHere is the link you wanted:nhttp://www.baidu.com' text_plain = MIMEText(text,’plain’, ’utf-8’) msg.attach(text_plain) #構(gòu)造圖片鏈接sendimagefile=open(r’D:pythontesttestimage.png’,’rb’).read()image = MIMEImage(sendimagefile)image.add_header(’Content-ID’,’<image1>’)image['Content-Disposition'] = ’attachment; filename='testimage.png'’msg.attach(image) #構(gòu)造html#發(fā)送正文中的圖片:由于包含未被許可的信息,網(wǎng)易郵箱定義為垃圾郵件,報(bào)554 DT:SPM :<p><img src='cid:image1'></p>html = '''<html> <head></head> <body> <p>Hi!<br> How are you?<br> Here is the <a rel='external nofollow' >link</a> you wanted.<br> </p> </body> </html> ''' text_html = MIMEText(html,’html’, ’utf-8’)text_html['Content-Disposition'] = ’attachment; filename='texthtml.html'’ msg.attach(text_html) #構(gòu)造附件sendfile=open(r’D:pythontest1111.txt’,’rb’).read()text_att = MIMEText(sendfile, ’base64’, ’utf-8’) text_att['Content-Type'] = ’application/octet-stream’ #以下附件可以重命名成aaa.txt #text_att['Content-Disposition'] = ’attachment; filename='aaa.txt'’#另一種實(shí)現(xiàn)方式text_att.add_header(’Content-Disposition’, ’attachment’, filename=’aaa.txt’)#以下中文測(cè)試不ok#text_att['Content-Disposition'] = u’attachment; filename='中文附件.txt'’.decode(’utf-8’)msg.attach(text_att) #發(fā)送郵件smtp = smtplib.SMTP() smtp.connect(’smtp.163.com’)#我們用set_debuglevel(1)就可以打印出和SMTP服務(wù)器交互的所有信息。#smtp.set_debuglevel(1) smtp.login(username, password) smtp.sendmail(sender, receiver, msg.as_string()) smtp.quit()
小編測(cè)試后發(fā)現(xiàn),這個(gè)實(shí)例可以把很多元素當(dāng)做一個(gè)多文本編輯器,放在郵件附件里,非常好用。
以上就是python郵件中附加文字、html、圖片、附件實(shí)現(xiàn)方法的詳細(xì)內(nèi)容,更多關(guān)于python郵件中添加元素附件方法的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算2. Java實(shí)現(xiàn)UDP通信過程實(shí)例分析【服務(wù)器端與客戶端】3. JS圖片懶加載庫VueLazyLoad詳解4. Java PreparedStatement用法詳解5. Python 解決火狐瀏覽器不彈出下載框直接下載的問題6. Java利用TCP協(xié)議實(shí)現(xiàn)客戶端與服務(wù)器通信(附通信源碼)7. 使用AJAX(包含正則表達(dá)式)驗(yàn)證用戶登錄的步驟8. Java實(shí)現(xiàn)的迷宮游戲9. HTML <!DOCTYPE> 標(biāo)簽10. Spring如何集成ibatis項(xiàng)目并實(shí)現(xiàn)dao層基類封裝

網(wǎng)公網(wǎng)安備