import smtplib, os
from email.header import Header
from email.mime.text import MIMEText
from Log.Logs import TestRecord
from email.mime.multipart import MIMEMultipart
# 第三方 SMTP 服务
def send_mail(accepter):
mail_host = "smtp.163.com" # SMTP服务器
mail_user = "xu354809049@163.com" # 用户名
mail_pass = "xxxxxxxx" # 授权密码,非登录密码
sender = 'xu354809049@163.com' # 发件人邮箱
receivers = ['' + accepter + ''] # 接收邮件,可设置为任意收件邮箱
message = MIMEMultipart()
message['From'] = Header("xu354809049@163.com")
message['To'] = Header("各位负责人")
subject = 'xxxxx平台UI自动化测试报告'
message['Subject'] = Header(subject, 'utf-8')
# 邮件正文内容
message.attach(MIMEText('这是xxxxx的UI自动化测试报告,详情请见附件:', 'plain', 'utf-8'))
# 构造附件1,传送log目录下的所有test.Log 文件
for i in range(0, len(TestRecord().test_log())):
att1 = MIMEText(
open(os.path.dirname(os.path.dirname(__file__)) + '\\Log\\' + TestRecord().test_log()[i], 'rb').read(),
'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# 这里的filename代表邮件中显示的名字
att1["Content-Disposition"] = 'attachment;filename=' + TestRecord().test_log()[i]
message.attach(att1)
# 构造附件2,传送test_report目录下的所有 result.html 文件
for i in range(0, len(TestRecord().test_html())):
att2 = MIMEText(
open(os.path.dirname(os.path.dirname(__file__)) + '\\TestReport\\' + TestRecord().test_html()[i],
'rb').read(), 'base64', 'utf-8')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment;filename=' + TestRecord().test_html()[i]
message.attach(att2)
# 构造附件3,传送test_report目录下的所有 test.png 文件
for i in range(0, len(TestRecord().test_image())):
att3 = MIMEText(
open(os.path.dirname(os.path.dirname(__file__)) + '\\TestReport\\Screen\\' + TestRecord().test_image()[i],
'rb').read(), 'base64', 'utf-8')
att3["Content-Type"] = 'application/octet-stream'
att3["Content-Disposition"] = 'attachment;filename=' + TestRecord().test_image()[i]
message.attach(att3)
try:
smtpObj = smtplib.SMTP_SSL(mail_host, 465)
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
print("恭喜您,自动化测试报告已发送成功")
except smtplib.SMTPException as e:
print(e)