自定义了一个email模块,符合大多数人的使用习惯
# coding: utf-8 import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
import re def e_mail(smtpserver ='smtp.163.com',
username='自己邮箱账号',
password='自己邮箱授权码',
receiver='目标邮箱地址',
subject='邮件主题',
text='邮件内容',
sendfiles=None, # 附件
): """
此函数用以发送邮件
参数说明:smtpserver为smtp服务器
username为发送者邮箱用户名
password为发送者邮箱密码/授权码
receiver为接收者邮箱(可以有多个,用','分隔)
subject为邮件主题
text为邮件内容
sendfiles为附件路径(可以有多个,用','分隔)
注:所有参数均为字符串类型 """ # 发件人
sender = username # 收件人(可以为多个收件人)
# receivers = ['aaakkkbbbaaa2@163.com', 'liwei@staff.cntv.cn']
receivers = []
for i in receiver.split(','):
receivers.append(i) # 邮件标题
subject = Header(subject, 'utf-8').encode() # 邮件内容
main_body = text # 构造邮件对象
msg = MIMEMultipart('mixed') # 将邮件标题、发件人、收件人加入邮件对象。
msg['Subject'] = subject
msg['From'] = '%s <%s>' % (username, username)
# msg['To'] = 'XXX@126.com'
# 收件人为多个收件人,通过join将列表转换为以;为间隔的字符串
msg['To'] = ";".join(receivers)
# msg['Date']='2012-3-16' # 将邮件内容加入邮件对象
if text:
# 编码文字内容
text_plain = MIMEText(main_body, 'plain', 'utf-8')
# 将文字内容加入邮件对象
msg.attach(text_plain) # 将附件加入邮件对象
if sendfiles:
# 遍历所有附件
for i in sendfiles.split(','):
# 加载附件
sendfile = open(r'%s' % i, 'rb')
sendfile = sendfile.read()
text_att = MIMEText(sendfile, 'base64', 'utf-8')
text_att["Content-Type"] = 'application/octet-stream'
# 为附件命名
try:
file_name = re.match(r'.*[\\/](.*)$', i).group(1)
except AttributeError:
file_name = i
text_att.add_header('Content-Disposition', 'attachment', filename=file_name)
# 将附件加入邮件对象
msg.attach(text_att) # 发送邮件
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
# 用set_debuglevel(1)可以打印出和SMTP服务器交互的所有信息。
# smtp.set_debuglevel(1)
smtp.login(username, password)
smtp.sendmail(sender, receivers, msg.as_string())
smtp.quit()
return '邮件发送完毕' if __name__ == '__main__':
e_mail(sendfiles=r'C:\Users\Administrator\Desktop\结果.png,C:\Users\Administrator\Desktop\Agent说明文档.txt')
自定义了一个email模块,符合大多数人的使用习惯的更多相关文章
- python email模块
python email模块 官方文档 email模块 电子邮件包是一个用于管理电子邮件消息的库.它的特殊设计不用于向SMTP (RFC 2821).NNTP或其他服务器发送任何电子邮件消息;这些是模 ...
- Python_使用smtplib和email模块发送邮件
[http://blog.csdn.net/menglei8625/article/details/7721746] SMTP (Simple Mail Transfer Protocol) 邮件传送 ...
- C程序中引用自定义的C函数模块
原文:C程序中引用自定义的C函数模块 我们知道,刚开始接触C语言编程,一般都是在一个.c或者.cpp(以下只说.c)的文件中编写代码,其中一定会有一个入口函数, 也就是main()函数,你可以将程序代 ...
- python email ==> send 发送邮件 :) [smtplib, email 模块]
关于Email的预备知识: 原贴地址:http://www.cnblogs.com/lonelycatcher/archive/2012/02/09/2343480.html ############ ...
- 使用spring EL表达式+自定义切面封装缓存模块
需求是这样的,业务代码需要使用到缓存功能以减少数据库压力,使用redis来实现,并且需要生成缓存的key由方法的传参拼接而成(貌似也只能这样才能保证同样的select查询可以使用缓存),简单的方式就是 ...
- python+selenium之自定义封装一个简单的Log类
python+selenium之自定义封装一个简单的Log类 一. 问题分析: 我们需要封装一个简单的日志类,主要有以下内容: 1. 生成的日志文件格式是 年月日时分秒.log 2. 生成的xxx.l ...
- smtplib与email模块(实现邮件的发送)
SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件. Python对SMTP支持有smtplib和email两个模块,email负责构造邮件, ...
- Python使用SMTP模块、email模块发送邮件
一.smtplib模块: 主要通过SMTP类与邮件系统进行交互.使用方法如下: 1.实例化一个SMTP对象: s = smtplib.SMTP(邮件服务地址,端口号) s = smtplib.SMTP ...
- Python之自定义封装一个简单的Log类
参考:http://www.jb51.net/article/42626.htm 参考:http://blog.csdn.net/u011541946/article/details/70198676 ...
随机推荐
- Exp3 免杀原理与实践_05齐帅
Exp3 免杀原理与实践 20154305_齐帅 想要弄懂免杀,一定得先把基础问题弄明白啊~~ 一.基础问题回答 (1)杀软是如何检测出恶意代码的? - -检测特征码: 依靠分析总结出计算机病毒中常出 ...
- java集成memcached、redis防止缓存穿透
下载相关jar,安装Memcached,安装教程:http://www.runoob.com/memcached/memcached-install.html spring配置memcached &l ...
- 更改了ssh文件下,还没有权限
执行 cd /root/.ssh chmod 600 id_*
- this用法总结
在JavaScript中,this关键字可以说是最复杂的机制之一.对this的作用机制缺乏比较深入的理解很容易在实际开发中出现问题. 1.this的作用 为什么要在JavaScript中使用this呢 ...
- Django关联数据库时报错TypeError: __init__() missing 1 required positional argument: 'on_delete'
sgrade = models.ForeignKey("Grades",) 执行python manage.py makemigrations后出现TypeError: __ini ...
- CLion之C++框架篇-优化框架,引入boost(三)
背景 结合上一篇CLion之C++框架篇-优化框架,单元测试(二),继续进行框架优化!这一版优化引入一个我们日常经常使用的操作库Boost,估算使用频率在70%以上! Boost的优势在哪 ...
- 背水一战 Windows 10 (117) - 后台任务: 后台下载任务
[源码下载] 背水一战 Windows 10 (117) - 后台任务: 后台下载任务 作者:webabcd 介绍背水一战 Windows 10 之 后台任务 后台下载任务 示例演示 uwp 的后台下 ...
- Javascript高级编程学习笔记(36)—— DOM(2)Document
Documet类型 了解了基础的Node类型过后,我们来聊聊Node中的Document类型 我们知道所有的节点都继承自Node类型 所以除了Node类型公有的方法和类型之外,Document类型还有 ...
- 7.首页、bitmaputils
HomeProtocol public class HomeProtocol extends BaseProtocol<List<AppInfo>>{ // 1 把整个json ...
- puppet-type
puppet语法-type Table of Contents Custom Source 基本技能要求 Types简介 Type-Documentation Type-Properties Type ...