栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python发送邮件:获取最新测试报告并发送电子邮件

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python发送邮件:获取最新测试报告并发送电子邮件

一、目录结构


#coding=utf-8
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib,os

def get_report_file(report_path):
“”"
获取最新的测试报告
:param report_path:
:return:
“”"
lists = os.listdir(report_path)
lists.sort(key=lambda fn: os.path.getmtime(os.path.join(report_path, fn)))
print(u’最新测试生成的报告: '+lists[-1])
# 找到最新生成的报告文件
report_file = os.path.join(report_path, lists[-1])
return report_file

def send_mail(sender, psw, receiver, cc_receiver, smtpserver, report_file, port):
“”“发送最新的测试报告内容”""
with open(report_file, “rb”) as f:
mail_body = f.read()
# 定义邮件内容
msg = MIMEMultipart()
body = MIMEText(mail_body, _subtype=‘html’, _charset=‘utf-8’)
msg[‘Subject’] = u"接口自动化测试报告"
msg[“from”] = sender
msg[“to”] = “,”.join(receiver)
msg[“Cc”] = “,”.join(cc_receiver)
msg.attach(body)
# 添加附件
att = MIMEText(open(report_file, “rb”).read(), “base64”, “utf-8”)
att[“Content-Type”] = “application/octet-stream”
att[“Content-Disposition”] = ‘attachment; filename= “report.html”’
msg.attach(att)
smtp = smtplib.SMTP()
smtp.connect(smtpserver, port)
# 用户名密码
smtp.login(sender, psw)
smtp.sendmail(sender, receiver+cc_receiver, msg.as_string())
smtp.quit()
print(‘Test report email has send to {} !’.format(receiver))

def main():
# 获取最新的测试报告文件
report_path = os.path.join(cur_path, “report”) # 测试报告文件夹
report_file = get_report_file(report_path) # 获取最新的测试报告
print(‘report_file: ‘, report_file)
# 邮箱配置
sender = readconfig.sender
psw = readconfig.psw
smtp_server = readconfig.smtp_server
port = readconfig.port
receivers = readconfig.receiver.split(’,’)
cc_receiver = readconfig.cc_receiver.split(’,’)
send_mail(sender, psw, receivers, cc_receiver, smtp_server, report_file, port) # 发送报告

if name == “main”:
main()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/423342.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号