首頁 > 軟體

Python實現郵件自動下載的範例詳解

2022-07-14 22:02:28

開始碼程式碼之前,我們先來了解一下三種郵件服務協定:

1、SMTP協定

SMTP(Simple Mail Transfer Protocol),即簡單郵件傳輸協定。相當於中轉站,將郵件傳送到使用者端。

2、POP3協定

POP3(Post Office Protocol 3),即郵局協定的第3個版本,是電子郵件的第一個離線協定標準。該協定把郵件下載到本地計算機,不與伺服器同步,缺點是更易丟失郵件或多次下載相同的郵件。

3、IMAP協定

IMAP(Internet Mail Access Protocol),即互動式郵件存取協定。該協定連線遠端郵箱直接操作,與伺服器內容同步。

然後介紹一下email包

這個包的中心元件是代表電子郵件訊息的“物件模型”。 應用程式主要通過在 message 子模組中定義的物件模型介面與這個包進行互動。 應用程式可以使用此 API 來詢問有關現有電子郵件的問題、構造新的電子郵件,或者新增或移除自身也使用相同物件模型介面的電子郵件子元件。 也就是說,遵循電子郵件訊息及其 MIME 子元件的性質,電子郵件物件模型是所有提供 EmailMessage API 的物件所構成的樹狀結構。

接下來我們通過具體的程式碼實現一個登入郵箱使用者端,下載郵件,解析郵件附件內容的功能。

首先我們需要定義一個郵件解析的類,該類需要三個變數:

1、郵箱所屬的imap服務地址;

2、郵箱賬號;

3、郵箱密碼【注:不同郵箱需要不同的安全策略,例如qq郵箱需要簡訊驗證,獲取登入授權碼,而不是明文密碼去登入遠端使用者端】

class Email_parse:

    def __init__(self,remote_server_url,email_url,password):
    	# imap服務地址
        self.remote_server_url = remote_server_url
        # 郵箱賬號
        self.email_url = email_url
        # 郵箱密碼
        self.password = password

然後定義類中入口函數,登入遠端,預設獲取第一頁所有的郵件。我們獲取郵件的主題,並列印出來【不同郵件主題的編碼可能不同,二進位制需要轉碼才能正確顯示】

    def main_parse_Email(self):
        """入口函數,登入imap服務"""
        server = imaplib.IMAP4_SSL(self.remote_server_url, 993)
        server.login(self.email_url, self.password)
        server.select('INBOX')
        status,data = server.search(None,"ALL")
        if status != 'OK':
            raise Exception('read email error')
        emailids = data[0].split()
        mail_counts = len(emailids)
        print("count:",mail_counts)
        # 郵件的遍歷是按時間從後往前,這裡我們選擇最新的一封郵件
        for i in range(mail_counts - 1, mail_counts - 2, -1):
            status, edata = server.fetch(emailids[i], '(RFC822)')
            msg = email.message_from_bytes(edata[0][1])
            #獲取郵件主題title
            subject = email.header.decode_header(msg.get('subject'))
            if type(subject[-1][0]) == bytes:
                title = subject[-1][0].decode(str(subject[-1][1]))
            elif type(subject[-1][0]) == str:
                title = subject[-1][0]
            print("title:", title)

其中,msg變數儲存的就是郵件的主體,接下來因為會重複用到msg和tilte,我們將構造一個類函數返回msg和title。

    def get_email_title(msg):
        subject = email.header.decode_header(msg.get('subject'))
        if type(subject[-1][0]) == bytes:
            title = subject[-1][0].decode(str(subject[-1][1]))
        elif type(subject[-1][0]) == str:
            title = subject[-1][0]
        print("title:", title)
        return title

解析郵件,我們分為兩部分,郵件正文【HTML】和附件【xlsx等】,判斷有附件,我們就儲存到固定的路徑下。表格的解析不再贅述了,pandas之類的包足以搞定。

    def get_att(msg):
        """獲取附件並下載"""
        filename = Email_parse.get_email_name(msg)
        for part in msg.walk():
            file_name = part.get_param("name")
            if file_name:
                data = part.get_payload(decode=True)
                if data != None:
                    att_file = open('./src/' + filename, 'wb')
                    att_file.write(data)
                    att_file.close()
                else:
                    pass

郵件正文內容,我們直接解析html,將文字內容直接儲存到.txt檔案中,方便讀取。

    def get_text_from_HTML(msg):
        """獲取郵件中的html"""
        filename = Email_parse.get_email_name(msg)
        current_title = Email_parse.get_email_title(msg)
        print("filename:",filename,type(filename))
        for part in msg.walk():
            if not part.is_multipart():
                result = part.get_payload(decode=True)
                result = result.decode('gbk')
                f = open(f'./src/{current_title}.txt','w')
                f.write(result)
                f.close()
                return result

完整程式碼如下:

import email
import imaplib
from email.header import decode_header
import pandas as pd
import datetime


class Email_parse:
    def __init__(self,remote_server_url,email_url,password):
        self.remote_server_url = remote_server_url
        self.email_url = email_url
        self.password = password

    def get_att(msg):
        filename = Email_parse.get_email_name(msg)
        for part in msg.walk():
            file_name = part.get_param("name")
            if file_name:
                data = part.get_payload(decode=True)
                if data != None:
                    att_file = open('./src/' + filename, 'wb')
                    att_file.write(data)
                    att_file.close()
                else:
                    pass

    def get_email_title(msg):
        subject = email.header.decode_header(msg.get('subject'))
        if type(subject[-1][0]) == bytes:
            title = subject[-1][0].decode(str(subject[-1][1]))
        elif type(subject[-1][0]) == str:
            title = subject[-1][0]
        print("title:", title)
        return title


    def get_email_name(msg):
        for part in msg.walk():
            file_name = part.get_param("name")
            if file_name:
                h = email.header.Header(file_name)
                dh = email.header.decode_header(h)
                filename = dh[0][0]
                if dh[0][1]:
                    value, charset = decode_header(str(filename, dh[0][1]))[0]
                    if charset:
                        filename = value.decode(charset)
                        print("附件名稱:", filename)
                        return filename


    def main_parse_Email(self):
        server = imaplib.IMAP4_SSL(self.remote_server_url, 993)
        server.login(self.email_url, self.password)
        server.select('INBOX')
        status,data = server.search(None,"ALL")
        if status != 'OK':
            raise Exception('read email error')
        emailids = data[0].split()
        mail_counts = len(emailids)
        print("count:",mail_counts)
        for i in range(mail_counts - 1, mail_counts - 2, -1):
            status, edata = server.fetch(emailids[i], '(RFC822)')
            msg = email.message_from_bytes(edata[0][1])
            subject = email.header.decode_header(msg.get('subject'))
            if type(subject[-1][0]) == bytes:
                title = subject[-1][0].decode(str(subject[-1][1]))
            elif type(subject[-1][0]) == str:
                title = subject[-1][0]
            print("title:", title)
            Email_parse.get_att(msg)
            Email_parse.get_text_from_HTML(msg)


    def get_text_from_HTML(msg):
        filename = Email_parse.get_email_name(msg)
        current_title = Email_parse.get_email_title(msg)
        print("filename:",filename,type(filename))
        for part in msg.walk():
            if not part.is_multipart():
                result = part.get_payload(decode=True)
                result = result.decode('gbk')
                f = open(f'./src/{current_title}.txt','w')
                f.write(result)
                f.close()
                return result

if __name__ == "__main__":
    remote_server_url = 'imap.qq.com'
    email_url = "*********@qq.com"
    password = "**********"
    demo = Email_parse(remote_server_url,email_url,password)
    demo.main_parse_Email()

執行結果:

以上就是Python實現郵件自動下載的範例詳解的詳細內容,更多關於Python郵件自動下載的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com