python 使用zmail收发电子邮件

pip3 install zmail

1、发送邮件:
import zmail
server = zmail.server('yourmail@example.com’, 'yourpassword')

Send mail

server.send_mail('yourfriend@example.com',{'subject':'Hello!','content_text':'By zmail.'})
server.send_mail(['friend1@example.com','friend2@example.com'],{'subject':'Hello!','content_text':'By zmail.'})

2、接收最后一封邮件:
import zmail
server = zmail.server('yourmail@example.com’, 'yourpassword')

Retrieve mail

latest_mail = server.get_latest()
zmail.show(latest_mail)

3、发送带附件的邮件:
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)
server.send_mail(['yourfriend@example.com','12345@example.com'], mail)

4、发送html格式邮件:
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)

5、使用抄送:
server.send_mail(['foo@163.com','foo@126.com'],mail,cc=['bar@163.com'])

6、自定义server
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)

7、根据ID取回邮件:mail = server.get_mail(2)
根据日期、主题、发送人取回邮件:
mail = server.get_mails(subject='GitHub',after='2018-1-1',sender='github')
mail = server.get_mails(subject='GitHub',start_time='2018-1-1',sender='github',start_index=1,end_index=10)

8、查看邮箱统计
mailbox_info = server.stat() #结果为包含两个整型的元组: (邮件的数量, 邮箱的大小).

9、删除邮件:MailServer.delete(which)
10、保存附件:zmail.save_attachment(mail,target_path=None,overwrite=False)
11、保存邮件:zmail.save(mail,name=None,target_path=None,overwrite=False)
12、读取邮件:zmail.read(file_path,SEP=b'\r\n')

支持的列表:


image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容