此方法通用适合所有邮箱的使用,只需注意几个点,如下:
QQ邮箱、其他非QQ邮箱的写法,区别点如下:
#--------------------------使用腾讯企业邮箱作为发件人的操作如下---------------------
smtp_server = smtp.exmail.qq.com
Port = 465
Sender = lucky@iberhk.com
psw = xxxxxx(注:此处写的是发送邮箱的密码)
Receiver = 904199561@qq.com #------------------------------使用QQ邮箱作为发件人的操作如下---------------------
smtp_server = smtp.qq.com
Port = 465
Sender = 904199561@qq.com
psw =xxxxx(注:此处写的是授权码)
Receiver = 904199561@qq.com #######################对如上信息的解释说明########################### smtp_server:无论选择的发件人邮箱是什么格式(如:QQ、163等),查看此参数值写什么,需登录此邮箱,从设置中查找Port :一般都是465,同样是登录此邮箱,从设置中查找Sender :发送的邮箱psw:QQ邮箱:授权码。其他邮箱时:登录发送的邮箱密码Receiver:邮件接收者
完整的代码如下:
#!/usr/bin/env python
# coding=UTF-8 import os,sys
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
from Common.logs import logging
from Config.email import readConfig report_path = os.getcwd()[:-7] + '/Result/Report' + "/" #注:你可以给定一个自己有html的绝对路径。
if sys.getdefaultencoding() != 'utf-8':
reload(sys)
sys.setdefaultencoding('utf-8')
#----------------在此之间写上你的相关配置---------------------
sender = "lucky@iberhk.com"
psw = "xxxxx"
receiver = ['904199561@qq.com']
smtp_server = "smtp.exmail.qq.com"
Port = ""
#----------------------------------------------------------
class email_L: def get_Report_file(self,report_path):
'''
用途:获取最新的API测试报告
参数介绍:
report_path:报告存储的路径
'''
logging.info("获取最新的测试报告")
lists = os.listdir(report_path)
#print lists
lists.sort(key=lambda fn: os.path.getmtime(os.path.join(report_path, fn)))
logging.info(u"最新测试生成的报告:" + lists[-1])
report_file = os.path.join(report_path, lists[-1])
return report_file def send_mail(self,sender, psw, receiver, smtpserver, report_file, port,status):
'''
用途:发送最新的测试报告
参数介绍:
sender:发送者
psw:QQ的授权码
receive:接收者
smtpserver:邮件的格式
report_file:发送的邮件附件
port:邮箱的端口
'''
logging.info("邮件发送最新的API测试报告")
with open(report_file, "rb") as f:
mail_body = f.read() # 定义邮件内容
msg = MIMEMultipart()
body = MIMEText(mail_body, _subtype="html", _charset="utf-8")
msg['subject'] = u"【%s】iBer接口自动化测试报告"%status
msg['from'] = sender
msg['to'] = psw
msg.attach(body) # 添加附件
att = MIMEText(open(report_file, "rb").read(), "base64", "utf-8")
att["Content-Type"] = "application/octet-stream"
att["Content-Disposition"] = 'attachment;filename = "report.html"'
msg.attach(att)
try:
smtp = smtplib.SMTP_SSL(smtpserver, port)
except:
smtp = smtplib.SMTP()
smtp.connect(smtpserver, port) # 用户名和密码
smtp.login(sender, psw)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
logging.info("API测试报告已发送成功 !")
receiver = readConfig.receiver
logging.info("已发送的邮箱: %s" %receiver) def test_run(self,status):
'''如上2个方法的集合整理方法''' report_file = self.get_Report_file(report_path)
# 邮箱配置
# sender = readConfig.sender
# psw = readConfig.psw
# smtp_server = readConfig.smtp_server
# port = readConfig.port
# receiver = readConfig.receiver
self.send_mail(sender, psw, receiver.split(','), smtp_server, report_file, Port,status) # 发送报告 if __name__ == "__main__":
a = email_L()
a.test_run("pass6")
实现结果
QQ邮箱的授权码获取
腾讯企业邮箱获取服务器的端口号等
注:如上完整代码的实现,已经实现了我们测试结果用邮件发送的需求,那么一旦多人协作时,或者考虑到需要将你的接口测试框架让别人来如何更简单的使用呢,那么考虑到如上的问题,最直接的办法就是将配置信息与具体代码分隔,若下次任何人来使用,均需要修改自己的配置邮箱等即可。其他代码不需修改。
详见下篇:
Python+request+ smtplib 测试结果html报告邮件发送(下)