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 = '[email protected]'
password = '你的密码or授权码'

# 发送和接收邮箱用户
sender = '[email protected]'
receiver = '[email protected]'
# 发送给多人
# receiver = ['[email protected]','[email protected]','[email protected]']

# 定义标题和内容
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)

猜你喜欢

转载自www.cnblogs.com/5566yesongqiao/p/12030860.html