简体   繁体   English

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

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

The following code uses IMAP to log into an Outlook email and checks the contents of the Junk folder.以下代码使用IMAP登录到 Outlook email 并检查Junk文件夹的内容。

It returns the content of the last unseen email with subject "Title" .它返回主题为"Title"的最后一个看不见的 email 的内容。

I would like to add one thing to the source code.我想在源代码中添加一件事。

Is it possible to get the last email of for example the last 10 minutes?是否有可能获得例如最后 10 分钟的最后 email? So that he doesn't return older emails.这样他就不会返回旧电子邮件。

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)

There is no search by time in IMAP REF: https://www.rfc-editor.org/rfc/rfc3501#section-6.4.4 IMAP 中没有按时间搜索 REF: https://www.rfc-editor.org/rfc/rfc3501#section-6.4.4

Anyway you can do it:无论如何你可以这样做:

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 , I am author https://github.com/ikvk/imap_tools , 我是作者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM