1. 程式人生 > 程式設計 >python3通過qq郵箱傳送郵件以及附件

python3通過qq郵箱傳送郵件以及附件

本文例項為大家分享了python3通過qq郵箱傳送郵件以及附件的具體程式碼,供大家參考,具體內容如下

開啟qq郵箱的smtp服務

python3通過qq郵箱傳送郵件以及附件

程式碼:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart


def Mailer(to_list,th1=None,Subject=None,unipath=None):

 mail_host = 'smtp.qq.com'  # 郵箱伺服器
 mail_user = '[email protected]' # 發件人郵箱密碼(當時申請smtp給的口令)
 mail_pwd = '***********' # SMTP密碼
 s = smtplib.SMTP_SSL(mail_host,465,timeout=5)
 s.login(mail_user,mail_pwd)
 #郵件內容
 mail = str(th1)
 msg = MIMEMultipart()
 msgtext = MIMEText(mail.encode('utf8'),_subtype='html',_charset='utf8')
 msg['From'] = mail_user
 msg['Subject'] = Subject
 msg['To'] = ",".join(to_list)

 if unipath is not None:
  att1 = MIMEText(open(unipath,'rb').read(),'base64','gb2312')
  att1["Content-Type"] = 'application/octet-stream'
  att1.add_header('Content-Disposition','attachment',filename=(Subject+ '.xlsx'))
  msg.attach(att1)
 msg.attach(msgtext)
 try:
  s.sendmail(mail_user,to_list,msg.as_string())
  s.close()
  print('傳送成功')
 except Exception as e:
  print(e)

to_list = [
 #多使用者使用的list
 '[email protected]',]

Mailer(to_list,th1="這是要發的郵件內容",Subject='郵件標題',unipath=r'F:\test.xlsx')

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