在之前的工作中,测试web界面产生的报告是自动使用python中发送邮件模块实现,在全部自动化测试完成之后,把报告自动发送给相关人员

其实在python中很好实现,一个是smtplib和mail俩个模块来实现,主要如下:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
import os sender = 'xxx@126.com'
receives = 'xxx@qq.com'
cc_receiver = 'xxx@qq.com'
subject = 'Python auto test' msg = MIMEMultipart()
msg['From'] = sender
msg['To'] = receives
msg['Cc'] = cc_receiver
msg['Subject'] = subject
msg.attach(MIMEText('Dont reply','plain','utf-8')) os.chdir('H:\\')
with open('auto_report03.html','r',encoding = 'utf-8') as fp:
att = MIMEBase('html','html')
att.add_header('Content-Disposion','attachment',filename = 'auto_report03.html')
att.set_payload(fp.read())
msg.attach(att) sendmail = smtplib.SMTP()
sendmail.connect('smtp.126.com',25)
sendmail.login(xxx,xxx')
sendmail.sendmail(sender,receives,msg.as_string())
sendmail.quit()

在这里我们可以把发送mail的代码进行封装成一个函数供外部调用,如下:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
import os def sendMail(subject,textContent,sendFileName):
sender = 'xxx@126.com'
receives = 'xxx@qq.com'
cc_receiver = 'xxx@qq.com'
subject = subject msg = MIMEMultipart()
msg['From'] = sender
msg['To'] = receives
msg['Cc'] = cc_receiver
msg['Subject'] = subject
msg.attach(MIMEText(textContent,'plain','utf-8')) os.chdir('H:\\')
with open(sendFileName,'r',encoding = 'utf-8') as fp:
att = MIMEBase('html','html')
att.add_header('Content-Disposion','attachment',filename = sendFileName)
att.set_payload(fp.read())
msg.attach(att) sendmail = smtplib.SMTP()
sendmail.connect('smtp.126.com',25)
sendmail.login('xxx',xxx')
sendmail.sendmail(sender,receives,msg.as_string())
sendmail.quit()

这里把主题、正文和附件文件作为参数代入,函数中的其他变量也可以作为参数输入,个人觉得没什么必要,但可以把其他的一些变量放到文件来读取,这样方便管理

python之发送邮件~的更多相关文章

  1. python大法好——Python SMTP发送邮件

    Python SMTP发送邮件 SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式. py ...

  2. Python——SMTP发送邮件

    一.定义 SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式.python的smtplib ...

  3. 用Python自动发送邮件

    用Python自动发送邮件     最近需要在服务器上处理一些耗时比较长的任务,因此想到利用python写一个自动发送邮件的脚本,在任务执行完毕后发送邮件通知我.以下代码以163邮箱为例: 开通163 ...

  4. Appium+python 自动发送邮件(1)(转)

    (原文:https://www.cnblogs.com/fancy0158/p/10056091.html) SMTP:简单传输协议,实在Internet上传输Email的事实标准. Python的s ...

  5. python+selenium 发送邮件

    import time from selenium import webdriver from selenium.webdriver import ChromeOptions from seleniu ...

  6. python smtplib发送邮件遇到的认证问题

    python的smtplib模块主要是用来发送邮件的,使用起来比较方便. 使用程序发送邮件只需要写以下几行代码就OK了: #!/usr/bin/env python import smtplib s ...

  7. python实现发送邮件功能

    '''套接字是为特定的网络协议(例如TCP/IP,ICMP/IP,UDP/IP等),允许程序和接受并进行连接,要在python 中建立具有TCP和流套接字的简单服务器,需要使用socket模块,利用该 ...

  8. python SendMail 发送邮件

    最近在学习python 时,用到了发送邮件的操作,通过整理总结如下: 1.普通文本邮件 普通文本邮件发送的实现,关键是要将MIMEText中_subtype设置为plain,首先导入smtplib和m ...

  9. python模范发送邮件的时候,才smtp.connect的时候总是抛出错误

    python发送邮件的时候,总是出现:[Errno 10060] 错误码 根据debug得到在connect的时候出错. 认真检查了下host,没有错呀~应该就是服务器的host. 查看了下网上的一些 ...

随机推荐

  1. Polynomial regression

  2. 100道c++面试题(上)

    1. new, delete, malloc, free关系 new/delete是c++的运算符,delete会调用对象的析构函数: malloc/free是c/c++的标准库函数,free只释放内 ...

  3. FortiGate下视频会议等语音相关配置

    关闭老的基于会话的alg机制(即删除session-helper中的SIP条目) config system session-helper delete 13  #删除sip end

  4. java编译时出现——注:使用了未经检查或不安全的操作。注:有关详细信息,请使用 -Xlint:unchecked 重新编译

    网上说是泛型问题 private List<Product> products = new ArrayList<Product>(); 这种用法绝对没错!(因为是照着书写的)在 ...

  5. Python开发——函数【基础】

    函数的定义 以下规则 函数代码块以 def 关键词开头,后接函数标识符名称和圆括号(). 任何传入参数和自变量必须放在圆括号中间.圆括号之间可以用于定义参数. 函数的第一行语句可以选择性地使用文档字符 ...

  6. 接口测试3A原则

    手工的功能测试用例也可以用3A原则来编写. Arrange: 准备被测功能相关的测试数据,比如往系统里录入一批工单以便测试工单的分页功能 Act : 调用被测的功能,实际上这就是我们一直讲的测试步骤 ...

  7. PowerDesigner 缺省值 引号 问题

    在使用PowerDesigner做为MySQL数据库建模的时候,总是有这样的问题,例如我需要一个字段 createTime 类型是 Timestamp,要求这个字段默认值为系统当前时间,于是我给这个字 ...

  8. 《Linux就该这么学》第四天课程

     秦时明月经典语录: 侠道:五步之内,百人不当.十年磨剑,一孤侠道——荆轲 我发了一些课堂笔记,供你们参考 原创地址:https://www.linuxprobe.com/chapter-03.htm ...

  9. javascript常见内存泄露

    一.全局变量引起的内存泄漏 function func(){ lmw = 123456 //lmw是全局变量,不会被释放 } 二.闭包引起的内存泄漏 function func(){ var lmw ...

  10. numpy版本查看以及升降

     如题,参考:https://zhuanlan.zhihu.com/p/29026597 pip show numpy 查看numpy版本; pip install -U numpy==1.12.0, ...