Python可帶附件的郵件傳送
阿新 • • 發佈:2018-11-08
#coding=utf-8
import smtplibfrom email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText
msg_from='[email protected]' #發件人郵箱
passwd='xxxxx' #收件人郵箱
msg_to='
subject="python郵件測試"
content=("這是我使用python smtplib及email模組傳送的郵件")
msg = MIMEMultipart()
#msg = MIMEText(content)
msg['Subject'] = subject
msg['From'] = msg_from
msg['To'] = msg_to
part = MIMEText("diyiciceshi")
msg.attach(part)
part = MIMEApplication(open(r'C:\Users\Administrator\Desktop\foo.pdf','rb').read())
part.add_header('Content-Disposition', 'attachment', filename="foo.pdf")
msg.attach(part)
try:
s = smtplib.SMTP_SSL("smtp.qq.com",465)
s.login(msg_from, passwd)
s.sendmail( msg_from , msg_to , msg.as_string())
print("傳送成功")
except s.SMTPException as e:
print("傳送失敗")
finally:
s.quit()