python发送测试报告到邮件 -- smtplib

"""
python对SMTP支持有 smtplib 和 email 两个模块
email负责构造邮件
smtplib负责发送邮件,他对smtp协议进行了简单的封装
"""
import smtplib
from email.mime.text import MIMEText  # 邮件正文
from email.header import Header  # 邮件头
from email.mime.multipart import MIMEMultipart

# 登陆服务器
smtp_obj = smtplib.SMTP_SSL("smtp.qq.com", '465')  # 发送人邮箱中的SMTP服务器  端口号
smtp_obj.login("********@qq.com", "fbmkrxsfnnqhdjib")  # 发件人邮箱账号和邮箱密码
smtp_obj.set_debuglevel(1)  # 显示调试信息

msg = MIMEMultipart()
msg["From"] = Header("来自test的问候")  # 发送者
msg["To"] = Header("有缘人", 'utf-8')  # 接受者
msg['Subject'] = Header("那美的信件", 'utf-8')  # 主题

# 读取文件
f = open('/Users/report.html','r')  # 更改成自己报告的的路径
str_html = f.read()
f.close()

# 正文
msg.attach(MIMEText(str_html, 'html', 'utf-8'))
# 构造附件
att1 = MIMEText(open('/Users/bytedance/PycharmProjects/pythonProject/ModeTest/pytest学习/基础内容/report.html', 'rb').read(), 'base64', 'utf-8')
att1['Content-Type'] = 'application/octet-stream'
att1['Content-Disposition'] = 'attachment; filename= "report.html"'
msg.attach(att1)


# 发邮件
# smtp_obj = smtplib.SMTP('localhost')
smtp_obj.sendmail("******@qq.com", ["******@qq.com"], msg.as_string())
# smtp_obj.quit()

 


版权声明:本文为bo222原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。