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

简介

python发邮件以前用的是smtplib,代码太过于复杂,学习成本大,而且不少人学不会。以前专门写过一篇https://www.cnblogs.com/yoyoketang/p/7277259.html,无奈仍是一大堆人发送邮件失败。 今天介绍一个最简单,最强大的发邮件的包zmail,简单好上手,妈妈不再用担忧我不会发邮件了! github原文地址https://github.com/ZYunH/zmailhtml

zmail简介

Zmail容许您在python中尽量发送和接收电子邮件。无需检查服务器地址或制做您本身的MIME对象。使用zmail,您只须要关心您的邮件内容。 Zmail只在python3中运行,不须要第三方模块。不支持python2python

pip3 install zmailgit

特征:github

  • 自动查找服务器地址及其端口。
  • 自动使用合适的协议登陆。
  • 自动将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>' 邮件来定义发件人的姓名。spa

收件人列表

server.send_mail([ ' yourfriend@example.com ',' 12345 @ example.com ' ],mail)code

你也能够命名它们(使用元组,首先是它的名字,下一个是它的地址)server

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

相关文章
相关标签/搜索