将自动化测试报告发送至邮箱
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
def sender(self):
#设置邮件服务器
smtpserver = 'smtp.163.com'
#设置邮件服务器端口号
port = 25
#发件人地址
sender = '@163.com'
#密码或授权码
password = ''
#收件人
receivers = '15091835804@163.com;z744277252@163.com;jingying0037@163.com'
#创建邮件对象
mail = MIMEMultipart()
#初始化发件人
mail['from'] = sender
#添加收件人
mail['to'] = receivers
#添加主题
mail['subject'] = '自动化测试报告'
#添加附件
#读取附件
path = 'selenium/ranzhi/report/2021-03-02 16-34-52report.html'
with open(path,'rb') as file:
report = file.read()
#对附件进行编码 base64
attachment = MIMEText(report,'base64','utf-8')
#设置附件的类型
attachment['Content-Type'] = 'application/octet-stream'
#设置附件的处理方式
attachment['Content-Disposition'] = 'attachment;filename=%s'%path.split('/')[-1]
#添加附件
mail.attach(attachment)
#生成邮件正文
content = '''
Dear Jiao,
this is a phone,
Do you want?
Tom Cruse
'''
#对邮件正文进行编码
body = MIMEText(content,'html','utf-8')
#添加正文
mail.attach(body)
#创建SMTP对象
smtp = smtplib.SMTP()
#连接服务器
smtp.connect(smtpserver,port)
#登录服务器
smtp.login(sender,password)
#发送邮件
smtp.sendmail(sender,receivers.split(';'),mail.as_string())
#关闭邮件服务器
smtp.close()
print('邮件发送完成')
|