python使用QQ郵箱實(shí)現(xiàn)自動(dòng)發(fā)送郵件
最近用到Python自動(dòng)發(fā)送郵件,主要就是三步,登錄郵件、寫(xiě)郵件內(nèi)容、發(fā)送,用到的庫(kù)是 smtplib 和 email,直接使用pip安裝即可
我使用的是QQ郵箱,首先需要設(shè)置QQ郵箱POP3/SMTP服務(wù)
記住這個(gè)授權(quán)碼,這個(gè)授權(quán)碼就是Python腳本中登錄郵箱時(shí)的密碼,而不是你平時(shí)登錄郵箱時(shí)的那個(gè)密碼
一.發(fā)送普通文本郵件
#發(fā)送多種類型的郵件 from email.mime.multipart import MIMEMultipart msg_from = '1508691067@qq.com' # 發(fā)送方郵箱 passwd = 'xxx' #就是上面的授權(quán)碼 to= ['1508691067@qq.com'] #接受方郵箱 #設(shè)置郵件內(nèi)容 #MIMEMultipart類可以放任何內(nèi)容 msg = MIMEMultipart() conntent="這個(gè)是字符串" #把內(nèi)容加進(jìn)去 msg.attach(MIMEText(conntent,'plain','utf-8')) #設(shè)置郵件主題 msg['Subject']="這個(gè)是郵件主題" #發(fā)送方信息 msg['From']=msg_from #開(kāi)始發(fā)送 #通過(guò)SSL方式發(fā)送,服務(wù)器地址和端口 s = smtplib.SMTP_SSL("smtp.qq.com", 465) # 登錄郵箱 s.login(msg_from, passwd) #開(kāi)始發(fā)送 s.sendmail(msg_from,to,msg.as_string()) print("郵件發(fā)送成功")
二.發(fā)送攜帶附件的郵件
import smtplib from email.mime.text import MIMEText #發(fā)送多種類型的郵件 from email.mime.multipart import MIMEMultipart msg_from = '1508691067@qq.com' # 發(fā)送方郵箱 passwd = 'xxxxx' to= ['1508691067@qq.com'] #接受方郵箱 #設(shè)置郵件內(nèi)容 #MIMEMultipart類可以放任何內(nèi)容 msg = MIMEMultipart() conntent="這個(gè)是字符串" #把內(nèi)容加進(jìn)去 msg.attach(MIMEText(conntent,'plain','utf-8')) #添加附件 att1=MIMEText(open('result.xlsx','rb').read(),'base64','utf-8') #打開(kāi)附件 att1['Content-Type']='application/octet-stream' #設(shè)置類型是流媒體格式 att1['Content-Disposition']='attachment;filename=result.xlsx' #設(shè)置描述信息 msg.attach(att1) #加入到郵件中 #設(shè)置郵件主題 msg['Subject']="這個(gè)是郵件主題" #發(fā)送方信息 msg['From']=msg_from #開(kāi)始發(fā)送 #通過(guò)SSL方式發(fā)送,服務(wù)器地址和端口 s = smtplib.SMTP_SSL("smtp.qq.com", 465) # 登錄郵箱 s.login(msg_from, passwd) #開(kāi)始發(fā)送 s.sendmail(msg_from,to,msg.as_string()) print("郵件發(fā)送成功")
三.發(fā)送攜帶圖片的附件
同理,可以使用上面的方法也可以發(fā)送圖片附件
import smtplib from email.mime.text import MIMEText #發(fā)送多種類型的郵件 from email.mime.multipart import MIMEMultipart msg_from = '1508691067@qq.com' # 發(fā)送方郵箱 passwd = 'xxxxx' to= ['1508691067@qq.com'] #接受方郵箱 #設(shè)置郵件內(nèi)容 #MIMEMultipart類可以放任何內(nèi)容 msg = MIMEMultipart() conntent="這個(gè)是字符串" #把內(nèi)容加進(jìn)去 msg.attach(MIMEText(conntent,'plain','utf-8')) #添加附件 att1=MIMEText(open('result.xlsx','rb').read(),'base64','utf-8') #打開(kāi)附件 att1['Content-Type']='application/octet-stream' #設(shè)置類型是流媒體格式 att1['Content-Disposition']='attachment;filename=result.xlsx' #設(shè)置描述信息 att2=MIMEText(open('1.jpg','rb').read(),'base64','utf-8') att2['Content-Type']='application/octet-stream' #設(shè)置類型是流媒體格式 att2['Content-Disposition']='attachment;filename=1.jpg' #設(shè)置描述信息 msg.attach(att1) #加入到郵件中 msg.attach(att2) #設(shè)置郵件主題 msg['Subject']="這個(gè)是郵件主題" #發(fā)送方信息 msg['From']=msg_from #開(kāi)始發(fā)送 #通過(guò)SSL方式發(fā)送,服務(wù)器地址和端口 s = smtplib.SMTP_SSL("smtp.qq.com", 465) # 登錄郵箱 s.login(msg_from, passwd) #開(kāi)始發(fā)送 s.sendmail(msg_from,to,msg.as_string()) print("郵件發(fā)送成功")
四.發(fā)送 html 格式的郵件
import smtplib from email.mime.text import MIMEText #發(fā)送多種類型的郵件 from email.mime.multipart import MIMEMultipart import datetime msg_from = '1508691067@qq.com' # 發(fā)送方郵箱 passwd = 'xxxxxx' to= ['1508691067@qq.com'] #接受方郵箱 #設(shè)置郵件內(nèi)容 #MIMEMultipart類可以放任何內(nèi)容 msg = MIMEMultipart() # conntent="這個(gè)是字符串" # #把內(nèi)容加進(jìn)去 # msg.attach(MIMEText(conntent,'plain','utf-8')) #添加附件 att1=MIMEText(open('result.xlsx','rb').read(),'base64','utf-8') #打開(kāi)附件 att1['Content-Type']='application/octet-stream' #設(shè)置類型是流媒體格式 att1['Content-Disposition']='attachment;filename=result.xlsx' #設(shè)置描述信息 att2=MIMEText(open('1.jpg','rb').read(),'base64','utf-8') att2['Content-Type']='application/octet-stream' #設(shè)置類型是流媒體格式 att2['Content-Disposition']='attachment;filename=1.jpg' #設(shè)置描述信息 msg.attach(att1) #加入到郵件中 msg.attach(att2) now_time = datetime.datetime.now() year = now_time.year month = now_time.month day = now_time.day mytime = str(year) + " 年 " + str(month) + " 月 " + str(day) + " 日 " fayanren="愛(ài)因斯坦" zhuchiren="牛頓" #構(gòu)造HTML content = ''' <html> <body> <h1 align="center">這個(gè)是標(biāo)題,xxxx通知</h1> <p><strong>您好:</strong></p> <blockquote><p><strong>以下內(nèi)容是本次會(huì)議的紀(jì)要,請(qǐng)查收!</strong></p></blockquote> <blockquote><p><strong>發(fā)言人:{fayanren}</strong></p></blockquote> <blockquote><p><strong>主持人:{zhuchiren}</strong></p></blockquote> <p align="right">{mytime}</p> <body> <html> '''.format(fayanren=fayanren, zhuchiren=zhuchiren, mytime=mytime) msg.attach(MIMEText(content,'html','utf-8')) #設(shè)置郵件主題 msg['Subject']="這個(gè)是郵件主題" #發(fā)送方信息 msg['From']=msg_from #開(kāi)始發(fā)送 #通過(guò)SSL方式發(fā)送,服務(wù)器地址和端口 s = smtplib.SMTP_SSL("smtp.qq.com", 465) # 登錄郵箱 s.login(msg_from, passwd) #開(kāi)始發(fā)送 s.sendmail(msg_from,to,msg.as_string()) print("郵件發(fā)送成功")
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何用Python和JS實(shí)現(xiàn)的Web SSH工具
這篇文章主要介紹了如何用Python和JS實(shí)現(xiàn)的Web SSH工具,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02Python中的XML庫(kù)4Suite Server的介紹
這篇文章主要介紹了Python中的XML庫(kù)4Suite Server,來(lái)自于IBM官方網(wǎng)站,需要的朋友可以參考下2015-04-04Python爬蟲(chóng)之必備chardet庫(kù)
這篇文章主要介紹了Python爬蟲(chóng)之必備chardet庫(kù),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04Python實(shí)現(xiàn)簡(jiǎn)單2048小游戲
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)簡(jiǎn)單2048小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05Python實(shí)現(xiàn)批量識(shí)別銀行卡號(hào)碼以及自動(dòng)寫(xiě)入Excel表格步驟詳解
這篇文章主要介紹了使用Python實(shí)現(xiàn)高效摸魚(yú),批量識(shí)別銀行卡號(hào)碼并且自動(dòng)寫(xiě)入Excel表格,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01Python實(shí)現(xiàn)屏幕代碼雨效果的示例代碼
這篇文章主要介紹了如何利用Python中的Pygame模塊實(shí)現(xiàn)代碼雨效果,文中通過(guò)示例代碼介紹的非常詳細(xì),感興趣的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03