python发邮件之前用的是smtplib,代码太过于复杂,学习成本大,并且很多人学不会。之前专门写过一篇https://www.cnblogs.com/yoyoketang/p/7277259.html,无奈还是一大堆人发送邮件失败。 今天介绍一个最简单,最强大的发邮件的包zmail,简单好上手,妈妈再也不用担心我不会发邮件了! github原文地址https://github.com/ZYunH/zmail
Zmail允许您在python中尽可能发送和接收电子邮件。无需检查服务器地址或制作您自己的MIME对象。使用zmail,您只需要关心您的邮件内容。 Zmail只在python3中运行,不需要第三方模块。不支持python2
pip3 install 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)
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
第9期《python3接口自动化测试》课程,6月29号开学! 主讲老师:上海-悠悠 上课方式:QQ群视频在线教学 本期上课时间:6月29号-7月28号,每周六、周日晚上20:30-22:30 课表详情:https://www.cnblogs.com/yoyoketang/p/11030218.html 课表详情,点左下角[阅读原文]