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)
以上内容就足够发送到指定邮箱了,如果想多加一些功能,可以在添加。