Python可带附件的邮件发送

#coding=utf-8

import smtplib
from 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='[email protected]'                                  
                            
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()

猜你喜欢

转载自blog.csdn.net/hello_dzm/article/details/80560346