欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python+imbox庫(kù)實(shí)現(xiàn)郵件讀取與刪除和附件下載

 更新時(shí)間:2025年02月08日 16:14:09   作者:覓遠(yuǎn)  
這篇文章主要為大家詳細(xì)介紹了Python如何使用imbox庫(kù)實(shí)現(xiàn)郵件讀取與刪除以及附件下載,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下

獲取qq郵箱授權(quán)碼參考 Python+smtplib庫(kù)實(shí)現(xiàn)郵件發(fā)送功能

imbox.messages() 參數(shù):

    inbox_messages = imbox.messages(unread=True)  # 未讀郵件
    inbox_messages = imbox.messages(unread=False)  # 已讀郵件
    inbox_messages = imbox.messages(flagged=True)  # 已標(biāo)記郵件
    
    inbox_messages = imbox.messages(date__lt=datetime(2025, 2, 7))  # 獲取指定時(shí)間的郵件,某天以前的數(shù)據(jù)
    inbox_messages = imbox.messages(date__on=datetime(2025, 2, 7))  # 獲取指定時(shí)間的郵件,某天的數(shù)據(jù)
    inbox_messages = imbox.messages(date__gt=datetime(2025, 2, 7))  # 獲取指定時(shí)間的郵件,某天以后的數(shù)據(jù)

完整代碼

from datetime import datetime
from imbox import Imbox
import html
 
# 登錄憑證
username = "2xx85555ss@qq.com"
password = "xxxxxxx"
 
with Imbox('imap.qq.com', username, password, ssl=True) as imbox:
    inbox_messages = imbox.messages()  # 獲取全部郵件
    for uid, message in inbox_messages:
        subject = message.subject  # 郵件主題
        from_ = message.sent_from  # 發(fā)件人
        to_ = message.sent_to  # 收件人
        date = message.date  # 發(fā)送日期
 
        # print(message.body['plain'])  # 郵件文本格式正文
        # 處理郵件正文
        body = ''
        if 'plain' in message.body:
            # 提取正文內(nèi)容(列表中的第一個(gè)元素)
            if message.body['plain']:
                body = message.body['plain'][0]
                # 解碼 Unicode 編碼
                body = body.encode().decode('unicode_escape')
                # 解碼 HTML 實(shí)體
                body = html.unescape(body)
        else:
            print("No plain text body found.")
 
        html_body = message.body['html']  # html格式正文
 
        # print(message.attachments)  # 附件信息
        attachment_names = []
        for attachment in message.attachments:
            file_name = attachment.get('filename')
            attachment_names.append(file_name)
 
        print(
            f'主題:{subject}\n發(fā)件人:{from_}\n收件人:{to_}\n時(shí)間:{date}\n文本內(nèi)容:{body}\nHTML內(nèi)容:{html_body}\n附件:{attachment_names}\n')

print(message.body['plain']) 輸出結(jié)果是 Unicode 編碼的字符串列表,而不是直接解析后的文本。這是因?yàn)?imbox 返回的郵件正文內(nèi)容可能是以列表形式存儲(chǔ)的,并且可能包含 HTML 實(shí)體或 Unicode 編碼。

解決方案

提取列表中的字符串:message.body['plain'] 返回的是一個(gè)列表,你需要提取列表中的字符串。

解碼 Unicode 編碼:使用 Python 的內(nèi)置功能(如 .encode().decode('unicode_escape'))將 Unicode 編碼的字符串轉(zhuǎn)換為可讀文本。

處理 HTML 實(shí)體:如果郵件正文包含 HTML 實(shí)體(如 &nbsp; 或 <a> 標(biāo)簽),可以使用 html 模塊或第三方庫(kù)(如 BeautifulSoup)來解析。

解決問題的代碼:

        body = ''
        if 'plain' in message.body:
            # 提取正文內(nèi)容(列表中的第一個(gè)元素)
            if message.body['plain']:
                body = message.body['plain'][0]
                # 解碼 Unicode 編碼
                body = body.encode().decode('unicode_escape')
                # 解碼 HTML 實(shí)體
                body = html.unescape(body)
        else:
            print("No plain text body found.")

下載附件

        attachment_names = []
        for attachment in message.attachments:
            file_name = attachment.get('filename')
            attachment_names.append(file_name)
            with open(file_name, 'wb') as f:
                f.write(attachment['content'].getvalue())

標(biāo)記郵件

可根據(jù)郵件內(nèi)容或主題等進(jìn)行標(biāo)記

        imbox.mark_seen(uid)   # 設(shè)為已讀
        imbox.mark_flag(uid)  # 標(biāo)記

刪除郵件

可根據(jù)郵件內(nèi)容或主題等進(jìn)行刪除

        if '消息推送審核駁回的提醒' in subject and '2025 17:29' in date:
            imbox.delete(uid)  # 刪除

以上就是Python+imbox庫(kù)實(shí)現(xiàn)郵件讀取與刪除和附件下載的詳細(xì)內(nèi)容,更多關(guān)于Python imbox郵件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論