import smtplibfrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextfrom email.header import Headerdef send_mail(report_name, receiver ='***@'):'''发送测试报告到邮箱:param report_name: 需要发送的测试报告:param receiver: 邮件接收人:return:'''# ----------------------------------------------------------# 获取邮件正文,读取测试报告的内容f = open(report_name, 'rb')mail_body = f.read()f.close()# 邮件服务器smtpserver = ''# 发件人和密码sender = '***@'password = '******'# 接收人receiver = receiver# 邮件主题subject = u'自动化测试报告'# ----------------------------------------------------------# 连接登录邮箱server = smtplib.SMTP(smtpserver, 25)server.login(sender, password)# ----------------------------------------------------------# 添加附件sendfile = open(report_name, 'rb').read()att = MIMEText(sendfile, "base64", 'utf-8')att['Content-Type'] = 'application/octet-stream'att['Content-Disposition'] = 'attachment;filename="report.html"'msg = MIMEMultipart('related')msgtext = MIMEText(mail_body, 'html', 'utf-8')msg.attach(msgtext)msg['From'] = sendermsg['To'] = receivermsg['Subject'] = Header(subject, 'utf-8').encode()msg.attach(att)# ----------------------------------------------------------# 发送邮件 server.sendmail(sender, [receiver], msg.as_string())server.quit()print("发送成功!")send_mail("report.html")
如果觉得《python自动化测试——测试报告以附件形式发送邮件》对你有帮助,请点赞、收藏,并留下你的观点哦!