python發郵件以前用的是smtplib,代碼太過於複雜,學習成本大,而且不少人學不會。以前專門寫過一篇https://www.cnblogs.com/yoyoketang/p/7277259.html,無奈仍是一大堆人發送郵件失敗。 今天介紹一個最簡單,最強大的發郵件的包zmail,簡單好上手,媽媽不再用擔憂我不會發郵件了! github原文地址https://github.com/ZYunH/zmailhtml
Zmail容許您在python中儘量發送和接收電子郵件。無需檢查服務器地址或製做您本身的MIME對象。使用zmail,您只須要關心您的郵件內容。 Zmail只在python3中運行,不須要第三方模塊。不支持python2python
pip3 install zmailgit
特徵:github
在使用以前,請確保:服務器
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)
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