1、smtplib库
import os,smtplib,os.path,time
from configimport global_parameteras gl
from email.mime.multipartimport MIMEMultipart
from email.mime.textimport MIMEText
class send_email(object):
# 定义邮件内容 def email_init(self,report,reportName):
with open(report,'rb')as f:
mail_body = f.read()
# 创建一个带附件的邮件实例 msg = MIMEMultipart()
# 以测试报告作为邮件正文 msg.attach(MIMEText(mail_body,'html','utf-8'))
report_file = MIMEText(mail_body,'html','utf-8')
# 定义附件名称(附件的名称可以随便定义,你写的是什么邮件里面显示的就是什么) report_file["Content-Disposition"] ='attachment;filename='+reportName
# 定义发送时间(不定义的可能有的邮件客户端会不显示发送时间) msg['date'] = time.strftime('%a, %d %b %Y %H:%M:%S %z')
msg.attach(report_file)# 添加附件 msg['Subject'] ='好易借_APP自动化测试报告:'+reportName# 邮件标题 msg['From'] = gl.email_name#发件人 msg['To'] = gl.email_To#收件人列表 try:
server = smtplib.SMTP_SSL(gl.smtp_sever,gl.smtp_port)
server.login(gl.email_name,gl.email_password)
server.sendmail(msg['From'],msg['To'].split(';'),msg.as_string())
server.quit()
except smtplib.SMTPException:
print("邮件发送测试报告失败")
#self.mylog.error(u'邮件发送测试报告失败 at'+__file__) def sendReport(self):
# 找到最新的测试报告 report_list = os.listdir(gl.report_path)
report_list.sort(key=lambda fn: os.path.getmtime(gl.report_path+fn)if not os.path.isdir(gl.report_path+fn)else 0)
new_report = os.path.join(gl.report_path,report_list[-1])
# 发送邮件 self.email_init(new_report,report_list[-1])
if __name__ =='__main__':
se = send_email()
se.sendReport()
2、zmail模块
# -*- coding: utf-8 -*-
import zmail, os
from configimport global_parameteras gl
report_list = os.listdir(gl.report_path)
report_list.sort(key=lambda fn: os.path.getmtime(gl.report_path+fn)if not os.path.isdir(gl.report_path+fn)else 0)
new_report = os.path.join(gl.report_path, report_list[-1])
mail_content = {
'subject':"APP自动化测试报告",
'content_text':'来自zamil的来信',
'attachments': new_report
}
server = zmail.server(gl.email_name, gl.email_password)
server.send_mail(gl.email_To, mail_content)
网友评论