测试员的那点事Ulord社区联盟python自动化运维

Python 通过邮件发送测试报告

2020-03-09  本文已影响0人  伊洛的小屋
1. 自动化测试报告

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

2. Python SMTP发送邮件
3. 使用其他邮件服务商的 SMTP 访问
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.读取配置
#! /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()
6. 发送邮件带附件
#! /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 !

上一篇下一篇

猜你喜欢

热点阅读