python之发送邮件~
在之前的工作中,测试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之发送邮件~的更多相关文章
- python大法好——Python SMTP发送邮件
Python SMTP发送邮件 SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式. py ...
- Python——SMTP发送邮件
一.定义 SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式.python的smtplib ...
- 用Python自动发送邮件
用Python自动发送邮件 最近需要在服务器上处理一些耗时比较长的任务,因此想到利用python写一个自动发送邮件的脚本,在任务执行完毕后发送邮件通知我.以下代码以163邮箱为例: 开通163 ...
- Appium+python 自动发送邮件(1)(转)
(原文:https://www.cnblogs.com/fancy0158/p/10056091.html) SMTP:简单传输协议,实在Internet上传输Email的事实标准. Python的s ...
- python+selenium 发送邮件
import time from selenium import webdriver from selenium.webdriver import ChromeOptions from seleniu ...
- python smtplib发送邮件遇到的认证问题
python的smtplib模块主要是用来发送邮件的,使用起来比较方便. 使用程序发送邮件只需要写以下几行代码就OK了: #!/usr/bin/env python import smtplib s ...
- python实现发送邮件功能
'''套接字是为特定的网络协议(例如TCP/IP,ICMP/IP,UDP/IP等),允许程序和接受并进行连接,要在python 中建立具有TCP和流套接字的简单服务器,需要使用socket模块,利用该 ...
- python SendMail 发送邮件
最近在学习python 时,用到了发送邮件的操作,通过整理总结如下: 1.普通文本邮件 普通文本邮件发送的实现,关键是要将MIMEText中_subtype设置为plain,首先导入smtplib和m ...
- python模范发送邮件的时候,才smtp.connect的时候总是抛出错误
python发送邮件的时候,总是出现:[Errno 10060] 错误码 根据debug得到在connect的时候出错. 认真检查了下host,没有错呀~应该就是服务器的host. 查看了下网上的一些 ...
随机推荐
- 大数据学习笔记2 - 分布式文件系统HDFS(待续)
分布式文件系统结构 分布式文件系统是一种通过网络实现文件在多台主机上进行分布式存储的文件系统,采用C/S模式实现文件系统数据访问,目前广泛应用的分布式文件系统主要包括GFS和HDFS,后者是前者的开源 ...
- 199. Binary Tree Right Side View 从右侧看的节点数
[抄题]: Given a binary tree, imagine yourself standing on the right side of it, return the values of t ...
- WordPress 自动初始化数据库
背景 自动化搭建开发环境.测试.部署如通过网页操作(访问 /wp-admin/install.php)相对比较麻烦且在有的场景无法实现. 步骤 修改 wp-config.php 配置 wordpres ...
- Codeforces Round #552 (Div. 3) A题
题目网址:http://codeforces.com/contest/1154/problem/ 题目意思:就是给你四个数,这四个数是a+b,a+c,b+c,a+b+c,次序未知要反求出a,b,c,d ...
- CentOS Linux下VNC Server远程桌面配置详解
http://www.ha97.com/4634.html PS:偶以前基本不用Linux的远程图形桌面,前几天有开发的同事配置CentOS的vnc有问题,找我解决,就顺便记录总结一下,这个总结是比较 ...
- E: Sub-process /usr/bin/dpkg returned an error code
E: Sub-process /usr/bin/dpkg returned an error code (1)错误解决 在用apt-get安装软件时出现了类似于install-info: No dir ...
- Linux 6上使用UDEV绑定共享存储
1.硬盘的查看方式 [root@cl6-11gr2-rac1 ~]# ls -ltr /dev/sd* brw-rw----. 1 root disk 8, 48 8月 16 13:34 /dev/s ...
- Solidity合约记录——(三)如何在合约中对操作进行权限控制
合约中一般会有多种针对不同数据的操作:例如对于存证内容的增加.更新及查询,若不进行一套符合要求的权限控制,事实上整个合约在真实环境下是没有多少使用价值的.那么应当如何对合约的权限进行划分?我们针对So ...
- brew安装和换源
命令行执行ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)&q ...
- tab下图片要求
下面是每个tab的属性: 属性 类型 必填 说明 pagePath String 是 页面路径,必须在pages中先定义 text String 是 tab上按钮文字 iconPath String ...