使用python将最新的测试报告以附件的形式发到指定邮箱

作者:libre_yop 时间:2022-08-24 14:59:16 

具体代码如下所示:


import smtplib, email, os, time
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
#设置smtplib所需的参数
smtpserver = 'smtp.qq.com' #SMTP服务器地址
username = 'xxx@qq.com' # 发件人地址,通过控制台创建的发件人地址
password = '******' # 发件人密码,通过控制台创建的发件人密码
receiver = ['xxx@dadaodata.com'] #单个收件人
# receivers = ['xxx@dadaodata.com', 'xxx@qq.com'] # 收件人地址或是地址列表,支持多个收件人,最多30个
# 构造邮件MIMEMultipart对象
msg = MIMEMultipart('mixed')
msg['Subject'] = Header('自动化测试报告' + time.strftime("%Y-%m-%d"), 'utf-8').encode()#自定义邮件主题
msg['From'] = '%s <%s>' % (username, username)#邮件发送者
msg['To'] = ";".join(receiver)#邮件接受者
msg['Message-id'] = email.utils.make_msgid()
msg['Date'] = email.utils.formatdate()
# 构造文字内容
text_plain = MIMEText('附件为接口自动化测试报告,请查收!', 'plain', 'utf-8')#邮件内容
msg.attach(text_plain)
#构造附件
test_report = r'F:\PythonAutomation\Python_PyCharm\TestReport' #存放文件的目录
lists = os.listdir(test_report) #列出目录的下所有文件保存到lists
lists.sort(key=lambda fn:os.path.getmtime(test_report + "\\" + fn)) #按时间排序
file_new = os.path.join(test_report,lists[-1]) #获取最新的文件保存到file_new
sendfile = open(file_new,'rb').read()
text_att = MIMEText(sendfile, 'base64', 'utf-8')
text_att["Content-Type"] = 'application/octet-stream'
text_att["Content-Disposition"] = 'attachment; filename="report.html"'#重新命名附件
msg.attach(text_att)
# 发送邮件
try:
 # client = smtplib.SMTP()
 # client.connect(smtpserver, 25) #SMTP普通端口为25
 client = smtplib.SMTP_SSL() #python 2.7以上版本,若需要可使用SSL
 client.connect(smtpserver, 465) #SSL端口465
 # client.set_debuglevel(1) #用set_debuglevel(1)可以打印出和SMTP服务器交互的所有信息
 client.login(username, password)
 client.sendmail(username, receiver, msg.as_string())
 client.quit()
 print('邮件发送成功')
except smtplib.SMTPConnectError as e:
 print('邮件发送失败,连接失败:', e.smtp_code, e.smtp_error)
except smtplib.SMTPAuthenticationError as e:
 print('邮件发送失败,认证错误:', e.smtp_code, e.smtp_error)
except smtplib.SMTPSenderRefused as e:
 print('邮件发送失败,发件人被拒绝:', e.smtp_code, e.smtp_error)
except smtplib.SMTPRecipientsRefused as e:
 print('邮件发送失败,收件人被拒绝:', e.args, e.recipients)
except smtplib.SMTPDataError as e:
 print('邮件发送失败,数据接收拒绝:', e.smtp_code, e.smtp_error)
except smtplib.SMTPException as e:
 print('邮件发送失败: ', str(e))
except Exception as e:
 print('邮件发送失败: ', str(e))

执行结果如下:

使用python将最新的测试报告以附件的形式发到指定邮箱

总结

以上所述是小编给大家介绍的使用python将最新的测试报告以附件的形式发到指定邮箱,网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

来源:https://www.cnblogs.com/libre-yop/archive/2019/09/20/11555709.html

标签:python,发送,邮箱
0
投稿

猜你喜欢

  • Python 类的私有属性和私有方法实例分析

    2021-06-18 05:13:47
  • php生成缩略图填充白边(等比缩略图方案)

    2024-06-05 09:50:16
  • Python模仿POST提交HTTP数据及使用Cookie值的方法

    2022-05-04 04:37:35
  • 怎样修改 MySQL数据库中的密码

    2008-11-27 15:35:00
  • 用ASP设计购物车

    2008-04-17 13:52:00
  • PHP错误Allowed memory size of 67108864 bytes exhausted的3种解决办法

    2024-03-15 18:25:42
  • pip install urllib2不能安装的解决方法

    2022-05-27 12:25:13
  • oracle 性能优化建议小结

    2010-04-22 16:32:00
  • MySQL中如何优化order by语句

    2024-01-23 09:49:25
  • python实现线性插值的示例

    2023-08-10 21:33:55
  • 使用Python遍历文件夹实现查找指定文件夹

    2021-01-19 09:23:06
  • 基于KL散度、JS散度以及交叉熵的对比

    2021-05-05 01:27:07
  • 支持多类型数据库的c#数据库模型示例

    2024-01-13 16:41:15
  • python 统计代码行数简单实例

    2022-05-08 21:04:34
  • PHP利用ChatGPT实现轻松创建用户注册页面

    2023-05-25 09:22:16
  • Python基于Socket实现的简单聊天程序示例

    2022-12-22 09:14:50
  • 完美解决MySQL中文乱码

    2011-03-16 15:16:00
  • 详细分析mysql MDL元数据锁

    2024-01-28 18:29:10
  • 用函数模板,写一个简单高效的 JSON 查询器的方法介绍

    2024-04-18 10:53:00
  • 阿里云CentOS7搭建Apache+PHP+MySQL环境

    2023-11-23 02:44:59
  • asp之家 网络编程 m.aspxhome.com