当前位置 : 主页 > 网络安全 > 测试自动化 >

自动化测试基础篇--Selenium发送测试报告邮件

来源:互联网 收集:自由互联 发布时间:2021-06-19
发邮件需要用到python两个模块,smtplib和email,这俩模块是python自带的,只需import即可使用。smtplib模块主要负责发送邮件,email模块主要负责构造邮件。其中MIMEText()定义邮件正文,Heade

 

发邮件需要用到python两个模块,smtplib和email,这俩模块是python自带的,只需import即可使用。smtplib模块主要负责发送邮件,email模块主要负责构造邮件。其中MIMEText()定义邮件正文,Header()定义邮件标题。MIMEMulipart模块构造带附件。

Selenium发送邮件流程:

一、网易邮箱

Selenium发送邮件步骤:

1、导入smtplib和email模块;

2、准备发邮件的参数,每个邮箱的发件服务器都不一样,以163为例,百度搜到发件服务器为:smtp.163.com;

3、接下来就是写邮件的主题和正文内容,正文这里用html格式的;

4、最后调用发件服务。

5、参考代码

import smtplib
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart

# 以yeah邮箱为例
# ----------------发件相关参数----------------
smtpserver = ‘smtp.163.com‘
port = 0
sender = ‘[email protected]‘
password = ‘xxxxxxxxxxxx‘
receicer = ‘[email protected]‘

# ----------------编辑邮件内容----------------
subject = ‘发送邮件测试‘
body = ‘<p>发送邮件测试Test<p>‘
msg = MIMEText(body, ‘html‘, ‘UTF-8‘)
msg[‘from‘] = sender
msg[‘to‘] = receicer
msg[‘subject‘] = subject

# ------------------发送邮件-----------------
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(sender, password)
smtp.sendmail(sender, receicer, msg.as_string())
smtp.quit()

二、腾讯邮箱

Selenium发送邮件步骤:

1、导入smtplib和email模块;

2、腾讯邮箱是需要SSL认证的,找到QQ邮箱授权码,打开QQ邮箱-设置-账号-POP3开启服务-开启;

3、发验证短信获取授权码,照着提示发个短信,如何点我已发送,就会收到授权码;

4、收到授权码后复制,保存下来,这个就可以当QQ邮箱的密码;

5、接下来就是写邮件的主题和正文内容,正文这里用html格式的;

6、最后调用发件服务。

7、参考代码

import smtplib
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
# 以QQ邮箱为例
# ----------------发件相关参数----------------
smtpserver = ‘smtp.qq.com‘
port = 0
sender = ‘[email protected]‘
password = ‘授权码‘
receicer = ‘[email protected]‘

# ----------------编辑邮件内容----------------
subject = ‘发送邮件测试‘
body = ‘<p>发送邮件测试Test<p>‘
msg = MIMEText(body, ‘html‘, ‘UTF-8‘)
msg[‘from‘] = sender
msg[‘to‘] = receicer
msg[‘subject‘] = subject

# ------------------发送邮件-----------------
smtp = smtplib.SMTP_SSL(smtpserver,port)
smtp.login(sender, password)
smtp.sendmail(sender, receicer, msg.as_string())
smtp.quit()

三、同时兼容网易类和腾讯类邮箱

四、多个收件人

1、把receiver参数改成list对象,单个多个都是可以收到的;

2、msg["to"]这个参数不能用list了,得先把receiver参数转化成字符串。

五、发送附件

六、参考代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author  : chen
# @File    : c.py
# @Software: PyCharm
import smtplib
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
# 同时兼容网易类和腾讯类邮箱
# ----------------发件相关参数----------------
smtpserver = ‘smtp.qq.com‘
port = 0
sender = ‘[email protected]‘
password = ‘授权码‘
receicer = [‘[email protected]‘,‘[email protected]‘,]

# ----------------编辑邮件内容----------------
subject = ‘发送邮件测试‘
body = ‘<p>发送邮件测试Test<p>‘
msg = MIMEText(body, ‘html‘, ‘UTF-8‘)
msg[‘from‘] = sender
msg[‘to‘] = ‘;‘.join(receicer)
msg[‘subject‘] = subject

# 文字部分
part = MIMEText(‘TEST!!!‘)
msg.attach(part)
# 附件部分
#---xlsx类型附件---
part = MIMEApplication(open(‘D:\\test.xlsx‘,‘rb‘).read())
part.add_header(‘Content-Disposition‘, ‘attachment‘, filename="test.xlsx")
msg.attach(part)
# jpg类型附件(png类型和jpg一样)
part = MIMEApplication(open(‘D:\\test.jpg‘,‘rb‘).read())
part.add_header(‘Content-Disposition‘, ‘attachment‘, filename="test.jpg")
msg.attach(part)
# pdf类型附件
part = MIMEApplication(open(‘D:\\test.pdf‘,‘rb‘).read())
part.add_header(‘Content-Disposition‘, ‘attachment‘, filename="test.pdf")
msg.attach(part)
# mp3类型附件
part = MIMEApplication(open(‘D:\\test.mp3‘,‘rb‘).read())
part.add_header(‘Content-Disposition‘, ‘attachment‘, filename="test.mp3")
msg.attach(part)
# html类型
part = MIMEText(‘<html><h1>test!</h1></html>‘,‘html‘,‘utf-8‘)
msg.attach(part)

# ------------------发送邮件-----------------
try:
    smtp = smtplib.SMTP()
    smtp.connect(smtpserver)
    smtp.login(sender, password)
except:
    smtp = smtplib.SMTP_SSL(smtpserver,port)
    smtp.login(sender, password)
smtp.sendmail(sender, receicer, msg.as_string())
smtp.quit()
网友评论