通过邮件发送最新的测试报告

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

#发送邮箱服务器
smtp_server = 'smtp.exmail.qq.com'

#发送邮箱用户名
smpt_user = 'test@qq.com'
#发送邮箱密码
smpt_password = 'test'
#发送邮箱
sender = 'test@qq.com'
#接收邮箱
receiver = "test11@163.com"
#邮件主题
subject  = 'Python email test'
#编写HTML类型邮件正文
# msg = MIMEText('<html><h1>你好!</h1></html>','html','utf-8')
# msg['Subject'] = Header(subject,'utf-8')

sendfile = open('C:\Users\cao\Desktop\log.txt' , 'rb').read()

att = MIMEText(sendfile,'base64','utf-8')
att['Content-Type'] = 'application/octet-stream'
att['Content-Disposition'] = 'attachment; filename="log.txt"'

msgRoot = MIMEMultipart('related')

msgRoot['Subject'] = subject
msgRoot.attach(att)

smtp = smtplib.SMTP()
try:
    smtp.connect(smtp_server)
    smtp.login(smpt_user,smpt_password)
    smtp.sendmail(sender,receiver,msgRoot.as_string())
    print("成功")
except Exception as e:
    print(e)
    print("Error 失败")
finally:
    smtp.quit()

 实现发送带有附件的邮件

import os

#定义文件目录
result_dit = 'C:\Users\cao\Desktop\CADMS'

lists = os.listdir(result_dit)

#按照时间对文件进行排序
lists.sort(key=lambda fn: os.path.getatime(result_dit+"\" + fn))

# print(lists[-1])

file = os.path.join(result_dit,lists[-1])

print(file)

与发送邮件结合,即可通过邮件发送文件夹内最新的文件(最新的测试报告)

原文地址:https://www.cnblogs.com/wbw-test/p/11004814.html