簡體   English   中英

使用 IMAP-TOOLS 搜索最后 N 分鍾的 Email

[英]Search Email Of Last N Minutes Using IMAP-TOOLS

以下代碼使用IMAP登錄到 Outlook email 並檢查Junk文件夾的內容。

它返回主題為"Title"的最后一個看不見的 email 的內容。

我想在源代碼中添加一件事。

是否有可能獲得例如最后 10 分鍾的最后 email? 這樣他就不會返回舊電子郵件。

from imap_tools import MailBox, AND

with MailBox('imap.outlook.com').login(email, password, 'Junk') as mailbox:
    for msg in mailbox.fetch(AND(subject = f'title', seen = False), limit = 1, reverse = True):
        body = msg.text
    for line in body.splitlines():
        print(line)

IMAP 中沒有按時間搜索 REF: https://www.rfc-editor.org/rfc/rfc3501#section-6.4.4

無論如何你可以這樣做:

import datetime
from imap_tools import MailBox, A

# get emails that have been received since a certain time
with MailBox('imap.mail.com').login('test@mail.com', 'p', 'INBOX') as mailbox:
    for msg in mailbox.fetch(A(date_gte=datetime.date(2000, 1, 1))):
        print(msg.date.time(), 'ok' if msg.date.hour > 8 else '')
    

https://github.com/ikvk/imap_tools , 我是作者

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM