Python基于smtplib模塊發(fā)送郵件代碼實(shí)例
smtplib模塊負(fù)責(zé)發(fā)送郵件:是一個(gè)發(fā)送郵件的動(dòng)作,連接郵箱服務(wù)器,登錄郵箱,發(fā)送郵件(有發(fā)件人,收信人,郵件內(nèi)容)。
email模塊負(fù)責(zé)構(gòu)造郵件:指的是郵箱頁(yè)面顯示的一些構(gòu)造,如發(fā)件人,收件人,主題,正文,附件等。
email模塊下有mime包,mime英文全稱為“Multipurpose Internet Mail Extensions”,即多用途互聯(lián)網(wǎng)郵件擴(kuò)展,是目前互聯(lián)網(wǎng)電子郵件普遍遵循的郵件技術(shù)規(guī)范。
該mime包下常用的有三個(gè)模塊:text,image,multpart。
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.header import Header #郵件服務(wù)器信息 smtp_server = "smtp.qq.com" port = 465 # For starttls sender_email = "12345689@qq.com" password="" #get password from mailsetting #發(fā)送郵件信息,可以發(fā)送給多個(gè)收件人 receivers=["12345689@163.com","12345689@qq.com"] subject="This is import Python SMTP 郵件(文件傳輸) 多媒體測(cè)試" # message = MIMEText(text, "plain", "utf-8") #文本郵件 message = MIMEMultipart() message["Subject"] = Header(subject, "utf-8") message["from"] = sender_email message["to"] = ",".join(receivers) # 郵件正文內(nèi)容 text=""" Dear Sir: how are you ? \n for detail information pls refer to attach1。\n The files you need are as followed.\n If you have any concern pls let me known.\n enjoy your weekend.\n BEST REGARDS \n """ # message.attach(MIMEText('for detail information pls refer to attach1。\n The files you need are as followed. \n If you have any concern pls let me known. \n enjoy your weekend', 'plain', 'utf-8') message.attach(MIMEText(text,'plain','utf-8')) # 構(gòu)造附件1 attach_file1='IMG1965.JPG' attach1 = MIMEText(open(attach_file1, 'rb').read(), 'base64', 'utf-8') attach1["Content-Type"] = 'application/octet-stream' attach1["Content-Disposition"] = 'attachment; filename={0}'.format(attach_file1) message.attach(attach1) # 構(gòu)造附件2 attach_file2='YLJ.jpg' attach2 = MIMEText(open(attach_file2, 'rb').read(), 'base64', 'utf-8') attach2["Content-Type"] = 'application/octet-stream' attach2["Content-Disposition"] = 'attachment; filename={0}'.format(attach_file2) message.attach(attach2) # Try to log in to server and send email # server = smtplib.SMTP_SSL(smtp_server,port) server = smtplib.SMTP_SSL(smtp_server,port) try: server.login(sender_email, password) server.sendmail(sender_email,receivers,message.as_string()) print("郵件發(fā)送成功!!!") print("Mail with {0} & {1} has been send to {2} successfully.".format(attach_file1,attach_file2,receivers)) except Exception as e: # Print any error messages to stdout print("Error: 無(wú)法發(fā)送郵件") print(e) finally: server.quit()
結(jié)果
郵件發(fā)送成功!!!
Mail with IMG1965.JPG & IMG1963.jpg has been send to ['12345689@163.com', '12345689@qq.com'] successfully.
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python數(shù)據(jù)結(jié)構(gòu)與算法之圖結(jié)構(gòu)(Graph)實(shí)例分析
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)與算法之圖結(jié)構(gòu)(Graph),結(jié)合實(shí)例形式分析了圖結(jié)構(gòu)的概念、原理、使用方法及相關(guān)操作技巧,需要的朋友可以參考下2017-09-09利用Python操作MongoDB數(shù)據(jù)庫(kù)的詳細(xì)指南
MongoDB是由C++語(yǔ)言編寫(xiě)的非關(guān)系型數(shù)據(jù)庫(kù),是一個(gè)基于分布式文件存儲(chǔ)的開(kāi)源數(shù)據(jù)庫(kù)系統(tǒng),其內(nèi)容存儲(chǔ)形式類似JSON對(duì)象,下面這篇文章主要給大家介紹了關(guān)于利用Python操作MongoDB數(shù)據(jù)庫(kù)的相關(guān)資料,需要的朋友可以參考下2022-06-06Queue 實(shí)現(xiàn)生產(chǎn)者消費(fèi)者模型(實(shí)例講解)
下面小編就為大家?guī)?lái)一篇Queue 實(shí)現(xiàn)生產(chǎn)者消費(fèi)者模型(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11Python?OpenCV?Canny邊緣檢測(cè)算法的原理實(shí)現(xiàn)詳解
這篇文章主要介紹了Python?OpenCV?Canny邊緣檢測(cè)算法的原理實(shí)現(xiàn)詳解,由于邊緣檢測(cè)對(duì)噪聲敏感,因此對(duì)圖像應(yīng)用高斯平滑以幫助減少噪聲,具體詳情需要的小伙伴可以參考一下2022-07-07Python中連通域分割Two-Pass算法的原理與實(shí)現(xiàn)詳解
兩遍掃描法(?Two-Pass?),正如其名,指的就是通過(guò)掃描兩遍圖像,將圖像中存在的所有連通域找出并標(biāo)記,本文將詳細(xì)介紹Two-Pass算法的原理與實(shí)現(xiàn),需要的可以參考下2023-12-12Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)同時(shí)對(duì)數(shù)據(jù)做轉(zhuǎn)換和換算處理操作示例
這篇文章主要介紹了Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)同時(shí)對(duì)數(shù)據(jù)做轉(zhuǎn)換和換算處理操作,涉及Python使用生成器表達(dá)式進(jìn)行數(shù)據(jù)處理的相關(guān)操作技巧,需要的朋友可以參考下2018-03-03對(duì)Python 除法負(fù)數(shù)取商的取整方式詳解
今天小編就為大家分享一篇對(duì)Python 除法負(fù)數(shù)取商的取整方式詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12