python邮件发送自动化测试报告

话不多说直接贴代码

 

# encoding: utf-8
import smtplib #发送邮件模块
from email.mime.text import MIMEText #邮件内容
from email.header import Header



def send_email(new_reportfile):
"""发送邮件"""
f = open(new_reportfile,'rb')
mail_content = f.read()
f.close()

# 发送邮箱服务器
smtpserver = 'smtp.163.com'

# 用户名密码
user = 'ye_songqiao123@163.com'
password = '你的密码or授权码'

# 发送和接收邮箱用户
sender = 'ye_songqiao123@163.com'
receiver = '719584032@qq.com'
# 发送给多人
# receiver = ['123@qq.com','23432@qq.com','719584032@qq.com']

# 定义标题和内容
biaoti = "YT API 自动化测试报告"

# HTML邮件正文
msg = MIMEText(mail_content, 'html', 'utf-8')
msg['subject'] = Header(biaoti, 'utf-8')
msg['from'] = sender
msg['to'] = receiver
# 发送给多人,已逗号为分隔符,针对receiver这个变量
# msg['to'] =','.join(receiver)

# SSL协议端口号要使用465
smtp = smtplib.SMTP_SSL(smtpserver, 465)

# HELO 像服务器标识用户身份
smtp.helo(smtpserver)

# 服务器返回结果确认
smtp.ehlo(smtpserver)

# 登陆用户
smtp.login(user, password)
print("开始发送邮件.............")
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
print("邮件发送完成.....................")


if __name__ == '__main__':
path = 'E:\\testreport\\YTtest.html'
send_email(path)
posted @ 2019-12-12 18:17  叶先生啊  阅读(690)  评论(0编辑  收藏  举报