Python 邮件发送
SMTP(Simple Mail Transfer Protocol)
- 即简单邮件传输协议。它是一组用于从源地址到目的地址传输邮件的规范,通过它来控制邮件的中转方式。SMTP 协议属于TCP/IP协议簇,它帮助每台计算机在发送或中转信件时找到下一个目的地。SMTP 服务器就是遵循 SMTP 协议的发送邮件服务器。
- SMTP 认证,简单地说就是要求必须在提供了账户名和密码之后才可以登录 SMTP 服务器,这就使得那些垃圾邮件的散播者无可乘之机。
- 增加 SMTP 认证的目的是为了使用户避免受到垃圾邮件的侵扰。
SMTP 认证
更多资料: http://help.163.com/09/1223/14/5R7P6CJ600753VB8.html
smtplib模块
Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件。
Python对SMTP支持有smtplib和email两个模块,email负责构造邮件,smtplib负责发送邮件。
注意:使用前需要开启SMTP服务
案例:使用qq邮箱来结合smtp模块发送邮件 准备工作:
#/usr/bin python #-*- coding:UTF-8 -*- ##发送邮件模块 import smtplib ##定义邮件内容 from email.mime.text import MIMEText ##定义邮件头 from email.header import Header #发送邮箱服务器 smtpserver = 'smtp.qq.com' #发送邮箱用户名密码(客户端授权密码) user = '??@qq.com' passwd = ' ' #发送邮箱和接收邮箱 send = user receive = '??@foxmail.com' #发送邮件主题和内容 subject='Web Selenium 自动化测试报告' content='<html><h1 style="color:red">这是我的第一个自动化测试报告!</h1></html>' #HTML邮件正文(装载邮件标题和内容) msg字典类型 msg = MIMEText(content,'html','utf-8') msg['Subject'] = Header(subject,'utf-8') msg['From'] = send msg['To'] = receive #SSL协议端口号要使用465 smtp = smtplib.SMTP_SSL(smtpserver,465) #HELO 向服务器标识用户身份 smtp.helo(smtpserver) #服务器返回结果确认 smtp.ehlo(smtpserver) #登录邮箱服务器用户名和密码 smtp.login(user,passwd) print("开始发送邮件...") smtp.sendmail(send,receive,msg.as_string()) smtp.quit() print("邮件发送完成!")
带附件发送给多人
#/usr/bin python #-*- coding:UTF-8 -*- ##发送邮件模块 import smtplib ##定义邮件内容 from email.mime.text import MIMEText ##定义邮件头 from email.header import Header #用于传送附件 from email.mime.multipart import MIMEMultipart #发送邮箱服务器 smtpserver = 'smtp.qq.com' # #接收邮箱服务器 # popserver = 'pop.sunline.cn' #发送邮箱用户名密码(客户端授权密码) user = '??@qq.com' passwd = ' ' #发送邮箱和接收邮箱 send = user receive = ['??@foxmail.com','??@qq.com'] #发送邮件主题和内容 subject='Web Selenium 自动化测试报告' content='<html><h1 style="color:red">自动化测试报告发送给多人!</h1>' \ '</html>' #构造附件内容 send_file=open(r"E:\project\Python\p.jpg",'rb').read() att=MIMEText(send_file,'base64','utf-8') att["Content-Type"]='application/octet-stream' att["Content-Disposition"]='attachment;filename="p.jpg"' # #HTML邮件正文(装在邮件标题和内容) # msg = MIMEText(content,'html','utf-8') # msg['Subject'] = Header(subject,'utf-8') # msg['From'] = send # msg['To'] = receive #构建发送与接收信息 msgRoot=MIMEMultipart() msgRoot.attach(MIMEText(content, 'html', 'utf-8')) msgRoot['subject']=subject msgRoot['From']=send
# 以,为分割,将receive 中的值进行添加 msgRoot['To'] = ','.join(receive) msgRoot.attach(att) #SSL协议端口号要使用465 smtp = smtplib.SMTP_SSL(smtpserver,465) #HELO 向服务器标识用户身份 smtp.helo(smtpserver) #服务器返回结果确认 smtp.ehlo(smtpserver) #登录邮箱服务器用户名和密码 smtp.login(user,passwd) print("开始发送邮件...")
#发邮件函数,发送者,接收者,发送内容主题, smtp.sendmail(send,receive,msgRoot.as_string()) smtp.quit() print("邮件发送完成!")
案例:获取…\Test_Baidu\test_report目录下最新的测试报告
#/usr/bin python #-*- coding:UTF-8 -*- #用于访问操作系统功能的模块 import os #报告存放位置 report_dir='../../unittest/百度测试案例/test_reports' #os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表 lists=os.listdir(report_dir) #按时间顺序对该目录文件夹下面的文件进行排序 lists.sort(key=lambda fn:os.path.getatime(report_dir+'\\'+fn)) print(lists)
# -1 代表列表里面的最后一个 print("latest report is :"+lists[-1]) #输出最新报告的路径 file=os.path.join(report_dir,lists[-1]) print(file) print(os.getcwd())
Python os模块相关知识: http://www.cnblogs.com/MnCu8261/p/5483657.html
lambda 介绍 https://www.cnblogs.com/fanpl/articles/9244104.html
案例:将E:\project\Python\unittest\百度测试案例\test_reports 生成的最新测试报告发送到指定邮箱import unittestfrom HTMLTestRunner_PY3 import *
import time #发送邮件模块 import smtplib #定义邮件内容 from email.mime.text import MIMEText #定义邮件标题 from email.header import Header import os
########发送邮件 def send_mail(latest_report):
# 将报告内容读取,放入 mail_content 变量中 f=open(latest_report,'rb') mail_content=f.read() f.close() smtpserver='smtp.qq.com' # 发送邮箱用户名密码 user = ' ' password = ' ' # 发送和接收邮箱 sender = user receives = [' '] # 发送邮件主题和内容 subject = 'Web Selenium 自动化测试报告' # HTML邮件正文 msg = MIMEText(mail_content, 'html', 'utf-8') msg['Subject'] = Header(subject, 'utf-8') msg['From'] = sender msg['To'] = ','.join(receives) smtp = smtplib.SMTP_SSL(smtpserver, 465) # HELO 向服务器标识用户身份 smtp.helo(smtpserver) # 服务器返回结果确认 smtp.ehlo(smtpserver) # 登录邮箱服务器用户名和密码 smtp.login(user, password) print("Start send Email...") smtp.sendmail(sender, receives, msg.as_string()) smtp.quit() print("Send Email end!")
######查找测试报告 def latest_report(report_dir): lists = os.listdir(report_dir) # 按时间顺序对该目录文件夹下面的文件进行排序 lists.sort(key=lambda fn: os.path.getatime(report_dir + '\\' + fn)) print(("new report is :" + lists[-1])) file = os.path.join(report_dir, lists[-1]) print(file) return file if __name__ == '__main__':
#用例路径 test_dir='../../unittest/百度测试案例/'
#报告路径
report_dir='../../unittest/百度测试案例/test_reports'
#装载和执行测试案例 discover = unittest.defaultTestLoader.discover(test_dir, pattern="web_baidu.py") now = time.strftime("%Y-%m-%d %H_%M_%S") report_name = report_dir + '/' + now + 'result.html' with open(report_name, 'wb') as f: runner=HTMLTestRunner(stream=f,title="Test Report",description='Test case result') runner.run(discover) f.close() #h获取最新测试报告 latest_report=latest_report(report_dir) #发送邮件报告 send_mail(latest_report)
邮箱发生失败的常见问题:
http://help.163.com/09/1224/17/5RAJ4LMH00753VB8.html
 
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号