首页 > 解决方案 > Python Gmail IMAP 在某个日期之后

问题描述

我正在尝试使用 python IMAP 访问我的 gmail,但我只想在某个日期之后阅读邮件。我已将其缩小到搜索部分,但我在搜索中使用 On 和 before 并且它成功只有 after 不起作用。有没有办法在某个日期之后检索电子邮件,或者我需要全部阅读并比较日期?

typ, data = conn.search(None, "After 12/12/18")

try:
    for num in data[0].split():
        typ, msg_data = conn.fetch(num, '(RFC822)')
        for response_part in msg_data:
            if isinstance(response_part, tuple):
                msg = email.message_from_bytes(response_part[1])
                subject=msg['subject']
                by = msg['from']
                to = msg['to']

错误:

imaplib.IMAP4.error: SEARCH command error: BAD [b'Could not parse command']

标签: pythonpython-3.xgmail-imap

解决方案


任何寻找答案的人都有一个帮助我的网页:https ://automatetheboringstuff.com/chapter16/ 。

从特定日期检查的代码:

typ, data = conn.search(None, "SINCE 12-Dec-2018")

月份必须是月份的大写缩写。例如,Dec 表示 12 月。


推荐阅读