Python 通过邮件发送测试报告

作者: 伊洛的小屋 | 来源:发表于2020-03-09 21:53 被阅读0次
1. 自动化测试报告
  • 自动化的测试报告一般会通过邮件或钉钉机器人发送,或是直接显示在质量管理平台上来输出数据供大家查看。今天先来简单的说下Python发送邮件

本文首发于伊洛的个人博客:https://yiluotalk.com,欢迎关注并查看更多内容!!!

2. Python SMTP发送邮件
  • SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。
3. 使用其他邮件服务商的 SMTP 访问
  • 这里来用网易的163邮箱举例子
  • 登陆邮箱然后进入下图的页面,该勾的勾上
    截图来自网易邮箱
  • 然后设置一下客户端授权码
    截图来自网易邮箱
  • 发短信开启
4.简单的实例
#! /usr/bin/python3
# -*- coding: UTF-8 -*-
# 伊洛Yiluo https://yiluotalk.com

import smtplib
import time
from email.mime.text import MIMEText
from email.header import Header


class TestMail(object):

    def __init__(self):
        self.mail_host = "smtp.163.com"
        self.mail_user = "@163.com"
        self.mail_pass = "" # 163客户端授权密码

        self.sender = '@163.com'
        self.receivers = ''

    def send_mail(self):
        body = 'Dear all:\n接口自动化测试报告如下:\n   测试用例集合:{}\n   运行结果:{}'.format('xxxx', 'xxxx')
        tm = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
        message = MIMEText(body, 'plain', 'utf-8')
        message['From'] = self.mail_user
        message['To'] = self.receivers

        subject = '接口自动化报告测试'
        message['Subject'] = Header(subject + '_' + tm, 'utf-8')

        try:
            smtpObj = smtplib.SMTP()
            smtpObj = smtplib.SMTP_SSL(self.mail_host, 465)
            smtpObj.login(self.mail_user, self.mail_pass)
            smtpObj.sendmail(self.sender, self.receivers, message.as_string())
            print('send mail ok')
        except smtplib.SMTPException:
            print('send mail fail')


if __name__ == "__main__":
    send = TestMail()
    send.send_mail()
5.读取配置
  • 通常来讲一般发送邮件只是自动化框架的一个模块。封装发送邮件的时候,配置都是读取的
  • 来简单的创建一个'config.ini'的配置文件
#! /usr/bin/python3
# -*- coding: UTF-8 -*-
# 伊洛Yiluo https://yiluotalk.com

[mail]
#发送邮件信息
mail_host = smtp.163.com
mail_user = @163.com
mail_pass =                # 163客户端授权密码

sender = @163.com
receivers = 

  • 来读取一下配置
#! /usr/bin/python3
# -*- coding: UTF-8 -*-
# 伊洛Yiluo https://yiluotalk.com


import smtplib
import time
from pathlib import Path
from email.mime.text import MIMEText
from email.header import Header

from configparser import ConfigParser


class TestMail(object):

    def __init__(self):
        self.config = ConfigParser()
        self.conf_path = str(Path('config.ini').absolute())  # 读取绝对路径

        self.config.read(self.conf_path, encoding='utf-8')

    def send_mail(self):
        body = 'Dear all:\nDEMO 接口自动化测试报告:\n   测试用例集合:{}\n   运行结果:{}'.format('xxxx', 'xxxx')
        tm = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
        message = MIMEText(body, 'plain', 'utf-8')
        message['From'] = self.config.get('mail', 'sender')
        message['To'] = self.config.get('mail', 'receivers')

        subject = '接口自动化测试报告'
        message['Subject'] = Header(subject + '_' + tm, 'utf-8')

        try:
            smtpObj = smtplib.SMTP()
            smtpObj = smtplib.SMTP_SSL(self.config.get('mail', 'mail_host'), 465)
            smtpObj.login(self.config.get('mail', 'mail_user'), self.config.get('mail', 'mail_pass'))
            smtpObj.sendmail(self.config.get('mail', 'sender'), self.config.get('mail', 'receivers'), message.as_string())
            print('send mail ok')
        except smtplib.SMTPException:
            print('send mail fail')


if __name__ == "__main__":
    send = TestMail()
    send.send_mail()
  • 打开邮箱来看看邮件


    截图来自QQ邮箱
6. 发送邮件带附件
  • 发邮件有时会需要发送附件

  • 由于平时看到一些好的文章或者书会喜欢发送到kindle上面查看,其实推到kindle就是相当于给自己的kindle设备发了带有书籍文件附件的邮件,通常是mobi格式

  • 登录亚马逊官方,如下图查看自己设备对应的邮箱


    截图来自亚马逊
  • 去掉其他多余功能,简化下完,剩邮件部分代码

#! /usr/bin/python3
# -*- coding: UTF-8 -*-
# 伊洛Yiluo https://yiluotalk.com

import os
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header


def send_kindle():
    mail_host = "smtp.163.com"
    mail_user = "@163.com"
    mail_pass = ""

    sender = '@163.com'
    receivers = '@kindle.cn'

    message = MIMEMultipart()
    message['From'] = mail_user
    message['To'] = receivers
    subject = '{}'.format(filename)
    message['Subject'] = Header(subject, 'utf-8')

    message.attach(MIMEText('{}'.format(filename), 'plain', 'utf-8'))

    att1 = MIMEText(open('/Users/Documents/book/{}'.format(filename), 'rb').read(), 'base64', 'utf-8')
    att1["Content-Type"] = 'application/octet-stream'
    att1["Content-Disposition"] = 'attachment; filename="book.mobi"'
    message.attach(att1)

    try:
        smtpObj = smtplib.SMTP_SSL(mail_host, 465)
        smtpObj.login(mail_user, mail_pass)
        smtpObj.sendmail(sender, receivers, message.as_string())
        print("书籍已成功推送到您的kindle设备")
    except smtplib.SMTPException:
        print("Error: 无法推送书籍")


if __name__ == '__main__':
    filename = input("请输入您要推动到Kindle的书籍,请带Mobi后缀:")
    os.path.split(filename)
    send_kindle()

  • 运行脚本在如下会成功看到推送的内容


    截图来自亚马逊

    关注公众号获取更多内容

欢迎下方【戳一下】【点赞】
Author:伊洛Yiluo
愿你享受每一天,Just Enjoy !

相关文章

网友评论

    本文标题:Python 通过邮件发送测试报告

    本文链接:https://www.haomeiwen.com/subject/vqupdhtx.html