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

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

#
-*- 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(["[email protected]", "[email protected]"])

猜你喜欢

转载自www.cnblogs.com/chenri/p/11365640.html