Python+Selenium邮件发送

Python+Selenium邮件发送

PHILOSOPHY

为什么要用邮件:

当测试完生成测试报告后,是不是需要让程序给我们发送一下测试结果,这样不管我们在哪,随时都可以看到测试结果;我们直接上例子,在例子中讲解python邮件模块;

2.邮件实例程序

#coding=utf-8

'''

MIME对象集成:

MIMEBase

-- MIMENonMultipart

-- MIMEApplication

-- MIMEAudio

-- MIMEImage

-- MIMEMessage

-- MIMEText

-- MIMEMultipart

'''

importsmtplib

fromemail.mime.textimportMIMEText

fromemail.headerimportHeader

fromemail.mime.applicationimportMIMEApplication

fromemail.mime.multipartimportMIMEMultipart

'''

1.由于QQ邮箱安全级别比较高,所以邮箱登录不能用QQ密码,

只能用授权码(详见文章下方授权码获取方式)

2.QQ邮箱服务器:smtp.qq.com

3.邮箱端口号:465

'''

smtp_server='smtp.qq.com'

from_mail ="27******89@qq.com"

#服务授权码

mail_pass ="pr******ga"

'''

to_mail收件人可以设置多个,中间以','隔开格式如下:

to_mail="18******48@qq.com,10******72@qq.com"

'''

to_mail ="10******72@qq.com"

msg = MIMEMultipart()

msg["From"]=from_mail

msg["To"]=to_mail

#邮件标题,中文进行转码

msg["Subject"]=Header('自动化测试报告,请查收','utf-8').encode()

'''

文字部分

'''

TextPart = MIMEText(u"邮件发送测试")

msg.attach(TextPart)

'''

附件分为:1.xlsx

2.jpg、png

3.pdf

4.mp3

5......

'''

#xlsx类型附件

XlsxPart = MIMEApplication(open(r'E:\test.xlsx','rb').read())

XlsxPart.add_header('Content-Disposition','attachment',filename="test.xlsx")

msg.attach(XlsxPart)

#jpg类型附件

PicturePart = MIMEApplication(open(r'E:\test.jpg','rb').read())

PicturePart.add_header('Content-Disposition','attachment',filename="test.jpg")

msg.attach(PicturePart)

#pdf类型附件

PdfPart = MIMEApplication(open(r'E:\test.pdf','rb').read())

PdfPart.add_header('Content-Disposition','attachment',filename="test.pdf")

msg.attach(PdfPart)

#mp3类型附件

MP3Part = MIMEApplication(open(r'E:\test.mp3','rb').read())

MP3Part.add_header('Content-Disposition','attachment',filename="test.mp3")

msg.attach(MP3Part)

try:

s =smtplib.SMTP_SSL(smtp_server,465)

s.connect(smtp_server)

s.login(from_mail, mail_pass)

# as_string()把MIMEText对象变成str

s.sendmail(from_mail, to_mail, msg.as_string())

s.quit()

printu'恭喜发送邮件成功!!!!!!'

exceptsmtplib.SMTPExceptionase:

print"Error: %s"% e

3.QQ邮箱授权码获取

1.登录打开QQ邮箱

2.点击设置按钮

3.点击账户

4.下拉,点击开启POP3/SMTP服务

5.获取授权码后,复制粘贴

  • 发表于:
  • 原文链接:http://kuaibao.qq.com/s/20180119G04IEO00?refer=cp_1026

扫码关注云+社区