python使用QQ郵箱實(shí)現(xiàn)自動(dòng)發(fā)送郵件
最近用到Python自動(dòng)發(fā)送郵件,主要就是三步,登錄郵件、寫郵件內(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 = ’[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 #開(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 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’) #打開(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 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’) #打開(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 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’) #打開(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.yearmonth = now_time.monthday = now_time.daymytime = str(year) + ' 年 ' + str(month) + ' 月 ' + str(day) + ' 日 'fayanren='愛(ài)因斯坦'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 #開(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í)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Struts2獲取參數(shù)的三種方法總結(jié)2. JSP中Servlet的Request與Response的用法與區(qū)別3. IntelliJ IDEA刪除類的方法步驟4. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼5. Android 實(shí)現(xiàn)徹底退出自己APP 并殺掉所有相關(guān)的進(jìn)程6. vue cli4下環(huán)境變量和模式示例詳解7. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式8. Django視圖類型總結(jié)9. IntelliJ IDEA導(dǎo)入jar包的方法10. Xml簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
