Python 发送测试报告

Python 发送测试报告

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


class Email():
    # 定义发邮件
    def send_mail(self,file_path):
        """
        :param file_path:   report.html 路径
        """
        smtpserver = 'smtp.163.com'
        # 设置登录邮箱的账号和授权密码
        user = 'xxxxxxxxxxxxxxxxxxx@163.com'
        password = "NFWHTUIWGVGUEKFJ" #授权密码,不是邮箱的密码
        sender = 'xxxxxxxxxxxxxxxxxxx@163.com'
        # 可添加多个收件人的邮箱
        receives = ['xxxxxxxxxxxxxxxxxxx@xxx.com']
        # 构造邮件对象
        msg = MIMEMultipart('mixed')
        # 定义邮件的标题
        subject = '接口自动化测试报告'
        # HTML邮件正文,定义成字典
        msg['Subject'] = Header(subject, "utf-8")
        msg['From'] = sender
        msg['To'] = receives
        # 构造文字内容
        text_plain = MIMEText("附件是最新的自动化测试报告,请查看", 'html', 'utf-8')
        msg.attach(text_plain)

        # 构造附件
        f = open(file_path, 'rb')
        mail_body = f.read()
        f.close()
        text_attr = MIMEText(mail_body, 'base64', 'utf-8')
        text_attr["Content-Type"] = 'application/octet-stream'
        text_attr['Content-Disposition'] = 'attachment; filename = "test.html"'
        msg.attach(text_attr)

        # 邮箱设置时勾选了SSL加密连接,进行防垃圾邮件,SSL协议端口号要使用465
        smtp = smtplib.SMTP_SSL(smtpserver, 465)
        # 向服务器标识用户身份
        smtp.helo(smtpserver)
        # 向服务器返回确认结果
        smtp.ehlo(smtpserver)
        # 登录邮箱的账号和授权密码
        smtp.login(user, password)

        print("开始发送邮件...")
        # 开始进行邮件的发送,msg表示已定义的字典
        smtp.sendmail(sender, receives, msg.as_string())
        smtp.quit()
        print("已发送邮件")


if __name__ == "__main__":
    e =Email()
    report = "./report.html"
    e.send_mail(report)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值