导入模块:
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
代码示例:
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
import os
#定义文件目录
result_dir='C:\\Users\\lgf\\Desktop\\webtest\\sell\\html_test'
lists=os.listdir(result_dir)
#重新按时间对目录下的文件进行排序
lists.sort(key=lambda fn:os.path.getmtime(result_dir+"\\"+fn))
print(('最新的文件为'+lists[-1]))
file=os.path.join(result_dir,lists[-1])
print(file)
#发送邮箱服务器
smtpserver = 'smtp.ym.163.com'
#发送邮箱用户/密码
user = '用户邮箱号'
password = '邮箱密码'
#发送邮箱
sender = '发送邮箱号'
#接收邮箱
receiver = '接收邮箱号'
#发送邮件主题
subject = 'python email test'
#发送附件
sendfile = open('C:\\Users\\lgf\\Desktop\\webtest\\sell\\html_test\\result.html','rb').read()
#定义附件
att =MIMEText(sendfile,'base64','utf-8')
att["Conetent-Type"]='application/octet-stream'
att["Content-Disposition"] = 'attachment;filename="result.html"'
#编写HTML类型邮件正文
msg = MIMEText('<html><hl>你好!</hl></html>','html','utf-8')
msgRoot = MIMEMultipart('related')
#定义主题
msgRoot['Subject']=subject
msgRoot.attach(att)
msgRoot.attach(msg)
#连接发送邮箱
smtp=smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(user,password)
#smtp.sendmail(sender,receiver,msg.as_string())
smtp.sendmail(sender,receiver,msgRoot.as_string())
smtp.quit()
整合发送邮件功能
示例:
from HTMLTestRunner import HTMLTestRunner
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
import smtplib
import os
import unittest
import time
#===================定义发送邮件=================
def send_mail(file_new):
#邮件正文内容
f = open(file_new,'rb')
mail_body = f.read()
f.close()
#定义邮件标题、邮件正文
msg =MIMEText(mail_body,'html','utf-8')
#msg['Subject']=Header("自动化测试报告",'utf-8')
subject = '自动化测试报告'
#定义附件
att =MIMEText(mail_body,'base64','utf-8')
att["Conetent-Type"]='application/octet-stream'
att["Content-Disposition"] = 'attachment;filename="result.html"'
#定义邮箱附件类型
msgRoot = MIMEMultipart('related')
#定义主题
msgRoot['Subject']=subject
msgRoot.attach(att)
msgRoot.attach(msg)
#连接发送邮箱
smtp = smtplib.SMTP()
#连接邮箱服务器
smtp.connect("smtp.ym.163.com")
#设置邮箱登录用户/账号
smtp.login("邮箱登录用户","账号")
#设置发送邮箱、接收邮箱、发送信息
smtp.sendmail("发送邮箱","接收邮箱",msgRoot.as_string())
#关闭邮箱
smtp.quit()
print('邮件已发送!')
#===================查找测试报告目录,找到最新生成的测试报告文件=================
def new_html(testhtml):
#os.listdir可以获取目录下的所有文件及文件夹
lists = os.listdir(testhtml)
#重新按时间对目录下的文件进行排序,sort()方法对目录下文件及文件夹按时间重新排序
lists.sort(key=lambda fn:os.path.getmtime(testhtml+"\\"+fn))
#lists[-1]取最新生成的文件或文件夹
file_new=os.path.join(testhtml,lists[-1])
print("测试报告地址:",file_new)
return file_new
if __name__=='__main__':
#指定测试用例存放目录
test_dir='C:\\Users\\lgf\\Desktop\\webtest\\sell\\test_case'
#测试报告存放目录
test_html='C:\\Users\\lgf\\Desktop\\webtest\\sell\\html_test'
#执行测试用例文件
discover =unittest.defaultTestLoader.discover(test_dir,pattern='TEST_*.py')
now = time.strftime("%Y-%m-%d %H-%M-%S")#打印当前时间
#定义报告存放路径与文件名
filename = test_html +"\\"+ now + 'result.html'
fp = open(filename,'wb')
#定义测试报告,stream指定测试报告文件,title定义标题,description='用例执行情况:'
runner=HTMLTestRunner(stream=fp,title='售卖系统测试报告',description='用例执行情况:')
runner.run(discover)#运行测试用例
fp.close()#关闭报告文件
new_html = new_html(test_html)
send_mail(new_html)#发送测试报告