提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
05_发送测试报告到邮箱
- 1、获取邮箱授权码
- 2、构造附件
- 3、发送邮件
- 4、完整示例
测试报告生成后,可以将报告发送至开发、测试邮箱,以便查看测试报告详情,本文将演示如何将生成的html测试报告、excel 文件作为附件发送到指定邮箱
1、获取邮箱授权码
以QQ邮箱为例:登录–>账号与安全–>安全设置–>生成授权码,如下:
2、构造附件
构造html附件
# 构造发送邮件的附件
html_file = open(html_path,'rb').read() # 为html文件的路径
# 构造附件1:html格式
attachments_html = MIMEText(html_file,'base64','utf-8')
attachments_html['Content-Type'] = 'application/octet-stream'
attachments_html['Content-Disposition'] = 'attachment; filename = "test_report.html"' #filename是指下载的附件的命名
构造xlsx附件
# 构造附件2: xlsx格式
xlsx_file = open(xlsx_path, 'rb').read()
attachments_xlsx=MIMEText(xlsx_file,'base64','utf-8') #读取本地xlsx,路径用/不要\
attachments_xlsx["content_Type"]='application/octet-stream'
attachments_xlsx.add_header('Content-Disposition', 'attachment', filename='excel.xlsx')
3、发送邮件
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
#第三方SMTP服务
smtp_server = 'smtp.qq.com' #设置qq服务器
smtp_port = 25 #设置qq服务器端口号
sender = '156XXXX@qq.com' #用户名 & 发送者邮箱
smtp_authorization_code = 'test1111' #,密码(步骤1中获得的授权码)
receiver = ['123333333@qq.com'] #接受者邮箱
msg = MIMEMultipart()
msg['Subject'] = emial_subject #定义邮件主题
msg.attach(MIMEText('Python 邮件发送测试报告……', 'plain', 'utf-8')) # 邮件正文
msg['From'] = sender
msg['To'] = ', '.join(receiver)
msg.attach(attachments_html) #添加html附件
msg.attach(attachments_xlsx) #新增xlsx附件
try:
# 开启发信服务,这里使用的是非加密传输
server = smtplib.SMTP(smtp_server, smtp_port)
# 登录发信邮箱
server.login(sender, smtp_authorization_code)
# 发送邮件
server.sendmail(sender, receiver, msg.as_string()) # 发送方,所有收件人(包含抄送者),之前message构建的邮件信息
# 关闭服务器
server.quit()
print("邮件发送成功")
except smtplib.SMTPException as e:
print('邮件发送失败{e}')
4、完整示例
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
def send_email(emial_subject,html_path,xlsx_path,receivers):
r"""
发送邮件
:param emial_title: 邮件标题
:param html_path: 附件1:html路径
:param xlsx_path: 附件2:xlsx 路径
:param receivers: 收件人邮箱列表,eg:['11111@qq.com','222@qq.com']
:return: None
"""
# 构造发送邮件的附件
html_file = open(html_path,'rb').read()
# 构造附件1:html格式
attachments_html = MIMEText(html_file,'base64','utf-8')
attachments_html['Content-Type'] = 'application/octet-stream'
attachments_html['Content-Disposition'] = 'attachment; filename = "test_report.html"' #filename是指下载的附件的命名
# 构造附件2: xlsx格式
xlsx_file = open(xlsx_path, 'rb').read()
attachments_xlsx=MIMEText(xlsx_file,'base64','utf-8') #读取本地xlsx,路径用/不要\
attachments_xlsx["content_Type"]='application/octet-stream'
attachments_xlsx.add_header('Content-Disposition', 'attachment', filename='excel.xlsx')
# # 构造附件3,图片格式
# msgImage = MIMEImage(open('test.png', 'rb').read())
# msgImage.add_header('Content-ID', '<image1>')
# msgImage.add_header('Content-Disposition', 'attachment', filename='test.png')
#
# # 构造附件4 txt格式
# att4 = MIMEText(open('./test.txt', 'rb').read(), 'base64', 'utf-8')
# att4["Content-Type"] = 'application/octet-stream'
# att4["Content-Disposition"] = 'attachment; filename="test.txt"'
#
smtp_server = 'smtp.qq.com' #设置qq服务器
smtp_port = 25 #设置qq服务器端口号
sender = '156XXXX@qq.com' #用户名 & 发送者邮箱
smtp_authorization_code = 'test1111' #,密码(步骤1中获得的授权码)
receiver = ['123333333@qq.com'] #接受者邮箱
msg = MIMEMultipart()
msg['Subject'] = emial_subject #定义邮件主题
msg.attach(MIMEText('Python 邮件发送测试报告……', 'plain', 'utf-8')) # 邮件正文
msg['From'] = sender
msg['To'] = ', '.join(receiver)
msg.attach(attachments_html) #添加html附件
msg.attach(attachments_xlsx) #新增xlsx附件
try:
# 开启发信服务,这里使用的是非加密传输
server = smtplib.SMTP(smtp_server, smtp_port)
# 登录发信邮箱
server.login(sender, smtp_authorization_code)
# 发送邮件
server.sendmail(sender, receiver, msg.as_string()) # 发送方,所有收件人(包含抄送者),之前message构建的邮件信息
# 关闭服务器
server.quit()
print("邮件发送成功")
except smtplib.SMTPException as e:
print('邮件发送失败{e}')
if __name__ == "__main__":
send_email("接口自动化测试","report.html","1.xlsx",['reverver112@163.com'])