python使用QQ郵箱實(shí)現(xiàn)自動(dòng)發(fā)送郵件
最近用到Python自動(dòng)發(fā)送郵件,主要就是三步,登錄郵件、寫郵件內(nèi)容、發(fā)送,用到的庫是 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 = ’[email protected]’ # 發(fā)送方郵箱passwd = ’xxx’ #就是上面的授權(quán)碼 to= [’[email protected]’] #接受方郵箱 #設(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 #開始發(fā)送 #通過SSL方式發(fā)送,服務(wù)器地址和端口s = smtplib.SMTP_SSL('smtp.qq.com', 465)# 登錄郵箱s.login(msg_from, passwd)#開始發(fā)送s.sendmail(msg_from,to,msg.as_string())print('郵件發(fā)送成功')
二.發(fā)送攜帶附件的郵件
import smtplibfrom email.mime.text import MIMEText#發(fā)送多種類型的郵件from email.mime.multipart import MIMEMultipart msg_from = ’[email protected]’ # 發(fā)送方郵箱passwd = ’xxxxx’ to= [’[email protected]’] #接受方郵箱 #設(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’) #打開附件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 #開始發(fā)送 #通過SSL方式發(fā)送,服務(wù)器地址和端口s = smtplib.SMTP_SSL('smtp.qq.com', 465)# 登錄郵箱s.login(msg_from, passwd)#開始發(fā)送s.sendmail(msg_from,to,msg.as_string())print('郵件發(fā)送成功')
三.發(fā)送攜帶圖片的附件
同理,可以使用上面的方法也可以發(fā)送圖片附件
import smtplibfrom email.mime.text import MIMEText#發(fā)送多種類型的郵件from email.mime.multipart import MIMEMultipart msg_from = ’[email protected]’ # 發(fā)送方郵箱passwd = ’xxxxx’ to= [’[email protected]’] #接受方郵箱 #設(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’) #打開附件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 #開始發(fā)送 #通過SSL方式發(fā)送,服務(wù)器地址和端口s = smtplib.SMTP_SSL('smtp.qq.com', 465)# 登錄郵箱s.login(msg_from, passwd)#開始發(fā)送s.sendmail(msg_from,to,msg.as_string())print('郵件發(fā)送成功')
四.發(fā)送 html 格式的郵件
import smtplibfrom email.mime.text import MIMEText#發(fā)送多種類型的郵件from email.mime.multipart import MIMEMultipartimport datetimemsg_from = ’[email protected]’ # 發(fā)送方郵箱passwd = ’xxxxxx’ to= [’[email protected]’] #接受方郵箱 #設(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’) #打開附件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.yearmonth = now_time.monthday = now_time.daymytime = str(year) + ' 年 ' + str(month) + ' 月 ' + str(day) + ' 日 'fayanren='愛因斯坦'zhuchiren='牛頓'#構(gòu)造HTMLcontent = ’’’<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 #開始發(fā)送 #通過SSL方式發(fā)送,服務(wù)器地址和端口s = smtplib.SMTP_SSL('smtp.qq.com', 465)# 登錄郵箱s.login(msg_from, passwd)#開始發(fā)送s.sendmail(msg_from,to,msg.as_string())print('郵件發(fā)送成功')
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python調(diào)用接口合并Excel表代碼實(shí)例2. .net如何優(yōu)雅的使用EFCore實(shí)例詳解3. Python快速將ppt制作成配音視頻課件的操作方法4. ASP.Net Core對(duì)USB攝像頭進(jìn)行截圖5. 一文透徹詳解.NET框架類型系統(tǒng)設(shè)計(jì)要點(diǎn)6. 通過CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫特效7. ASP.NET MVC使用Boostrap實(shí)現(xiàn)產(chǎn)品展示、查詢、排序、分頁8. 通過Ajax方式綁定select選項(xiàng)數(shù)據(jù)的實(shí)例9. ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車10. ajax動(dòng)態(tài)加載json數(shù)據(jù)并詳細(xì)解析
