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

相關文章
相關標籤/搜索