1. 程式人生 > 程式設計 >python使用QQ郵箱實現自動傳送郵件

python使用QQ郵箱實現自動傳送郵件

最近用到Python自動傳送郵件,主要就是三步,登入郵件、寫郵件內容、傳送,用到的庫是 smtplib 和 email,直接使用pip安裝即可

我使用的是QQ郵箱,首先需要設定QQ郵箱POP3/SMTP服務

python使用QQ郵箱實現自動傳送郵件

python使用QQ郵箱實現自動傳送郵件

python使用QQ郵箱實現自動傳送郵件

記住這個授權碼,這個授權碼就是Python指令碼中登入郵箱時的密碼,而不是你平時登入郵箱時的那個密碼

一.傳送普通文字郵件

#傳送多種型別的郵件
from email.mime.multipart import MIMEMultipart
 
msg_from = '[email protected]' # 傳送方郵箱
passwd = 'xxx'  #就是上面的授權碼
 
to= ['[email protected]'] #接受方郵箱
 
#設定郵件內容
#MIMEMultipart類可以放任何內容
msg = MIMEMultipart()
conntent="這個是字串"
#把內容加進去
msg.attach(MIMEText(conntent,'plain','utf-8'))
 
#設定郵件主題
msg['Subject']="這個是郵件主題"
 
#傳送方資訊
msg['From']=msg_from
 
#開始傳送
 
#通過SSL方式傳送,伺服器地址和埠
s = smtplib.SMTP_SSL("smtp.qq.com",465)
# 登入郵箱
s.login(msg_from,passwd)
#開始傳送
s.sendmail(msg_from,to,msg.as_string())
print("郵件傳送成功")

python使用QQ郵箱實現自動傳送郵件

二.傳送攜帶附件的郵件

import smtplib
from email.mime.text import MIMEText
#傳送多種型別的郵件
from email.mime.multipart import MIMEMultipart
 
msg_from = '[email protected]' # 傳送方郵箱
passwd = 'xxxxx'
 
to= ['[email protected]'] #接受方郵箱
 
#設定郵件內容
#MIMEMultipart類可以放任何內容
msg = MIMEMultipart()
conntent="這個是字串"
#把內容加進去
msg.attach(MIMEText(conntent,'utf-8'))
 
#新增附件
att1=MIMEText(open('result.xlsx','rb').read(),'base64','utf-8') #開啟附件
att1['Content-Type']='application/octet-stream'  #設定型別是流媒體格式
att1['Content-Disposition']='attachment;filename=result.xlsx' #設定描述資訊
 
msg.attach(att1)  #加入到郵件中
 
#設定郵件主題
msg['Subject']="這個是郵件主題"
 
#傳送方資訊
msg['From']=msg_from
 
#開始傳送
 
#通過SSL方式傳送,伺服器地址和埠
s = smtplib.SMTP_SSL("smtp.qq.com",msg.as_string())
print("郵件傳送成功")

python使用QQ郵箱實現自動傳送郵件

三.傳送攜帶圖片的附件

同理,可以使用上面的方法也可以傳送圖片附件

import smtplib
from email.mime.text import MIMEText
#傳送多種型別的郵件
from email.mime.multipart import MIMEMultipart
 
msg_from = '[email protected]' # 傳送方郵箱
passwd = 'xxxxx'
 
to= ['[email protected]'] #接受方郵箱
 
#設定郵件內容
#MIMEMultipart類可以放任何內容
msg = MIMEMultipart()
conntent="這個是字串"
#把內容加進去
msg.attach(MIMEText(conntent,'utf-8') #開啟附件
att1['Content-Type']='application/octet-stream'  #設定型別是流媒體格式
att1['Content-Disposition']='attachment;filename=result.xlsx' #設定描述資訊
 
att2=MIMEText(open('1.jpg','utf-8')
att2['Content-Type']='application/octet-stream'  #設定型別是流媒體格式
att2['Content-Disposition']='attachment;filename=1.jpg' #設定描述資訊
 
msg.attach(att1)  #加入到郵件中
msg.attach(att2)
 
#設定郵件主題
msg['Subject']="這個是郵件主題"
 
#傳送方資訊
msg['From']=msg_from
 
#開始傳送
 
#通過SSL方式傳送,伺服器地址和埠
s = smtplib.SMTP_SSL("smtp.qq.com",msg.as_string())
print("郵件傳送成功")

python使用QQ郵箱實現自動傳送郵件

四.傳送 html 格式的郵件

import smtplib
from email.mime.text import MIMEText
#傳送多種型別的郵件
from email.mime.multipart import MIMEMultipart
import datetime
msg_from = '[email protected]' # 傳送方郵箱
passwd = 'xxxxxx'
 
to= ['[email protected]'] #接受方郵箱
 
#設定郵件內容
#MIMEMultipart類可以放任何內容
msg = MIMEMultipart()
# conntent="這個是字串"
# #把內容加進去
# msg.attach(MIMEText(conntent,'utf-8')
att2['Content-Type']='application/octet-stream'  #設定型別是流媒體格式
att2['Content-Disposition']='attachment;filename=1.jpg' #設定描述資訊
 
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="愛因斯坦"
zhuchiren="牛頓"
#構造HTML
content = '''
        <html>
        <body>
          <h1 align="center">這個是標題,xxxx通知</h1>
          <p><strong>您好:</strong></p>
          <blockquote><p><strong>以下內容是本次會議的紀要,請查收!</strong></p></blockquote>
          
          <blockquote><p><strong>發言人:{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',msg.as_string())
print("郵件傳送成功")

python使用QQ郵箱實現自動傳送郵件

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。