python实现自动发送邮件具体步骤参考笔者的另一篇博文,python实现邮件的发送
本次只展示发送附件的代码,MIMEApplication支持常用格式文档(.jpg、.mp3、zip等)当做附件上传
代码如下:
1 #!/usr/bin/python 2 # -*- coding: UTF-8 -*- 3 4 import smtplib 5 from email.mime.multipart import MIMEMultipart 6 from email.mime.application import MIMEApplication 7 8 smtp_server = 'smtp.163.com' 9 sender = 'SunshineWuya@163.com' 10 pwd = 'xxxx' 11 12 txt = MIMEMultipart() 13 txt['Subject'] = '自动化测试报告' 14 txt['From'] = sender 15 16 # 上传附件 17 part = MIMEApplication(open('/Users/ydj/Desktop/微信后台测试.html','rb').read()) 18 part.add_header('Content-Disposition', 'attachment', filename=('utf-8','','微信后台测试.html')) 19 txt.attach(part) 20 21 mail_server = smtplib.SMTP(smtp_server,25) 22 mail_server.login(sender,pwd) 23 mail_server.sendmail(sender,['SunshineWuya@163.com'],txt.as_string()) 24 25 mail_server.quit()
结果如下: