Skip to main content
 首页 » 编程设计

特定日期后的 Python Gmail IMAP

2025年05月04日24myhome

我正在尝试使用 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'] 

请您参考如下方法:

任何人都在寻找这个问题的答案,有一个网页对我有帮助:https://automatetheboringstuff.com/chapter16/ .

从某个日期开始检查的代码:

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

月份需要是月份的大写缩写。例如,Dec 代表十二月。