import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from HTMLTestRunner import HTMLTestRunner
from email.header import Header
import unittest
import time,os

#==============定义发送邮件 ===============

def send_mail(file_new):
    f = open(file_new,'rb')
    #读取测试报告正文
    mail_body = f.read()
    f.close()
    # 发送邮箱服务器
    smtpserver = "smtp.wondershare.cn"
    # 发件人邮箱
    sender = 'buyer_1291455767_per@wondershare.cn'
    # 发件人邮箱密码
    password = 'Hello789'
    # 接收人邮箱
    receiver = ['jiangyf@wondershare.cn','buyer_1291455767_per@wondershare.cn']

    #通过  模块构造的带附件的邮件如图
    msg = MIMEMultipart()
    #编写html类型的邮件正文,MIMEtext()用于定义邮件正文
    #发送正文
    text = MIMEText(mail_body, 'html', 'utf-8')
    text['Subject'] = Header('自动化测试报告', 'utf-8')
    msg.attach(text)
    #发送附件
    #Header()用于定义邮件标题
    msg['Subject'] = Header('自动化测试报告', 'utf-8')
    msg_file = MIMEText(mail_body, 'html', 'utf-8')
    msg_file['Content-Type'] = 'application/octet-stream'
    msg_file["Content-Disposition"] = 'attachment; filename="TestReport.html"'
    msg.attach(msg_file)

# 如果只发正文的话,上面的代码 从receiver下面到这段注释上面
# 全部替换为下面的两行代码即可,上面的代码是增加了发送附件的功能。
#     text = MIMEText(mail_body, 'html', 'utf-8')
#     text['Subject'] = Header('自动化测试报告', 'utf-8')

    smtp = smtplib.SMTP()
    smtp.connect(smtpserver)
    smtp.login(sender, password)  # 登录的用户名和密码
    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()
    print('sendmail success')

#======================查找最新的测试报告==========================

def new_report(testreport):
    dirs = os.listdir(testreport)
    dirs.sort()
    newreportname = dirs[-1]
    print('The new report name: {0}'.format(newreportname))
    file_new = os.path.join(testreport, newreportname)
    return file_new

if __name__ == '__main__':
    test_report = "D:\\Python\\Demo\\report"
    new_report = new_report(test_report)
    send_mail(new_report)

  

python发送邮件(在邮件中显示HTMLTestRunner生成的报告)的更多相关文章

  1. 定时执行自动化脚本-(二)ant发送邮件及邮件中添加附件

    发送邮件及邮件添加附件均需要用java来实现 1.idea创建一个maven的java项目,目录结构如下 2.pom.xml文件添加依赖的javax.mail <dependencies> ...

  2. Python3.0 调用HTMLTestRunner生成的报告中不能显示用例中print函数的输出

    官方原生的HTMLTestRunner.py支持python2.0版本,python3.0版本的使用需要做一些修改: Python3调用HTMLTestRunner执行用例生成测试报告中,不能正常显示 ...

  3. 转-调用HTMLTestRunner生成的报告中不能显示用例中print函数的输出

    官方原生的HTMLTestRunner.py支持python2.0版本,python3.0版本的使用需要做一些修改: Python3调用HTMLTestRunner执行用例生成测试报告中,不能正常显示 ...

  4. Python 3.6版本中实现 HTMLTestRunner输出时”fp=file(filename,'wb')“报错

    原错误代码: if __name__=='__main__': suite=unittest.makeSuite(WidgetTestCase) filename='D:\\myreport.html ...

  5. 【转】【Python】Python发送邮件(常见四种邮件内容)

    在写脚本时,放到后台运行,想知道执行情况,会通过邮件.SMS(短信).飞信.微信等方式通知管理员,用的最多的是邮件.在linux下,Shell脚本发送邮件告警是件很简单的事,有现成的邮件服务软件或者调 ...

  6. python发送邮件

    python发送邮件(无附件) ======================================================= #!/usr/bin/env python#coding ...

  7. python发送各类邮件的主要方法

    更多详见: http://www.w3cschool.cc/python/python-email.html python中email模块使得处理邮件变得比较简单,今天着重学习了一下发送邮件的具体做法 ...

  8. Python发送邮件(最全)

    简单邮件传输协议(SMTP)是一种协议,用于在邮件服务器之间发送电子邮件和路由电子邮件. Python提供smtplib模块,该模块定义了一个SMTP客户端会话对象,可用于使用SMTP或ESMTP侦听 ...

  9. python发送邮件方法总结

    python中email模块使得处理邮件变得比较简单,今天着重学习了一下发送邮件的具体做法,这里写写自己的的心得,也请高手给些指点.     一.相关模块介绍 发送邮件主要用到了smtplib和ema ...

随机推荐

  1. Django中的信号基础知识

    Django中提供了“信号调度”,用于在框架执行操作时解耦.通俗来讲,就是一些动作发生的时候,信号允许特定的发送者去提醒一些接受者. 1.Django内置信号 1 2 3 4 5 6 7 8 9 10 ...

  2. Debian 9 strech 安装 ROS lunar

    1. 配置源 按照我以前的博客配置或者按照wiki上的配置. 2. sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(ls ...

  3. HDOJ 1754 I Hate It (线段树)

    题目: Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感.不管你喜不喜欢,现在需要你做的是,就是按照老师的要 ...

  4. python异步编程模块asyncio学习(二)

    尽管asyncio应用通常作为单线程运行,不过仍被构建为并发应用.由于I/O以及其他外部事件的延迟和中断,每个协程或任务可能按一种不可预知的顺序执行.为了支持安全的并发执行,asyncio包含了thr ...

  5. FHQ Treap摘要

    原理 以随机数维护平衡,使树高期望为logn级别 不依靠旋转,只有两个核心操作merge(合并)和split(拆分) 因此可持久化 先介绍变量 ; int n; struct Node { int v ...

  6. SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data错误的解决

    记录个报错: 问题描述: 经过服务器生成图片返到前台时,在火狐浏览器中下载图片或打开图片时报错:SyntaxError: JSON.parse: unexpected character at lin ...

  7. Windows系统下安装dig命令

    dig 是一个 Linux 下用来 DNS 查询信息的工具,全称是Domain Information Groper,与 nslookup 类似,但比 nslookup 功能更强大.Windows 下 ...

  8. oracle 报表带小计合计

    selectcase when (grouping(glbm)=1) then '合计' else DECODE(glbm,null,'',glbm) end glbm,case when (grou ...

  9. Struts2配置拦截器

    <package name="loginaction" namespace="/" extends="struts-default"& ...

  10. iOS Core Data 数据库的加密(待研究)

    https://github.com/project-imas/encrypted-core-data 使用起来很方便,底层还是使用了SQLCipher,有时间要研究一下! 数据库的密码不能用固定字符 ...