Python(1)自动发送邮件
python发邮件需要掌握两个模块的用法,smtplib和email,这俩模块是python自带的,只需import即可使用。smtplib模块主要负责发送邮件,email模块主要负责构造邮件。
smtplib模块主要负责发送邮件:是一个发送邮件的动作,连接邮箱服务器,登录邮箱,发送邮件(有发件人,收信人,邮件内容)。
email模块主要负责构造邮件:指的是邮箱页面显示的一些构造,如发件人,收件人,主题,正文,附件等。
1.smtplib模块
import smtplib smtp = smtplib.SMTP()
smtp.connect('smtp.163.com,25')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
2.email模块
from email.mime.text import MIMEText
from email.header import Header
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
我们必须把Subject,From,To添加到MIMEText对象或者MIMEMultipart对象中,邮件中才会显示主题,发件人,收件人。
# 组装邮件内容和标题,中文需参数‘utf-8’,单字节字符不需要
msg = MIMEMultipart()
msg['Subject'] = Header(subject)
msg['From'] = sender
msg['To'] = ','.join(user_list)
3.编写发件函数,方便调用
自动化测试报告为HTML,以附件方式发送,
不管什么类型的附件,都可以用MIMEApplication,MIMEApplication默认子类型是application/octet-stream
# 发送html内容的邮件
import smtplib
import time
import os
from email.mime.text import MIMEText
from email.header import Header
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart class SendMail():
def find_new_file(self, dir):
'''查找目录下最新的文件'''
file_lists = os.listdir(dir)
file_lists.sort(key=lambda fn: os.path.getmtime(dir + "\\" + fn)
if not os.path.isdir(dir + "\\" + fn)
else 0)
# print('最新的文件为: ' + file_lists[-1])
file = os.path.join(dir, file_lists[-1])
print('完整文件路径:', file)
return file def send_mail_html(self, file):
'''发送html格式测试报告邮件'''
# 发送邮箱
sender = 'name@163.com'
# 接收邮箱
user_list = [
'user@foxmail.com',
'user@qq.com',
'user@qq.com']
# 发送邮件主题
t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
subject = '接口自动化测试结果(请下载附件查看)_' + t
# 发送邮箱服务器
smtpserver = 'smtp.163.com'
# 发送邮箱用户/密码
username = 'user@163.com'
password = 'youer_password'
# 组装邮件内容和标题,中文需参数‘utf-8’,单字节字符不需要
msg = MIMEMultipart()
msg['Subject'] = Header(subject)
msg['From'] = sender
msg['To'] = ','.join(user_list)
# ---这是附件部分---
# html类型附件,不管什么类型的附件,都可以用MIMEApplication,MIMEApplication默认子类型是application/octet-stream。
part = MIMEApplication(open(file, 'rb').read())
part.add_header('Content-Disposition', 'attachment', filename=file)
msg.attach(part)
# 登录并发送邮件
try:
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(username, password)
smtp.sendmail(sender, user_list, msg.as_string())
except BaseException:
print("邮件发送失败!")
else:
print("邮件发送成功!")
finally:
smtp.quit() if __name__ == '__main__':
sen = SendMail()
dir = r'D:\\api\\report' # 指定文件目录
b = sen.find_new_file(dir) # 查找最新的html文件
sen.send_mail_html(b) # 发送html内容邮件
ps:
python邮件发送给多人时,只有第一个人能收到的问题,
MIMEText()["to"]的数据类型与sendmail(from_addrs,to_addrs,...)的to_addrs不同;前者为str类型,多个地址使用逗号分隔,后者为list类型。
expects toaddrs to be a list of email addresses. (Or, of course, just use recipients in place of toaddrs.)
参考链接:https://stackoverflow.com/questions/20509427/python-not-sending-email-to-multiple-addresses
Python(1)自动发送邮件的更多相关文章
- python selenium-7自动发送邮件
https://jingyan.baidu.com/article/647f0115b78f8d7f2148a8e8.html 1.发送HTML格式的邮件 import smtplib from em ...
- python实现自动发送邮件
Python发送邮件成功的前提,应是先开启授权码.目前使用广泛的邮箱有:163邮箱.qq邮箱等. 163邮箱开启授权码的方法如下图: qq邮箱开启授权码的方法如下图: 接下来代码的实现: import ...
- python如何自动发送邮件
#coding=utf-8 import smtplib from email.mime.text import MIMEText from email.mime.application import ...
- python☞自动发送邮件
一.SMTP 协议 SMTP(Simple Mail Transfer Protocol)是简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式 二.smtplib ...
- 用Python自动发送邮件
用Python自动发送邮件 最近需要在服务器上处理一些耗时比较长的任务,因此想到利用python写一个自动发送邮件的脚本,在任务执行完毕后发送邮件通知我.以下代码以163邮箱为例: 开通163 ...
- python+selenium生成测试报告后自动发送邮件
标签(空格分隔): 自动化测试 运行自动化脚本后,会产生测试报告,而将测试报告自动发送给相关人员,能够让对方及时的了解测试情况,查看测试结果. 整个脚本包括三个部分: 生成测试报告 获取最新的测试报告 ...
- Python 自动发送邮件
简单邮件传输协议(SMTP)是一种协议,用于在邮件服务器之间发送电子邮件和路由电子邮件.Python提供smtplib模块,该模块定义了一个SMTP客户端会话对象,可用于使用SMTP或ESMTP侦听器 ...
- 利用Python自动发送邮件
# -*- coding:utf-8 -*-from email.mime.text import MIMETextfrom email.header import Headerimport smtp ...
- python 自动发送邮件遇到的问题
在学习自动化测试高级应用,根据内容写关于自动发送邮件出现了几个问题以及解决办法: 问题1: 代码写好之后,运行的时候,出现如下报错: SMTPAuthenticationError: (550, '\ ...
- Python3.x:定时自动发送邮件
定时自动发送邮件 一.简述 python发邮件需要掌握两个模块的用法,smtplib和email,这俩模块是python自带的,只需import即可使用.smtplib模块主要负责发送邮件,email ...
随机推荐
- [Cypress] install, configure, and script Cypress for JavaScript web applications -- part4
Load Data from Test Fixtures in Cypress When creating integration tests with Cypress, we’ll often wa ...
- Mongodb 分片 手动维护chunk
去年的笔记 For instance, if a chunk represents a single shard key value, then MongoDB cannot split the ch ...
- MongoDB 4.0 事务实现解析
MongoDB 4.0 引入的事务功能,支持多文档ACID特性,例如使用 mongo shell 进行事务操作 > s = db.getMongo().startSession() sessio ...
- 内置对象(Math、Date、String、Array、基本包装类型)
一.内置对象 js中三种对象:内置对象.自定义对象.浏览器对象 实例对象是指通过构造函数创建出来,然后实例化的对象(new关键字) 静态对象是指不需要创建,直接调用的对象,可以在整个JS里调用的公共对 ...
- 对象(面向对象、创建对象方式、Json)
一.面向对象 面向过程:凡事亲力亲为,每件事的具体过程都要知道,注重过程 面向对象:根据需求寻找对象,所有的事都用对象来做,注重结果 面向对象特性:封装.继承.多态(抽象性) js是一门基于对象的语言 ...
- P2313 [HNOI2005]汤姆的游戏
题目描述 汤姆是个好动的孩子,今天他突然对圆规和直尺来了兴趣.于是他开始在一张很大很大的白纸上画很多很多的矩形和圆.画着画着,一不小心将他的爆米花弄撒了,于是白纸上就多了好多好多的爆米花.汤姆发现爆米 ...
- Word 有哪些神奇的功能?
作者:秦阳链接:https://www.zhihu.com/question/27035859/answer/621742048来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- zabbix (11) 监控TCP连接数
对TCP的监控可以采用ss.netstat./proc/net/tcp这三个不同的方案来实现.其中ss是最快的 (1)ss命令 [root@manager1 script_py ::]#time ss ...
- 页面tr和td的的隐藏与显示
<view:qrytr attributes="class=zcrzs"> </view:qrytr> var bd11 = $("tr[ ...
- BeanFactory 简介以及它 和FactoryBean的区别
BeanFacotry是spring中比较原始的Factory.如XMLBeanFactory就是一种典型的BeanFactory.原始的BeanFactory无法支持spring的许多插件,如AOP ...