此方法通用适合所有邮箱的使用,只需注意几个点,如下:

QQ邮箱、其他非QQ邮箱的写法,区别点如下:

#--------------------------使用腾讯企业邮箱作为发件人的操作如下---------------------
smtp_server = smtp.exmail.qq.com
Port = 465
Sender = lucky@iberhk.com
psw = xxxxxx(注:此处写的是发送邮箱的密码)
Receiver = 904199561@qq.com #------------------------------使用QQ邮箱作为发件人的操作如下---------------------
smtp_server = smtp.qq.com
Port = 465
Sender = 904199561@qq.com
psw =xxxxx(注:此处写的是授权码)
Receiver = 904199561@qq.com #######################对如上信息的解释说明########################### smtp_server:无论选择的发件人邮箱是什么格式(如:QQ、163等),查看此参数值写什么,需登录此邮箱,从设置中查找Port :一般都是465,同样是登录此邮箱,从设置中查找Sender :发送的邮箱psw:QQ邮箱:授权码。其他邮箱时:登录发送的邮箱密码Receiver:邮件接收者

完整的代码如下:

#!/usr/bin/env python
# coding=UTF-8 import os,sys
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
from Common.logs import logging
from Config.email import readConfig report_path = os.getcwd()[:-7] + '/Result/Report' + "/" #注:你可以给定一个自己有html的绝对路径。
if sys.getdefaultencoding() != 'utf-8':
reload(sys)
sys.setdefaultencoding('utf-8')

#----------------在此之间写上你的相关配置---------------------
sender = "lucky@iberhk.com"
psw = "xxxxx"
receiver = ['904199561@qq.com']
smtp_server = "smtp.exmail.qq.com"
Port = ""
#----------------------------------------------------------
class email_L: def get_Report_file(self,report_path):
'''
用途:获取最新的API测试报告
参数介绍:
report_path:报告存储的路径
'''
logging.info("获取最新的测试报告")
lists = os.listdir(report_path)
#print lists
lists.sort(key=lambda fn: os.path.getmtime(os.path.join(report_path, fn)))
logging.info(u"最新测试生成的报告:" + lists[-1])
report_file = os.path.join(report_path, lists[-1])
return report_file def send_mail(self,sender, psw, receiver, smtpserver, report_file, port,status):
'''
用途:发送最新的测试报告
参数介绍:
sender:发送者
psw:QQ的授权码
receive:接收者
smtpserver:邮件的格式
report_file:发送的邮件附件
port:邮箱的端口
'''
logging.info("邮件发送最新的API测试报告")
with open(report_file, "rb") as f:
mail_body = f.read() # 定义邮件内容
msg = MIMEMultipart()
body = MIMEText(mail_body, _subtype="html", _charset="utf-8")
msg['subject'] = u"【%s】iBer接口自动化测试报告"%status
msg['from'] = sender
msg['to'] = psw
msg.attach(body) # 添加附件
att = MIMEText(open(report_file, "rb").read(), "base64", "utf-8")
att["Content-Type"] = "application/octet-stream"
att["Content-Disposition"] = 'attachment;filename = "report.html"'
msg.attach(att)
try:
smtp = smtplib.SMTP_SSL(smtpserver, port)
except:
smtp = smtplib.SMTP()
smtp.connect(smtpserver, port) # 用户名和密码
smtp.login(sender, psw)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
logging.info("API测试报告已发送成功 !")
receiver = readConfig.receiver
logging.info("已发送的邮箱: %s" %receiver) def test_run(self,status):
'''如上2个方法的集合整理方法''' report_file = self.get_Report_file(report_path)
# 邮箱配置
# sender = readConfig.sender
# psw = readConfig.psw
# smtp_server = readConfig.smtp_server
# port = readConfig.port
# receiver = readConfig.receiver
self.send_mail(sender, psw, receiver.split(','), smtp_server, report_file, Port,status) # 发送报告 if __name__ == "__main__":
a = email_L()
a.test_run("pass6")

实现结果

QQ邮箱的授权码获取

腾讯企业邮箱获取服务器的端口号等

注:如上完整代码的实现,已经实现了我们测试结果用邮件发送的需求,那么一旦多人协作时,或者考虑到需要将你的接口测试框架让别人来如何更简单的使用呢,那么考虑到如上的问题,最直接的办法就是将配置信息与具体代码分隔,若下次任何人来使用,均需要修改自己的配置邮箱等即可。其他代码不需修改。

详见下篇:

Python+request+ smtplib  测试结果html报告邮件发送(下)

Python+request+ smtplib 测试结果html报告邮件发送(上)《五》的更多相关文章

  1. Python+request+ smtplib 测试结果html报告邮件发送(下)《六》

    目录结构如下: 1.cfg.ini的配置信息写法如下: [email] ;--------------------------使用腾讯企业邮箱作为发件人的操作如下------------------- ...

  2. 【Linux】结合Python 简易实现监控公司网站,邮件发送异常

    背景 由于一些原因,博主负责测试的网站的服务器切换到了香港,切换后出现了多次访问超时的情况 于是主动请缨写一个自动监测的脚本,本来准备完全使用shell来写,后来发现shell发送邮件只能在测试机之间 ...

  3. python初级实战-----关于邮件发送问题

    python发邮件需要掌握两个模块的用法,smtplib和email,这俩模块是python自带的,只需import即可使用.smtplib模块主要负责发送邮件,email模块主要负责构造邮件. sm ...

  4. Python SMTP邮件发送

    SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件. Python对SMTP支持有smtplib和email两个模块: email负责构造邮件 ...

  5. spring各种邮件发送

    参考地址一 参考地址二 参考地址三 参考地址四 Spring邮件抽象层的主要包为org.springframework.mail.它包括了发送电子邮件的主要接口MailSender,和值对象Simpl ...

  6. 使用python的email、smtplib、poplib模块收发邮件

    使用python的email.smtplib.poplib模块收发邮件 一封电子邮件的旅程是: MUA:Mail User Agent——邮件用户代理.(即类似Outlook的电子邮件软件) MTA: ...

  7. python的email、smtplib、poplib模块收发邮件

    一封电子邮件的旅程是: MUA:Mail User Agent--邮件用户代理.(即类似Outlook的电子邮件软件) MTA:Mail Transfer Agent--邮件传输代理,就是那些Emai ...

  8. python基础之psutil模块和发邮件(smtplib和yagmail)

    除了内建的模块外,Python还有大量的第三方模块. 基本上,所有的第三方模块都会在PyPI - the Python Package Index上注册,只要找到对应的模块名字,即可用pip安装. 此 ...

  9. 初识python 之 smtplib 发送(dolphinscheduler任务监测)邮件

    需求 监测dolphinscheduler调度系统,任务执行异常情况.如有异常,则发送邮件通知. 处理思路 因DS本身自带的邮件发送功能,不能正常发送邮件. 故而,通过查询DS源数据表,获取当前任务执 ...

随机推荐

  1. 关于iframe的一些使用

    在iframe页面,获取当前iframe的id var iframeId = window.frameElement.id.toString(); 获取父窗口中另一个iframe的iframe,注意返 ...

  2. 第一个web 程序(servlet 和 jsp )&

    开发工具是便于程序员的编写,真正运行的代码不是编写的代码,而是tomcat服务器中部署好的代码.tomcat 会根据请求自动调用对应的代码进行请求处理. 可能遇到的问题: 1. 没有classes文件 ...

  3. [转帖]详解oracle数据库唯一主键SYS_GUID()

    详解oracle数据库唯一主键SYS_GUID() https://www.toutiao.com/i6728736163407856139/ 其实 需要注意 这里满不能截取 因为截取了 就不一定唯一 ...

  4. [转帖]8086 CPU 寄存器简介

    8086 CPU 寄存器简介 https://www.cnblogs.com/BoyXiao/archive/2010/11/20/1882716.html 哎 没看完 感觉好复杂. 引子 打算写几篇 ...

  5. C++:标准C函数(随机数,时间函数)

    介绍 ANSI组织定义了C标准和标准库函数. 使用标准C函数优点: 使用标准C函数在任何平台上都支持,使得同一个源码,在Windows编译运行的结果和Linux上编译运行结果相同,无需更改代码. 随机 ...

  6. Markdown中有序列表和无序列表

    最近有用户问我,在简书写 Markdown, 一条有序列表 item 之后接一条无序列表 item,为什么 parse 的结果,第二个 item 依旧是作为有序列表的第二项显示,带有有序列表的列表符号 ...

  7. 【爬虫集合】抖音API分析

    1. 分析接口 Charles注册码 Registered Name: https://zhile.io License Key: 48891cf209c6d32bf4 抖音API分析 抖音.猫眼网页 ...

  8. win10 右键新建卡顿

    前段时间不知道自己搞啥了,右键变得很慢,找了一些常规的解决方案,什么清除注册表等等的,对我来说,没好用. 然后将就继续使用,然后觉得是office的问题,卸载,重装2010的,发现还是一样卡... 继 ...

  9. 使用shared memory 计算矩阵乘法 (其实并没有加速多少)

    #include "cuda_runtime.h" #include "device_launch_parameters.h" #include "d ...

  10. C# 循环中 直接索引 VS 缓存索引 性能测试

    using System; namespace TestCSharp { class MainClass { public class t1 { public b1 b = new b1(); } p ...