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. 查看了下网上的一些 ...
随机推荐
- Python学习—爬虫篇之破解ntml登陆问题
之前帮公司爬取过内部的一个问题单网站,要求将每个问题单的下的附件下载下来.一开始的时候我就遇到一个破解登陆验证的大坑...... (╬ ̄皿 ̄)=○ 由于在公司使用的都是内网,代码和网站的描述 ...
- 好看的alert弹出框sweetalert
转载:https://www.cnblogs.com/lamp01/p/7215408.html
- C++成员函数在内存中的存储方式
用类去定义对象时,系统会为每一个对象分配存储空间.如果一个类包括了数据和函数,要分别为数据和函数的代码分配存储空间.按理说,如果用同一个类定义了10个对象,那么就需要分别为10个对象的数据和函数代码分 ...
- [leetcode]256. Paint House粉刷房子(三色可选)
There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...
- eclipse装了springboot插件后yml文件没有自动提示问题
选择打开方式:spring yaml
- ionic3使用第三方图标
1.打开阿里图标库http://www.iconfont.cn 2.找到自己所需的图标,加入购物车(免费使用的) 3.打开购物车,点击右下角(下载代码) 4.解压文件,打开demo_unicode.h ...
- C#中 property 与 attribute的区别
说的通俗些Attribute是类,不过是一类比较特殊的类,Attribute必须写在一对方括号中,用来处理.NET中多种问题:序列化.程序的安全特征等等,在.NET中的作用非同凡响 Attribute ...
- shell脚本监控系统负载、CPU和内存使用情况
hostname >>/home/vmuser/xunjian/xj.logdf -lh >>/home/vmuser/xunjian/xj.logtop -b -n 1 | ...
- Linux安装yum
方法/步骤 1 查看.卸载已安装的yum包 查看已安装的yum包 #rpm –qa|grep yum 卸载软件包 #rpm –e –nodeps yum 2 下载安装依赖包python python- ...
- 2019.02.15 codechef Favourite Numbers(二分+数位dp+ac自动机)
传送门 题意: 给444个整数L,R,K,nL,R,K,nL,R,K,n,和nnn个数字串,L,R,K,数字串大小≤1e18,n≤65L,R,K,数字串大小\le1e18,n\le65L,R,K,数字 ...