按正文、附件、正文+附件,三种方式,分别举例(仅构造邮件部分不同):
实际使用中,可使用内容+附件方式
- MIMEText:支持HTML格式的邮件正文
- MIMEMultipart:支持带附件的邮件
1. 正文
1 import smtplib 2 from email.mime.text import MIMEText 3 from email.header import Header 4 import time 5 6 7 # 发送邮箱服务器、用户、密码 8 smtpserver = 'smtp.qq.com' 9 user = '123456789' 10 passwd = 'fjdjfrewjfdrlmk' 11 12 # 发送邮箱、接收邮箱 13 sender = '123456789@qq.com' 14 receiver = '123456@163.com,123456@dingtalk.com' 15 16 # 主题、内容 17 subject = '自动化测试报告 '+ time.strftime('%Y-%m-%d %H_%M_%S') 18 file = open(r'D:zhangyangPycharmProjects est2 estresult2020-03-31 16_22_18.html', 'rb') 19 test_report = file.read() 20 file.close() 21 22 # 构造邮件(正文) 23 msg = MIMEText(test_report, 'html', 'utf-8') 24 msg['Subject'] = Header(subject, 'utf-8') 25 26 # 连接邮箱、发送邮件 27 smtp = smtplib.SMTP() 28 smtp.connect(smtpserver) 29 smtp.login(user, passwd) 30 smtp.sendmail(sender, receiver.split(','), msg.as_string()) 31 smtp.quit()
2. 附件
1 import smtplib 2 from email.mime.text import MIMEText 3 from email.mime.multipart import MIMEMultipart 4 from email.header import Header 5 import time 6 7 8 # 发送邮箱服务器、用户、密码 9 smtpserver = 'smtp.qq.com' 10 user = '123456789' 11 passwd = 'jidjfkldhnajhf' 12 13 # 发送邮箱、接收邮箱 14 sender = '123456789@qq.com' 15 receiver = '123456@163.com,123456@dingtalk.com' 16 17 # 主题、内容 18 subject = '自动化测试报告 '+ time.strftime('%Y-%m-%d %H_%M_%S') 19 file = open(r'D:zhangyangPycharmProjects est2 estresult2020-03-31 16_22_18.html', 'rb') 20 test_report = file.read() 21 file.close() 22 23 # 构造邮件(附件) 24 att = MIMEText(test_report, 'base64', 'utf-8') 25 att['Content-Type'] = 'application/octet-stream' 26 att['Content-Disposition'] = 'attachment; filename="testresult2020-03-31 16_22_18.html"' 27 28 msgRoot = MIMEMultipart('related') 29 msgRoot['Subject'] = subject 30 msgRoot.attach(att) 31 32 # 连接邮箱、发送邮件 33 smtp = smtplib.SMTP() 34 smtp.connect(smtpserver) 35 smtp.login(user, passwd) 36 smtp.sendmail(sender, receiver.split(','), msgRoot.as_string()) 37 smtp.quit()
3. 正文+附件
1 import smtplib 2 from email.mime.text import MIMEText 3 from email.mime.multipart import MIMEMultipart 4 import time 5 6 7 # 发送邮箱服务器、用户、密码 8 smtpserver = 'smtp.qq.com' 9 user = '123456789' 10 passwd = 'jfidlsjgkfsjlg' 11 12 # 发送邮箱、接收邮箱 13 sender = '123456789@qq.com' 14 receiver = '123456@163.com,123456@dingtalk.com' 15 16 # 主题、内容 17 subject = '自动化测试报告 '+ time.strftime('%Y-%m-%d %H_%M_%S') 18 file = open(r'D:zhangyangPycharmProjects est2 estresult2020-03-31 16_22_18.html', 'rb') 19 test_report = file.read() 20 file.close() 21 22 # 构造邮件(正文 + 附件) 23 msgRoot = MIMEMultipart('related') 24 msgRoot['Subject'] = subject 25 26 body = MIMEText(test_report, 'html', 'utf-8') # 正文 27 att = MIMEText(test_report, 'base64', 'utf-8') # 附件 28 att['Content-Type'] = 'application/octet-stream' 29 att['Content-Disposition'] = 'attachment; filename="testresult2020-03-31 16_22_18.html"' 30 31 msgRoot.attach(att) 32 msgRoot.attach(body) 33 34 # 连接邮箱、发送邮件 35 smtp = smtplib.SMTP() 36 smtp.connect(smtpserver) 37 smtp.login(user, passwd) 38 smtp.sendmail(sender, receiver.split(','), msgRoot.as_string()) 39 smtp.quit()
mac上会报错AttributeError: 'str' object has no attribute 'get_all',使用以下语句:
smtp.send_message(msg, sender, receiver.split(',') )
ps:
MIMEText定义正文、格式、编码
Content-Typez指定附件内容类型,application/octet-stream表示二进制流
Content-Disposition指定显示附件的文件,attachment; filename="testresult2020-03-31 16_22_18.html"指定附件的文件名
效果如下: