五十五、、Python生成报告自动发邮件

2021/9/5 12:06:16

本文主要是介绍五十五、、Python生成报告自动发邮件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

python 获取当前目录,上级目录,上上级目录

 

import os

print '***获取当前目录***'
print os.getcwd()
print os.path.abspath(os.path.dirname(__file__))

print '***获取上级目录***'
print os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
print os.path.abspath(os.path.dirname(os.getcwd()))
print os.path.abspath(os.path.join(os.getcwd(), ".."))

print '***获取上上级目录***'
print os.path.abspath(os.path.join(os.getcwd(), "../.."))

 

 

使用QQ邮箱发送邮件,使用的是授权码,需要先到QQ邮箱申请授权码。

邮箱设置-->账户

 

 

 

 

import smtplib
from email.mime.text import MIMEText

# 参数配置
smtpserver = "smtp.qq.com"  # 发送邮件的服务器
port = 465  # 端口
sender = "190xxx38946@qq.com"  # 发送的邮箱
psw = "suw******vzucehc"  # QQ授权码,这里填写上自己的授权码
receiver = "2753xxxx929@qq.com"  # 接收邮件的邮箱

# 写信模板
body = '<pre><h1>吴湘雨的测试报告,请查收</h1></pre>'

msg = MIMEText(body, 'html', "utf-8")
msg['from'] = sender
msg['to'] = receiver
msg['subject'] = "这是自动化测试报告"  # 邮件的主题

# 写信流程
try:
    smtp1 = smtplib.SMTP_SSL(smtpserver, port)  # 实例化
    smtp1.login(sender, psw)  # 登录
    smtp1.sendmail(sender, receiver, msg.as_string())  # 配置发送邮箱,接收邮箱,以及发送内容
    smtp1.quit()  # 关闭发邮件服务
    print("邮件发送成功!")
except smtplib.SMTPException:
    print("Error: 抱歉!发送邮件失败。")

 

发送邮件给多个领导:

 

 

# coding:utf-8
import smtplib
from email.mime.text import MIMEText

# 参数配置
smtpserver = "smtp.qq.com"  # 发送邮件的服务器
port = 465  # 端口
sender = "3437871062@qq.com"  # 发送的邮箱
psw = "tdcejfnutrascjee"  # QQ授权码,这里填写上自己的授权码
# receiver = "1039020476@qq.com"  # 接收邮件的邮箱
receiver = ['3437871062@qq.com', '1039020476@qq.com'] # 用列表装多个领导的邮箱
# 写信模板
body = '<pre><h1>测试报告,请查收`</h1></pre>'

msg = MIMEText(body, 'html', "utf-8")
msg['from'] = sender
msg['to'] = ','.join(receiver) # 用join处理一下,分别发送
msg['subject'] = "这是自动化测试报告"  # 邮件的主题

# 写信流程
try:
    smtp1 = smtplib.SMTP_SSL(smtpserver, port)  # 实例化
    smtp1.login(sender, psw)  # 登录
    smtp1.sendmail(sender, msg['to'].split(','), msg.as_string())  # 配置发送邮箱,接收邮箱,以及发送内容
    smtp1.quit()  # 关闭发邮件服务
    print("邮件发送成功!")
except smtplib.SMTPException:
    print("Error: 抱歉!发送邮件失败。")

 

 

二、发送带正文和附件的邮件

 

先看下文件目录

 

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


def send_email(smtpserver, port, sender, psw, receiver):
    # 写信模板
    msg = MIMEMultipart()
    msg['Subject'] = "这是ssp项目自动化测试报告"
    msg['From'] = sender
    msg['to'] = receiver

    # 通过os获取文件路径
    # current_path = os.getcwd()  # 获取当前脚本所在的文件夹路径
    annex_path = os.path.abspath(os.path.dirname(os.getcwd())) +'\\report'
    b=os.path.join(annex_path, "report.html")  # 附件内容的路径
    a = open(b, "r", encoding="utf-8").read()

    body = '<pre><h1>测试报告,请查收`</h1></pre>'

    # 添加正文到容器
    body = MIMEText(body, "html", "utf-8")
    msg.attach(body)

    # 添加附件到容器
    att = MIMEText(a, "base64", "utf-8")
    att["Content-Type"] = "application/octet-sream"
    att["Content-Disposition"] = 'attachment;filename="ssp_test_report.html"'
    msg.attach(att)

    # 连接发送邮件
    smtp = smtplib.SMTP_SSL(smtpserver, port)
    smtp.login(sender, psw)
    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()

if __name__ == "__main__":
    send_email("smtp.qq.com", 465, "190xxxx46@qq.com", "你的授权码", "27xxxxx9@qq.com")

 

 

 

 

 

 

三、生成报告的同时自动发邮件

 

 

 

import unittest,os
from commen import HTMLTestRunner
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import os


def send_email(smtpserver, port, sender, psw, receiver):
    # 写信模板
    msg = MIMEMultipart()
    msg['Subject'] = "这是ssp项目自动化测试报告"
    msg['From'] = sender
    msg['to'] = receiver

    # 通过os获取文件路径
    # current_path = os.getcwd()  # 获取当前脚本所在的文件夹路径
    annex_path = os.path.abspath(os.path.dirname(os.getcwd())) +'\\all\\report'
    b=os.path.join(annex_path, "report.html")  # 附件内容的路径
    a = open(b, "r", encoding="utf-8").read()

    body = '<pre><h1>测试报告,请查收`</h1></pre>'

    # 添加正文到容器
    body = MIMEText(body, "html", "utf-8")
    msg.attach(body)

    # 添加附件到容器
    att = MIMEText(a, "base64", "utf-8")
    att["Content-Type"] = "application/octet-sream"
    att["Content-Disposition"] = 'attachment;filename="ssp_test_report.html"'
    msg.attach(att)

    # 连接发送邮件
    smtp = smtplib.SMTP_SSL(smtpserver, port)
    smtp.login(sender, psw)
    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()



def all_test():
    # test_dir="F:\\learn\\python\\all\\test"    绝对路径
    dir = os.getcwd()  #相对路径
    test_path = os.path.join(dir, "test")
    discover=unittest.defaultTestLoader.discover(
                                                    # test_dir,  绝对路径
                                                    test_path,
                                                    pattern="test*.py"
                                                    )

    return discover


if __name__ == '__main__':
    result_path="F:\\learn\\python\\all\\report\\report.html"
    fb = open(result_path,"wb")

    runner= HTMLTestRunner.HTMLTestRunner(stream=fb,
                                          title="我的教育系统登录测试报告",
                                          description="自动化测试用例执行的情况")
    runner.run(all_test())

    fb.close()
    send_email("smtp.qq.com", 465, "648156323@qq.com", "suweyvyohvzucehc", "648156323@qq.com")

 

 

 

 

 

 

 



这篇关于五十五、、Python生成报告自动发邮件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程