简介

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

版权声明:本文为yoyoketang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/yoyoketang/p/11048495.html