前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python笔记37-史上最好用的发邮件zmail

python笔记37-史上最好用的发邮件zmail

作者头像
上海-悠悠
发布2019-06-21 14:11:04
2.1K0
发布2019-06-21 14:11:04
举报
文章被收录于专栏:从零开始学自动化测试

简介

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即可

快速开始

代码语言:javascript
复制
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功能是否正常工作

代码语言:javascript
复制
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。

发送邮件

代码语言:javascript
复制
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内容

代码语言:javascript
复制
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)

或者

代码语言:javascript
复制
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)

收到你的邮件

获取最新邮件

代码语言:javascript
复制
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()

代码语言:javascript
复制
import zmail
server = zmail.server('yourmail@example.com', 'yourpassword')
mail = server.get_latest()
zmail.show(mail)

查看邮件中的所有内容。

代码语言:javascript
复制
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

第9期《python3接口自动化测试》课程,6月29号开学! 主讲老师:上海-悠悠 上课方式:QQ群视频在线教学 本期上课时间:6月29号-7月28号,每周六、周日晚上20:30-22:30 课表详情:https://www.cnblogs.com/yoyoketang/p/11030218.html 课表详情,点左下角[阅读原文]

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-06-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 从零开始学自动化测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 简介
  • zmail简介
  • 快速开始
  • 案例
  • 发送邮件
    • 收件人列表
      • 发送HTML内容
        • 使用抄送
          • 自定义您的服务器
          • 收到你的邮件
          • 解析你的邮件
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档