python接口自动化学习(三)邮件功能

给脚本添加发送邮件功能
可以先看python发送邮件

configEmail.py代码:

import os
import smtplib
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from FlychordInterfaceTest.getPathInfo import get_Path
from FlychordInterfaceTest.readConfig import get_email

path = os.path.join(get_Path(), 'report')


# 查找最新的测试报告
def newReport():
    # 定义文件目录
    lists = os.listdir(path)
    # 重新按时间对目录下的文件进行排序
    lists.sort(key=lambda fn: os.path.getmtime(path + "\\" + fn))
    # print(('最新的文件为: ' + lists[-1]))
    file = os.path.join(path, lists[-1])
    # print(file)
    return file


# 上传附件
def att(path, name):
    sendfile = open(path, 'rb').read()
    att = MIMEText(sendfile, 'base64 ', 'utf-8')
    att['Content-type'] = 'application/octet-stream'
    # 附件在邮件中显示的名字,附件名称为中文时的写法
    att.add_header("Content-Disposition", "attachment", filename=("gbk", "", name))
    # 附件在邮件中显示的名字,附件名称为非中文时的写法
    # att['Content-Disposition'] = 'attachment;filename= "' + name + '"'
    return att


def setMail():
    smtpserver = get_email('mail_host')  # 发送者邮件服务器
    mailuser = get_email('mail_user')  # 发送着邮件账户
    password = get_email('mail_pass')  # 发送者邮件授权码
    receiver = get_email('sender')  # 接收者邮件账户
    subject = get_email('subject')  # 邮件标题
    mailbody = get_email('content')  # 邮件正文
    reportAttachment = get_email('reportAttachment')  # 报告附件名称
    file1 = newReport()
    msg = MIMEMultipart()
    # 附件加入到邮件中
    msg.attach(att(file1, reportAttachment))
    msg['Subject'] = Header(subject, 'utf-8')
    msg['From'] = mailuser  # 之前没有写From和To,发送邮件出现554错误
    msg['To'] = receiver
    msg.attach(MIMEText(mailbody, 'plain', 'utf-8'))
    try:
        # 连接发送邮件
        smtp = smtplib.SMTP()
        smtp.connect(smtpserver)
        smtp.login(mailuser, password)
        smtp.sendmail(mailuser, receiver, msg.as_string())
        smtp.quit()
        print('邮件发送成功')
    except Exception:
        # logs.smtplib('Error')
        pass
    else:
        # logs.smtplib('Success')
        pass


def sendMail():
    on_off = int(get_email('on_off'))
    if on_off == 1:
        setMail()
    elif on_off == 0:
        print('不发送邮件')
    else:
        print('开关设置错误')


if __name__ == '__main__':
    sendMail()

msg.attach(MIMEText(mailbody, 'plain', 'utf-8'))改成msg.attach(MIMEText(mailbody, 'html', 'utf-8'))可以发送HTML格式邮件,但是不建议使用,在邮箱中可能会导致页面显示不全

run.py代码:

import unittest
from time import strftime
from FlychordInterfaceTest.common import configEmail
from BeautifulReport import BeautifulReport

# 格式化获取时间 以时间戳命名时不能用冒号(:)
ctime = strftime('%Y-%m-%d %H-%M-%S')
report_name = ctime + '-report.html'
test_suite = unittest.defaultTestLoader.discover('./case', pattern='test*.py')
# 调用测试报告框架
runner = BeautifulReport(test_suite)
# 定义测试报告相关的参数
runner.report(filename=report_name, description='flychord接口测试报告', log_path='./report')
# 发送邮件
configEmail.sendMail()

# 程序的执行入口
if __name__ == "__main__":
    unittest.main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值