python笔记37-史上最好用的发邮件zmail
简介
python发邮件之前用的是smtplib,代码太过于复杂,学习成本大,并且很多人学不会。之前专门写过一篇https://www.cnblogs.com/yoyoketang/p/7277259.html,无奈还是一大堆人发送邮件失败。
今天介绍一个最简单,最强大的发邮件的包zmail,简单好上手,妈妈再也不用担心我不会发邮件了!
github原文地址https://github.com/ZYunH/zmail
zmail简介
Zmail允许您在python中尽可能发送和接收电子邮件。无需检查服务器地址或制作您自己的MIME对象。使用zmail,您只需要关心您的邮件内容。
Zmail只在python3中运行,不需要第三方模块。不支持python2
pip3 install zmail
特征:
- 自动查找服务器地址及其端口。
- 自动使用合适的协议登录。
- 自动将python字典转换为MIME对象(带附件)。
- 自动添加邮件标题和本地名称,以避免服务器拒绝您的邮件。
- 轻松自定义邮件标题。
- 支持HTML作为邮件内容。
- 只需要python> = 3.5,您可以将其嵌入到项目中而无需其他模块。
在使用之前,请确保:
- 使用python3
- 在您的邮件中打开SMTP / POP3功能(对于@ 163.com和@ gmail.com,您需要设置您的应用程序私人密码)
然后,您只需要导入zmail即可
快速开始
import zmail
server = zmail.server('yourmail@example.com', 'yourpassword')
# Send mail
server.send_mail('yourfriend@example.com',{'subject':'Hello!','content_text':'By zmail.'})
# Or to a list of friends.
server.send_mail(['friend1@example.com','friend2@example.com'],{'subject':'Hello!','content_text':'By zmail.'})
# Retrieve mail
latest_mail = server.get_latest()
zmail.show(latest_mail)
案例
验证SMTP和POP功能是否正常工作
import zmail
server = zmail.server('yourmail@example.com’, 'yourpassword')
if server.smtp_able():
pass
# SMTP function.
if server.pop_able():
pass
# POP function.
如果SMTP和POP工作正常,该函数将返回True,否则返回Fasle。
发送邮件
import zmail
mail = {
'subject': 'Success!', # Anything you want.
'content_text': 'This message from zmail!', # Anything you want.
'attachments': ['/Users/zyh/Documents/example.zip','/root/1.jpg'], # Absolute path will be better.
}
server = zmail.server('yourmail@example.com', 'yourpassword')
server.send_mail('yourfriend@example.com', mail)
您可以通过添加 'from':'Boss <mymail@foo.com>'
邮件来定义发件人的姓名。
收件人列表
server.send_mail([ ' yourfriend@example.com ',' 12345 @ example.com ' ],mail)
你也可以命名它们(使用元组,首先是它的名字,下一个是它的地址)
server.send_mail([('Boss','yourfriend@example.com'),'12345@example.com'], mail)
发送HTML内容
mail = {
'subject': 'Success!', # Anything you want.
'content_html': ['HTML CONTENT'],
'attachments': '/Users/zyh/Documents/example.zip', # Absolute path will be better.
}
server.send_mail('yourfriend@example.com',mail)
或者
with open('/Users/example.html','r') as f:
content_html = f.read()
mail = {
'subject': 'Success!', # Anything you want.
'content_html': content_html,
'attachments': '/Users/zyh/Documents/example.zip', # Absolute path will be better.
}
server.send_mail('yourfriend@example.com',mail)
使用抄送
server.send_mail(['foo@163.com','foo@126.com'],mail,cc=['bar@163.com'])
同样,你也可以命名它们(使用元组,首先是它的名字,下一个是它的地址)
server.send_mail(['foo@163.com','foo@126.com'],mail,cc=[('Boss','bar@163.com'),'bar@126.com'])
自定义您的服务器
server = zmail.server('username','password',smtp_host='smtp.163.com',smtp_port=994,smtp_ssl=True,pop_host='pop.163.com',pop_port=995,pop_tls=True)
收到你的邮件
获取最新邮件
import zmail
server = zmail.server('yourmail@example.com‘, 'yourpassword')
mail = server.get_latest()
通过其ID检索邮件。
mail = server.get_mail(2)
获取邮件列表(主题,之后,之前,发件人)
mail = server.get_mails(subject='GitHub',start_time='2018-1-1',sender='github')
在示例中,如果'GitHub'在邮件的主题中,它将被匹配,例如'[GitHub]您的密码已更改'
发件人是一样的。
您还可以指定邮件范围。
mail = server.get_mails(subject='GitHub',start_time='2018-1-1',sender='github',start_index=1,end_index=10)
获取邮箱信息。
mailbox_info = server.stat()
结果是2个整数的元组:(message count, mailbox size)。
解析你的邮件
在zmail中,所有邮件都将映射到python字典,您可以通过访问您的邮件
subject = mail['subject']
显示邮件,使用zmail.show()
import zmail
server = zmail.server('yourmail@example.com', 'yourpassword')
mail = server.get_latest()
zmail.show(mail)
查看邮件中的所有内容。
import zmail
server = zmail.server('yourmail@example.com', 'yourpassword')
mail = server.get_latest()
for k,v in mail.items():
print(k,v)
github原文地址https://github.com/ZYunH/zmail
最新文章
- 解析大型.NET ERP系统 自动更新
- 烂泥:切割nginx日志
- C++ Windows进程管理
- Selenium2+python自动化27-查看selenium API
- Linux下使用inode删除文件
- canvas实现音乐中的歌词播放效果
- 两个队列+k叉哈夫曼树 HDU 5884
- Delphi Refactor 重构
- Java Applet and ServiceLoader
- Flexbox介绍
- 谷歌浏览器chrome假死、卡死、经常无反应,火狐firefox闪黑格子的解决办法(显卡/驱动兼容问题)
- 7.2 Database Backup Methods 数据备份方法:
- java socket报文通信(三)java对象和xml格式文件的相互转换
- webapp前端开发软键盘与position:fixed为我们带来的不便
- django进阶-4
- Windows环境安装运行:Angular.js
- 09 Zabbix4.0系统clone、mass update使用
- Button中command后面函数添加参数解决方法
- Sql的行列(纵横表)转换
- CSS3动画常用demo
热门文章
- CMDBuild部署教程
- Centos7——无法访问Windows系统的分区
- 利用docker搭建RTMP直播流服务器实现直播
- 【bat】【windows】win10查看所有wifi密码
- ElasticSearch中";distinct";,";count";和";group by";的实现
- Vim 入门
- Akka-CQRS(9)- gRPC,实现前端设备与平台系统的高效集成
- Win10各个PC版本的差别
- php GD 和图像处理函数, 制作一张图片
- ADO.net(内置类区别)随记