smtplib 发送包括测试报告的邮件

这里是用smtplib实现发送qq邮件,首先需要获取qq邮箱的授权码,这里就不再赘述,大家可百度哈

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


#==============定义发送邮件 ===============
class SendMail():
    def sendmail(self,filename):
        #发送邮箱服务器
        smtpserver='smtp.qq.com'
        #发送邮箱用户/密码
        user='发送人邮箱'
        password='发送人授权码'
        #发送邮箱
        sender='发送人邮箱'
        #接收邮箱
        receiver='收件人邮箱'
        #发送邮件主题
        subject='python email'
        #发送的附件
        sendfile=open(filename,'rb').read()
        att=MIMEText(sendfile,'base64','utf-8')
        att["Content-Type"]='application/octet-stram'
        att["content-Disposition"]='attachment;filename="result.html"'
        msgRoot=MIMEMultipart('related')
        msgRoot['Subject']=subject
        msgRoot.attach(att)
        #连接发送邮件
        smtp=smtplib.SMTP()
        smtp.connect(smtpserver)
        smtp.login(user,password)
        smtp.sendmail(sender,receiver,msgRoot.as_string())
        smtp.quit()
        print('邮件发送成功!')

然后在主类中调用该方法:

可参考我写的 HtmlTextRunnerNew 的博客,里面有调用该方法的代码

最后,附上发送邮件成功图片

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
发送测试报告邮件可以使用 Python 的 `smtplib` 模块和 `email` 模块。以下是一个示例代码,可以将测试报告以附件的形式发送到指定邮件地址: ```python import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication # 邮件配置 smtp_server = 'smtp.163.com' smtp_port = 465 smtp_user = 'your_email@163.com' smtp_password = 'your_password' # 收件人和发件人信息 to_address = 'to_email@qq.com' from_address = 'your_email@163.com' # 邮件主题和正文 subject = '测试报告' content = '请查收附件中的测试报告' # 测试报告文件路径 report_file = 'test_report.html' # 构造邮件内容 msg = MIMEMultipart() msg['Subject'] = subject msg['From'] = from_address msg['To'] = to_address # 添加正文 text = MIMEText(content) msg.attach(text) # 添加附件 with open(report_file, 'rb') as f: att = MIMEApplication(f.read()) att.add_header('Content-Disposition', 'attachment', filename=report_file) msg.attach(att) # 发送邮件 try: server = smtplib.SMTP_SSL(smtp_server, smtp_port) server.login(smtp_user, smtp_password) server.sendmail(from_address, to_address, msg.as_string()) server.quit() print('邮件发送成功') except Exception as e: print('邮件发送失败:', e) ``` 在代码中,需要根据实际情况修改邮件配置、收件人和发件人信息、邮件主题、正文以及测试报告文件路径。将代码中的 `your_email@163.com` 和 `your_password` 修改为实际的发件人邮箱和密码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值