Python接收郵件的幾種方式

工作中,我們基本上都用過電子郵件的客戶端,比如說 OutLook,Foxmail,從配置項可以知道,SMTP 協議用於發送郵件,POP3 和 IMAP 協議用於接收郵件。其實很多編程語言都有這類協議的實現,Python 自然也不例外,標準庫 smtplib、poplib、imaplib 是對應協議的實現。

至於發送郵件,不推薦初學者使用 smtplib,推薦使用 djangomail,具體方法見前文最簡單的方式發送郵件,讓程序出錯自動發郵件

今天分享如何使用 poplib、imaplib 來接收郵件。

你說這兩個都可以用來收郵件,到底用哪一個呢? 先看下他們的區別。

POP3 與 IMAP 的區別

POP3 協議是 Post Office Protocol 3 的簡稱,即郵局協議的第 3 個版本,是 TCP/IP 協議族中的一員,默認端口是110。本協議主要用於支持使用客戶端遠程管理在服務器上的電子郵件。

IMAP 全稱是 Internet Mail Access Protocol,即交互式郵件訪問協議,是一個應用層協議,端口是 143。用來從本地郵件客戶端訪問遠程服務器上的郵件。

POP3 工作在傳輸層,而 IMAP 工作中應用層,從這一點來看,IMAP 更爲高級,事實上正是如此。雖然這兩個協議都是從郵件服務器下載郵件到本地,但是不同的是 IMAP 提供雙向通信,也即在客戶端所作的更改會反饋給服務器端,跟服務器端形成同步,例如刪除郵件,創建文件夾等。而 POP3 是單向通信的,即下載郵件到本地就算了,所作的更改都只是在客戶端,不會反映到服務器端。所以使用 IMAP 協議也會更便捷,體驗更好,更可靠。

因此,如果你希望對郵件的更改同步到服務端,那麼使用 IMAP,否則使用 POP3

POP3 發送郵件

以下面的代碼爲例,我們來獲取最新的一封郵件內容:

import poplib
from email.parser import Parser
from utils import print_info
import settings


# 連接到POP3服務器:
server = poplib.POP3(settings.pop3_server)
# 身份認證:
server.user(settings.email)
server.pass_(settings.password)

# stat()返回郵件數量和佔用空間:
print('Messages: %s. Size: %s' % server.stat())
# list()返回所有郵件的編號:
resp, mails, octets = server.list()
# 可以查看返回的列表類似[b'1 82923', b'2 2184', ...]

# 獲取最新一封郵件, 注意索引號從1開始:
latest_mail_index = len(mails)
resp, lines, octets = server.retr(latest_mail_index)

# lines存儲了郵件的原始文本的每一行,
# 可以獲得整個郵件的原始文本:
msg_content = b'\r\n'.join(lines).decode('utf-8')
# 稍後解析出郵件:
msg = Parser().parsestr(msg_content)
print_info(msg)
# 郵件索引號直接從服務器刪除郵件
# server.dele(index)
# 關閉連接:
server.quit()

執行結果如下:


poplib 收取郵件分兩步:第一步是獲取郵件列表,第二步是用 email 模塊把原始郵件解析爲 Message 對象,然後,用適當的形式把郵件內容展示出來。print_info 函數的邏輯比較複雜,放在了 utils.py 中,完整代碼見文末的鏈接。

基於 poplib 的三方庫

使用完標準庫 poplib,也使用過三方庫 zmail,我只想說,還是三方庫用起來爽。

zmail

Zmail 使得在 Python3 中發送和接受郵件變得更簡單。你不需要手動添加服務器地址、端口以及適合的協議,zmail 會幫你完成。此外,使用一個字典來代表郵件內容也更符合直覺。

Zmail 僅支持 Python3,不依賴任何三方庫。安裝方法:

pip install zmail

特性:

  • 自動尋找服務器地址以及端口
  • 自動使用可靠的鏈接協議
  • 自動將一個python字典映射成MIME對象(帶有附件的)
  • 自動添加頭文件以及localhostname來避免服務器拒收你的郵件
  • 輕鬆自定義你的頭文件
  • 支持使用HTML作爲郵件內容
  • 僅需 python>=3.5,你可以將其嵌入你的項目而無需其他的依賴

示例代碼:

import zmail
server = zmail.server('[email protected]', 'yourpassword')

# Send mail
server.send_mail('[email protected]',{'subject':'Hello!','content_text':'By zmail.'})
# Or to a list of friends.
server.send_mail(['[email protected]','[email protected]'],{'subject':'Hello!','content_text':'By zmail.'})

# Retrieve mail
latest_mail = server.get_latest()
zmail.show(latest_mail)

可以看出,接收最新的郵件只需要兩行代碼:

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

執行結果如下:

很簡潔,很好用。

文檔:https://github.com/zhangyunhao116/zmail/blob/master/README-cn.md

imap 接收郵件

很多主流郵箱如 163,qq 郵箱默認關閉了 imap 的服務,可手動前往郵箱賬戶設置頁面開啓,並生成授權碼,授權碼就是代碼中用於登錄的密碼。

獲取最新的郵件並展示:

import imaplib
import email  #導入兩個庫
import settings
from utils import print_info

M = imaplib.IMAP4_SSL(host = settings.imap_server)
print('已連接服務器')
M.login(settings.email,settings.password)
print('已登陸')
print(M.noop())
M.select()

typ, data = M.search(None, 'ALL')
for num in data[0].split():
    typ, data = M.fetch(num, '(RFC822)')
    # print('Message %s\n%s\n' % (num, data[0][1]))
    # print(data[0][1].decode('utf-8'))
    msg = email.message_from_string(data[0][1].decode('utf-8'))
    print_info(msg)
    break
M.close()
M.logout()

運行結果如下:

基於 imaplib 的三方庫

你可能會問:爲什麼要爲 Python 創建另一個 IMAP 客戶端庫?Python 標準庫不是已經有 imaplib 了嗎?。

imaplib 的問題在於它非常底層。 使用起來相當複雜,你可能需要處理很多細節問題,由於 IMAP 服務器響應可能非常複雜,這意味着使用 imaplib 的每個人最終都會編寫自己的脆弱解析程序。

此外,imaplib 沒有很好地利用異常。 這意味着您需要檢查 imaplib 的每次調用的返回值,以查看請求是否成功。下面推薦兩個常用的三方庫。

imapclient

imapclient 在內部使用的 imaplib,但比 imaplib 好用的多,示例代碼如下:

import ssl
from imapclient import IMAPClient
import settings
# context manager ensures the session is cleaned up


ssl_context = ssl.create_default_context()
# don't check if certificate hostname doesn't match target hostname
ssl_context.check_hostname = False
# don't check if the certificate is trusted by a certificate authority
ssl_context.verify_mode = ssl.CERT_NONE

with IMAPClient(host=settings.imap_server,ssl_context=ssl_context) as client:
    client.login(settings.account,settings.password)
    select_info = client.select_folder('INBOX')
    print('%d messages in INBOX' % select_info[b'EXISTS'])
    # search criteria are passed in a straightforward way
    # (nesting is supported)
    messages = client.search(['FROM', '[email protected]'])
    # `response` is keyed by message id and contains parsed,
    # converted response items.
    for message_id, data in client.fetch(messages, ['ENVELOPE']).items():
        envelope = data[b'ENVELOPE']
        print('{id}: subject: {subject} date: {date}'.format(
            id=message_id,
            subject = envelope.subject.decode(),
            date = envelope.date
        ))

文檔: https://github.com/mjs/imapclient

imap_tools

通過 IMAP 處理電子郵件和郵箱,支持以下功能:

  • 解析的電子郵件消息屬性
  • 用於搜索電子郵件的查詢生成器
  • 使用電子郵件的操作:複製、刪除、標記、移動、看到、追加
  • 使用文件夾的操作:列表、設置、獲取、創建、存在、重命名、刪除、狀態
  • 沒有依賴項
pip install imap-tools

示例代碼:

from imap_tools import MailBox, AND

# get list of email subjects from INBOX folder
with MailBox('imap.mail.com').login('[email protected]', 'pwd') as mailbox:
    subjects = [msg.subject for msg in mailbox.fetch()]

# get list of email subjects from INBOX folder - equivalent verbose version
mailbox = MailBox('imap.mail.com')
mailbox.login('[email protected]', 'pwd', initial_folder='INBOX')  # or mailbox.folder.set instead 3d arg
subjects = [msg.subject for msg in mailbox.fetch(AND(all=True))]
mailbox.logout()

文檔:https://github.com/ikvk/imap_tools

最後的話

完整示例代碼:https://github.com/somenzz/tutorial/tree/master/email

使用標準庫有助於我們加深對郵件協議細節的理解,而三方庫卻可以不用考慮過多細節,直接上手,標準庫相當於手動擋,三方庫相當於自動擋,具體用哪個,選擇最適合自己的就好。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章