Python单元测试框架unittest知识<5>

121 阅读1分钟
原文链接: zhuanlan.zhihu.com

使用Python脚本发送测试报告

Python也可以很方便地写出发送邮件的代码,并且比JAVA更加简介。

案例:利用python发送电子邮件

#!/usr/bin/envpython

#coding:utf-8

importsmtplib

fromemail.mime.textimportMIMEText

fromemail.mime.multipartimportMIMEMultipart

#发送邮箱服务器

smtpserver='smtp.126.com'

#发送邮箱

sender='tomtang@126.com'

#接受邮箱

receiver='tomtang@126.com'

#发送邮箱用户名、密码

username='tomtang@126.com'

password='123456'

#邮件主题

subject='Pythonsendemail'

#发送的附件,即测试报告

sendfile=open('result.html','rb').read()

att=MIMEText(sendfile,'base64','utf-8')

att["content-Type"]='application/octest-stream'

att["content-Disposition"]='attachment;filename="result.html"'

msgRoot=MIMEMultipart('related')

msgRoot['Subject']=subject

msgRoot.attach(att)

smtp=smtplib.SMTP()

smtp.connect(smtpserver)

smtp.login(username,password)

smtp.sendmail(sender,receiver,msgRoot.as_string())

smtp.quit()