码迷,mamicode.com
首页 > 其他好文 > 详细

自动化测试常用脚本-发送邮件

时间:2019-08-16 18:47:10      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:pat   mem   内容类型   测试结果   内容   def   file   name   smt   

#用于发送测试报告或其它内容

#
-*- coding:utf-8 -*- import os import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart class SendEmail(object): def __init__(self, receivers): mail_host = "127.0.0.1" # SMTP服务器 mail_user = "username" # 用户名 mail_pass = "password" # 密码 sender = "123456@qq.com" # 发件人邮箱 file_path = os.path.dirname(os.path.dirname(__file__)) + \\report\\test_result.html title = 自动化测试结果 # 邮件主题 mail_body = "查看测试报告请下载附件!!!" message = MIMEMultipart() message[From] = "{}".format(sender) message[To] = ",".join(receivers) message[Subject] = title msgtext = MIMEText(mail_body, _subtype=plain, _charset=utf-8) message.attach(msgtext) # 添加主邮件主体内容 # 添加一个HTML文本附件 ff = open(file_path, rb) att = MIMEText(ff.read(), base64, utf-8) # 附件设置内容类型,设置为二进制流 att["Content-Type"] = application/octet-stream # 设置附件头,添加文件名 att["Content-Disposition"] = attachment; filename="test_result.html" # 解决中文附件名乱码问题 # att.add_header(‘Content-Disposition‘, ‘attachment‘, filename=(‘utf-8‘, ‘‘, basename)) message.attach(att) ff.close() try: smtpObj = smtplib.SMTP(mail_host, 25) smtpObj.login(mail_user, mail_pass) smtpObj.sendmail(sender, receivers, message.as_string()) # 发送 print("邮件已成功发送") smtpObj.quit() except smtplib.SMTPException as e: print(e) if __name__ == "__main__": SendEmail(["123456@qq.com", "123467@qq.com"])

 

自动化测试常用脚本-发送邮件

标签:pat   mem   内容类型   测试结果   内容   def   file   name   smt   

原文地址:https://www.cnblogs.com/chenri/p/11365640.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!