Selenium实战(七)——自动发送邮件

SMPT(Simple Mail Transfer Protocol)简单邮件传输协议,是一组由源地址到目的地址传送邮件的规则,可以控制信件的中转方式。Python的smptlib模块提供了简单的API用来实现发送邮件的功能,它对SMPT进行了简单的封装。

一、python自带的发送邮件功能

1、发送邮件正文

 1 import smtplib
 2 from email.mime.text import MIMEText
 3 from email.header import Header
 4 
 5 # 发送邮件主题
 6 subject = 'hello my dear;'
 7 
 8 # 编写HTML类型的邮件正文
 9 msg = MIMEText('<html><h1>helloMMAMAMAAMAM</h1></html>', 'html', 'utf-8')
10 msg['Subject'] = Header(subject, 'utf-8')
11 msg['from'] = 'zudangli@126.com'
12 msg['to'] = 'zudangli@126.com'
13 
14 # 发送邮件
15 smtp = smtplib.SMTP()
16 smtp.connect("smtp.126.com")
17 smtp.login("zudangli@126.com", "19970507zudangli")
18 smtp.sendmail("zudangli@126.com", "zudangli@126.com", msg.as_string())
19 smtp.quit()

 email模块下面的MIMEText类,定义发送邮件的正文、格式,以及编码,Header类定义邮件的主题和编码类型

smptlib模块用于发送邮件的。connect()方法指定连接的邮箱服务;login()方法指定登录邮箱的账号和密码;sendmail()方法指定发件人、收件人,以及邮件的正文;quit()方法用于关闭邮件服务器的连接。

 2、发送带附件的邮件

 1 import smtplib
 2 from email.mime.text import MIMEText
 3 from email.mime.multipart import MIMEMultipart
 4 
 5 # 邮件主题
 6 subject = 'python带附件的发送邮件'
 7 # 发送的附件
 8 with open('test.txt', 'rb') as f:
 9     send_att = f.read()
10 
11 att = MIMEText(send_att, 'text', 'utf-8')
12 att["Content-Type"] = 'application/octet-stream'
13 att["Content-Disposition"] = 'attachment; filename="I am attachment.txt"'
14 
15 
16 msg = MIMEMultipart()
17 msg['Subject'] = subject
18 msg['from'] = 'zudangli@126.com'
19 msg['to'] = 'zudangli@126.com'
20 msg.attach(att)
21 
22 # 发送邮件
23 smtp = smtplib.SMTP()
24 smtp.connect("smtp.126.com")
25 smtp.login("zudangli@126.com", "19970507zudangli")
26 smtp.sendmail("zudangli@126.com", "zudangli@126.com", msg.as_string())
27 smtp.quit()

   首先,读取附件的内容。通过MIMEText类,定义发送邮件的正文、格式,以及编码;

  • Content-Type指定附件内容类型;
  • application/octet-stream表示二进制流;
  • Content-Disposition指定显示附件的文件

  然后,使用MIMEMultipart类定义邮件的主题,attach()指定附件信息。

  最后,通过smtplib模块发送邮件。

 二、用yagmail发送邮件

  yagmail是Python的一个第三方库,GitHub项目地址:https://github.com/kootenpv/yagmail

安装命令:pip install yagmail

 1 import yagmail
 2 
 3 # 连接邮箱服务器
 4 yag = yagmail.SMTP(user="zudangli@126.com", password="19970507zudangli", host="smtp.126.com")
 5 
 6 # 邮件正文
 7 contents = ['This is the body,and here is just text http://somedomain/image.png', 'You can find an audio file attached.']
 8 
 9 # 发送邮件
10 yag.send('zudangli@126.com', 'subject', contents)

 

 

 如果想给多个用户发送邮件,只需要把收件人放到一个list中即可

yag.send(['pegawayatstudying@126.com', '1271916637@qq.com', 'zudangli@126.com'], 'subject', contents)

 如果想发送带附件的邮件,只需要指定本地附件的路径即可。

1 yag.send(['pegawayatstudying@126.com', '1271916637@qq.com', 'zudangli@126.com'], 'subject', contents, ["C://Users//zudl//Pictures//timg.jpg","F://秒通OApython//formal.py"])

三、整合自动发送邮件

 1 import time
 2 import unittest
 3 import yagmail
 4 
 5 from HTMLTestRunner import HTMLTestRunner
 6 
 7 
 8 # 把测试报告作为附件发送到指定邮箱
 9 def send_mail(report):
10     yag = yagmail.SMTP(user="zudangli@126.com",
11                        password="19970507zudangli",
12                        host='smtp.126.com')
13     subject = "主题,自动化测试报告"
14     contents = "正文,请查看附件"
15     yag.send(['1271916637@qq.com', 'pegawayatstudying@126.com', 'zudangli@126.com'], subject, contents, report)
16     print('email has send out !')
17 
18 
19 if __name__ == '__main__':
20     # 定义测试用例的目录为当前目录中的unit_test
21     test_dir = './'
22     suits = unittest.defaultTestLoader.discover(test_dir, pattern='test*.py')
23     # 取当前日期时间
24     now_time = time.strftime("%Y-%m-%d %H_%M_%S")
25     html_report = './' + now_time + 'result.html'
26     # 生成HTML格式的测试报告
27     fp = open('./' + now_time + 'result.html', 'wb')
28     runner = HTMLTestRunner(stream=fp,
29                             title="百度搜索测试报告",
30                             description="运行环境:Windows 10,Chrome浏览器")
31     runner.run(suits)
32     fp.close()
33     send_mail(html_report)

 

整个程序的执行过程分为两部分:

(1)定义测试报告文件,并赋值给变量html_report,通过HTMLTestRunner运行测试用例,将结果写入文件后关闭。

(2)调用send_mail()函数,并传入html_report文件。在send_mail()函数中,把测试报告作为邮件的附件发送到指定邮箱。

AS a side Note: 不直接把测试报告的内容读取出来作为邮件的正文发送的原因:HTMLTeatRunner报告在展示时引用了Bootstrap样式库,当作为邮件正文“写死”在邮件中时,会导致样式丢失。

 

posted @ 2020-02-26 17:57  pegawayatstudying  阅读(649)  评论(0编辑  收藏  举报