将测试报告发送到指定的邮箱

import smtplib,time
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart


def sentmail(file_new):
    msg = MIMEMultipart()
    msg["Subject"] = "自动化测试报告主题"
    msg["data"] = time.strftime("%a,%d %b %Y %H:%M:%S %z")
    with open(file_new, "rb")as f:
        mail_body = f.read()
    html = MIMEText(mail_body, _subtype="html", _charset="utf-8")
    msg.attach(html)
    att1 = MIMEText(mail_body, "base64", "gb2312")
    att1["Content-Type"] = "application/octet-stream"
    att1["Content-Disposition"] = "attachment;filename='TestReport.html'"
    msg.attach(att1)

    # 发送邮箱
    msg["from"] = "Kerr@123456.com"
    # 接收邮箱
    msg["to"] = "Kerr@123456.com"
    # 发送邮箱服务器
    exmail = "smtp.exmail.qq.com"
    username = "Kerr@123456.com" 
    password = "123456"

    smtp=smtplib.SMTP()  # 定义一个实例
    smtp.connect(exmail)   # 连接邮箱服务器,先把邮箱发送到对应的服务器,再由服务器去转发对应的接收邮箱
    smtp.login(username, password)   # 配置发送邮箱的用户名和密码
    smtp.sendmail(msg["from"], msg["to"], msg.as_string())   # 配置发送邮箱,接收邮箱,以及发送的内容
    smtp.close()    # 退出发邮箱服务
    print("sendmail success")


sentmail(file_new)

以上内容就足够发送到指定邮箱了,如果想多加一些功能,可以在添加。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值