1. 程式人生 > >python傳送郵件(帶附件)

python傳送郵件(帶附件)

python通過stmp傳送qq郵件,帶附件

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


"""
1》測試郵件傳送,帶附件
2》有收件人、發件人、主題、郵件內容
"""

sender = '[email protected]'
receivers = ['[email protected]']  # 接收郵件,可設定為你的QQ郵箱或者其他郵箱
stmp_pwd = 'xx'
stmp_server = 'smtp.qq.com'
stmp_port = 25

message = MIMEMultipart()

message['From'] = sender
message['To'] = ';'.join(receivers)
message['Subject'] = Header('豆瓣動畫電影推薦', 'utf-8')

#郵件內容
message.attach(MIMEText('豆瓣動畫電影推薦', 'plain', 'utf-8'))

#附件
att2 = MIMEText(open('豆瓣動畫電影1543993335.5267258.xls', 'rb').read(), 'base64', 'utf-8')
att2["Content-Type"] = 'text/plain'
att2["Content-Disposition"] = 'attachment; filename="dianyingtuijian.xls"'
message.attach(att2)


smtpObj = smtplib.SMTP(stmp_server, stmp_port)
# smtpObj.set_debuglevel(1)
smtpObj.starttls()
smtpObj.login(sender, stmp_pwd)
smtpObj.sendmail(sender, receivers, message.as_string())
smtpObj.quit()
print('success')