python selenium-7自动发送邮件
https://jingyan.baidu.com/article/647f0115b78f8d7f2148a8e8.html
1.发送HTML格式的邮件
import smtplib
from email.mime.text import MIMEText
from email.header import Header
#发送邮箱服务器
smtpserver = "smtp.126.com"
#发送邮箱用户/密码
user = "发送邮箱@126.com"
password = "登陆密码"
#发送邮箱
sender = "发送邮箱@126.com"
#接收邮箱
receiver = "接收邮箱@qq.com"
#发送邮箱主题
subject = "python发送邮件"
#编写HTML类型的邮箱正文
msg=MIMEText('<html><h1>给QQ邮箱发送邮件</h1></html>','html','utf-8')
msg['Subject']=Header(subject,'utf-8')
msg['From']="张三"
msg['To']=receiver
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(user,password)
try:
smtp.sendmail(sender, receiver, msg.as_string())
print("发送成功")
except smtplib.SMTPDataError as e:
print("发送失败")
finally:
smtp.quit()
2.发送文本内容的邮件
import smtplib
from email.mime.text import MIMEText
from email.header import Header
msg_from = '发送邮箱@qq.com'
passwd = '授权码'
msg_to = '收件人邮箱@126.com'
subject = "python邮件测试"
content = "这是我使用python smtplib及email模块发送的邮件"
msg = MIMEText(content)
msg['Subject'] = subject
msg['From'] = "发送方"
msg['To'] = "接收方"
try:
s = smtplib.SMTP_SSL("smtp.qq.com", 465)
s.login(msg_from, passwd)
s.sendmail(msg_from, msg_to, msg.as_string())
print("发送成功")
except s.SMTPDataError as e:
print("发送失败")
finally:
s.quit()
其中 s = smtplib.SMTP_SSL("smtp.qq.com", 465)#相当于以下2行
s = smtplib.SMTP()
s.connect("smtp.qq.com")
3.发送带附件的邮件
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
smtpserver = "smtp.126.com"
sender = "发送邮箱@126.com"
receiver = "接收邮箱@qq.com"
user = "发送邮箱@126.com"
password = "发送邮箱密码"
subject = "测试附件3"
sendfile = open("/Users/chenshanju/PycharmProjects/SeleniumOfJenkins/report/log.txt","rb").read()
att = MIMEText(sendfile,"base64","utf-8")
att["Content-Type"] = "application/octet-stream"
att["Content-Disposition"] ="attachment;filename='log.txt'"
msgRoot = MIMEMultipart("related")
msgRoot["Subject"] = subject
msgRoot["From"] = sender
msgRoot["To"] = receiver
msgRoot.attach(att)
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(user,password)
smtp.sendmail(sender,receiver,msgRoot.as_string())
smtp.quit()
4.查找最新的测试报告
import sys,os
#获取当前脚本的目录
script_path=sys.path[0].split("/")
#获取工程的主目录
end_index=script_path.index("SeleniumOfJenkins")+1
#report所在的目录
report_dir="/".join(script_path[:end_index])+"/report"
#查看当前的文件生成列表
list_file=os.listdir(report_dir)
#对文件按照创建时间进行排序
list_file.sort(key=lambda file:os.path.getmtime(report_dir+"/"+file))
print(list_file[-1])
5.自动发送测试报告
import unittest,os,sys
from time import strftime
from HTMLTestRunner import HTMLTestRunner
import smtplib
from email.mime.text import MIMEText
from email.header import Header
def get_new_report(report_dir):
"get_new_report()方法传入一个参数(测试报告所在目录),得到最新的report文件"
file_list = os.listdir(report_dir)
file_list.sort(key=lambda file:os.path.getmtime(report_dir+"/"+file))
return report_dir+"/"+file_list[-1]
def send_email(new_report):
"send_email()接收一个参数(new_report),发送邮件"
f = open(new_report,"rb")
mail_body=f.read()
f.close()
sender = "发送邮箱@126.com"
receiver = "接收邮箱@qq.com"
msg = MIMEText(mail_body,"html","utf-8")
msg['Subject'] = Header("自动化测试报告","utf-8")
msg['From'] = sender
msg['To'] = receiver
smtp = smtplib.SMTP()
smtp.connect("smtp.126.com")
smtp.login("发送邮箱@126.com","发送邮箱密码")
try:
smtp.sendmail(sender,receiver,msg.as_string())
print("发送成功")
except :
print("发送失败")
finally:
smtp.quit()
if __name__=="__main__":
"""1.执行测试 2.获取最新报告 3.发送邮件"""
current_dir_list = sys.path[0].split("/")
index = current_dir_list.index("SeleniumOfJenkins") + 1
if index == len(current_dir_list):
home_dir = "/".join(current_dir_list)
else:
home_dir = "/".join(current_dir_list[:index])
report_dir = home_dir + "/" + "report"
test_dir = home_dir + "/testcase/testsearch"
discover = unittest.defaultTestLoader.discover(test_dir, pattern="test*.py")
filename="report/"+strftime("%Y_%m_%d_%H_%M_%S")+"_result.html"
fp = open(filename,"wb")
runner=HTMLTestRunner(stream=fp,title="百度搜索报告",description="用例执行情况")
runner.run(discover)
fp.close()
new_report = get_new_report(report_dir)
send_email(new_report)
FAQ:
1.qq邮箱需要开启imap和smtp权限

## 2.从126邮箱发送qq邮箱抛出异常smtplib.SMTPDataError
smtplib.SMTPDataError: (554, b'DT:SPM 126 smtp1,C8mowABHGtEduABcQk94BA--.46414S2 1543551006,please see http://mail.163.com/help/help_spam_16.htm?ip=125.122.53.172&hostid=smtp1&time=1543551006')
解决方法:需要对msg['From']和msg['To']赋值
## 3.发送的附件不是指定的文件名,而是tcmime.*.bin类型的文件
原因:att["Content-Disposition"] ="attachment;filename='log.txt'"误写为Content-Dispostion
## 4.自动发送测试报告中,qq邮箱样式丢失,126邮箱正常

使用附件可以解决这个问题
```#python
import smtplib,os,sys
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def get_home_dir():
"获取最新的报告"
current_path_list=sys.path[0].split("/")
index = current_path_list.index("SeleniumOfJenkins")+1
if index == len(current_path_list):
home_path=sys.path[0]
else:
home_path="/".join(current_path_list[:index])
return home_path
def get_new_file():
"获取最新的报告"
file_list=os.listdir(report_path)
file_list.sort(key=lambda file:os.path.getmtime(report_path+file))
return file_list[-1]
def send_email():
"发送邮件"
new_file_name = get_new_file()
new_file = report_path + new_file_name
server = "smtp.126.com"
sender="发送邮箱@126.com"
receiver="接收邮箱@qq.com"
subject = "测试报告见附件3"
sendfile= open(new_file,"rb").read()
att = MIMEText(sendfile,"base64","utf-8")
att['Content-Type'] = "application/octet-stream"
att['Content-Disposition'] = "attachment;filename='%s'" % new_file_name
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = subject
msgRoot['From'] = sender
msgRoot['To'] = receiver
msgRoot.attach(att)
smtp = smtplib.SMTP()
try:
smtp.connect(server)
smtp.login("发送邮箱@126.com","发送邮箱密码")
smtp.sendmail(sender,receiver,msgRoot.as_string())
print("发送成功")
except:
print("发送失败")
finally:
smtp.quit()
if name=="main":
home_path = get_home_dir()
report_path = home_path + "/report/"
send_email()
python selenium-7自动发送邮件的更多相关文章
- 开源you-get项目爬虫,以及基于python+selenium的自动测试利器
写在前面 爬虫和自动测试,对于python来说是最合适不过也是最擅长的. 开源的项目也很多,例如you-get项目https://github.com/soimort/you-get.盗链和爬虫神器. ...
- Python+Selenium学习--自动生成HTML测试报告
前言 在脚本运行完成之后,除了在log.txt 文件看到运行日志外,我们更希望能生一张漂亮的测试报告来展示用例执行的结果. HTMLTestRunner 是Python 标准库的unit ...
- Python+selenium整合自动发邮件功能
主要实现的目的是:自动将测试报告以邮件的形式通知相关人员 from HTMLTestRunner import HTMLTestRunner import HTMLTestReport from em ...
- 批量群发,营销必备!Python代码实现自动发送邮件!
在运维开发中,使用 Python 发送邮件是一个非常常见的应用场景.今天一起来探讨一下,GitHub 的大牛门是如何使用 Python 封装发送邮件代码的. 一般发邮件方法 SMTP是发送邮件的协议, ...
- Python selenium 文件自动下载 (自动下载器)
MyGithub:https://github.com/williamzxl 最新代码已经上传到Github,以下版本为stupid版本. 由于在下载过程中需要下载不同文件,所以可以把所有类型放在Va ...
- python+selenium实现自动抢票
使用说明 程序运行开始,需要输入出发地,目的地,出发时间,乘客信息,车次:乘客信息和车次可以输入多个 刚刚开始学习爬虫,selenium仅仅是解放了双手,运行效率不是很高: 程序运行时会打开chrom ...
- python selenium 练习 自动获取豆瓣阅读当前特价书籍 chrome 元素定位 窗口切换 元素过期
豆瓣原创电子书每周推出数十本限时免费数目,一周免费期过后恢复原价.想着豆瓣原创书中有不少值得一看,便写了个脚本,免去一个个添加的烦恼. 使用了Windows下selenium+Python的组合,有较 ...
- python QQ邮箱自动发送邮件
于初学者来讲在写发送邮件代码时常见的错误有SMTPAuthenticationError535,有点懵逼,检查用户名,密码正确就是报错, 想当年笔者也是这么过来的,现在就给大家分享一下个人经验: 一, ...
- python+selenium之自动生成excle,保存到指定的目录下
进行之自动化测试,想把自动生成的excle保存到指定的目录下.网上百度的代码如下: import xlwt import time time = time.strftime ('%Y%m%d%H%M% ...
- 用Python自动发送邮件
用Python自动发送邮件 最近需要在服务器上处理一些耗时比较长的任务,因此想到利用python写一个自动发送邮件的脚本,在任务执行完毕后发送邮件通知我.以下代码以163邮箱为例: 开通163 ...
随机推荐
- 201621123010《Java程序设计》第10周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 1. 常用异常 结合题集题目7-1回答 1.1 自己以前编写的代码中经常出现 ...
- Win10玩游戏时听歌音量忽大忽小
问题原因是你的声卡被识别成了5.1声道,解决方法: 1.右键桌面右下角小喇叭选择“声音” 2.右键当前的播放设备选择“配置扬声器” 3.选择“立体声”,可以测试一下,然后点击下一步退出,可能会中断当前 ...
- node 常见的一些系统问题
nodde正风生火起,很多介绍却停留在入门阶段,无法投入生产 许多文章在讲第三方类库,可是这些库质量差距较大,一旦遇到问题怎么办 全面了解node核心才能成为一名合格的node开发人员 1. node ...
- arpg网页游戏特效播放(一)
网页游戏中的特效,主要包括:场景特效,攻击特效和UI特效三种.场景特效是在地图层上播放的特效,攻击特效主要是技能触发的一些特效,UI特效是面板上的一些特效,还有一些在人物身上播放的特效,例如脚底光圈特 ...
- CF1093:E. Intersection of Permutations(树状数组套主席树)
题意:给定长度为N的a数组,和b数组,a和b都是1到N的排列: 有两种操作,一种是询问[L1,R1],[L2,R2]:即问a数组的[L1,R1]区间和b数组的[L2,R2]区间出现了多少个相同的数字. ...
- BZOJ3771: Triple【生成函数】
Description 我们讲一个悲伤的故事. 从前有一个贫穷的樵夫在河边砍柴. 这时候河里出现了一个水神,夺过了他的斧头,说: "这把斧头,是不是你的?" 樵夫一看:" ...
- hdu 5184 类卡特兰数+逆元
BC # 32 1003 题意:定义了括号的合法排列方式,给出一个排列的前一段,问能组成多少种合法的排列. 这道题和鹏神研究卡特兰数的推导和在这题中的结论式的推导: 首先就是如何理解从题意演变到卡特兰 ...
- ES6 — 箭头函数
一 为什么要有箭头函数 我们在日常开发中,可能会需要写类似下面的代码 const Person = { 'name': 'little bear', 'age': 18, 'sayHello': fu ...
- Linux配置NTP服务器,时间同步
当服务器多了,时间准确与否,一致与否是个大问题.虽然这个问题总是被忽略,但是统一一致的时间是很有必要的.下面说一下在局域网内配置Linux时间服务器的方法. 配置的环境及要求: 假设在192.168. ...
- 【转】每天一个linux命令(28):tar命令
原文网址:http://www.cnblogs.com/peida/archive/2012/11/30/2795656.html 通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候 ...