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

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. 把axios挂载到vue实例上面/==Axios 各种请求方式传递参数格式

    /*ajax请求*/   import axios from 'axios'   axios.defaults.baseURL = 'https://api.douban.com/v2/movie' ...

  2. Python操作Redis(转)

    原文1:https://cloud.tencent.com/developer/article/1151834 原文2:https://www.cnblogs.com/zhaohuhu/p/91406 ...

  3. HTML 禁止复制文字

    因为本人平时喜欢看网络小说,但是喜欢看的文通过正经网站或者app都需要收费,让人很是不爽,所以...总之,百度网盘上资源很多.但是问题来了,这些资源肯定不会是作者自己流出的,也不应该是网站或app流出 ...

  4. C# Task.WhenAll

    1.有时候我们需要同时执行一些操作,然后把这些操作的结果进行汇总,以达到用异步处理降低操作耗时的效果,此时我们会考虑使用Task,而Task.WhenAll则排上了用场. public void Is ...

  5. nodejs入门API之path模块

    Path模块在各个系统上的差异 Path模块API解析 一.Path模块在各个系统上的差异 path模块提供用于处理文件路径和目录路径的使用工具. let path = require('path') ...

  6. vue-cli搭建vue项目环境

    该篇文章是继https://www.cnblogs.com/qing-5/p/11321585.html来写 1.打开终端,输入指令"npm install --global vue-cli ...

  7. S2-029

    前言 S2-029漏洞是由于Struts2的i18n.text标签中的name属性的值会经过两次Ognl表达式解析. 正文 假设有如下缺陷代码: jsp文件中使用Struts2的i18n标签获取请求中 ...

  8. XCode5环境下利用crash log调试线上Crash的流程

    1.前言 本文主要介绍在XCode5环境下,如何根据App自己生成的crashlog来调试真机上运行时产生的crash问题. 2. 步骤 (1)构造一段会crash的代码,并放到viewDidLoad ...

  9. selenium实现登录百度(自动识别简单验证码)

    需要做的工作 0.工程结构 1.代码: ①baidu_login.py import re import os import sys import time import random from se ...

  10. XSS学习收集

    XSS platform supporting HTTPShttps://www.w0ai1uo.org/xss/xss.php?do=loginhttps://x.secbox.cn/index.p ...