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')

  

猜你喜欢

转载自www.cnblogs.com/aixw/p/10072073.html