Skip to main content
 首页 » 编程设计

Python imaplib 不会将消息标记为看不见

2024年10月17日33unruledboy

我正在开发一个从电子邮件中收集数据的功能,并有一个开关可以将消息标记为未看到。在开发过程中它开始失败我不知道为什么。我在文档中查找了它,我搜索了 stackoverflow(找到了 this 线程,但它没有帮助)。反正。这是代码:

    mail = imaplib.IMAP4_SSL('imap.gmail.com', '993') 
    mail.login(settings.INVOICES_LOGIN, settings.INVOICES_PASSWORD) 
    mail.select('inbox') 
 
    result, data = mail.uid('search', '(UNSEEN)', 'X-GM-RAW', 
                            'SUBJECT: "{0}" FROM: "{1}"'.format(attachment_subject, attachment_from)) 
    uids = data[0].split() 
    for uid in uids: 
        result, data = mail.uid('fetch', uid, '(RFC822)') 
        m = email.message_from_string(data[0][1]) 
 
        if m.get_content_maintype() == 'multipart': 
            for part in m.walk(): 
                if part.get_content_maintype() == 'multipart': 
                    continue 
                if part.get('Content-Disposition') is None: 
                    continue 
                if re.match(attachment_filename_re, part.get_filename()): 
                    attachments.append({'uid': uid, 'data': part.get_payload(decode=True)}) 
 
        if set_not_read: 
            mail.store(uid, '-FLAGS', '(\Seen)') 

我已经调试过了,我确信使用这个标志 mail.store(uid, '-FLAGS', '(\Seen)') 部分被输入了,我也尝试切换到 \SEEN 和 \Seen 而不是 (\Seen)。

编辑:

我想做的是制作一个脚本,允许用户将电子邮件标记为 unseen(未读),这会重置 Seen 标志,并且不允许它将电子邮件标记为已读(已读)。

请您参考如下方法:

我相信你想要

mail.store(uid, '+FLAGS', '(\\Seen)') 

我认为您现在正在做的是移除看到的旗帜。但我会查看 RFC 以确保确定。

编辑:是的。这就是RFC says

-FLAGS <flag list> 
         Remove the argument from the flags for the message.  The new 
         value of the flags is returned as if a FETCH of those flags was 
         done. 

您可能会发现相关的其他位:

The currently defined data items that can be stored are: 
 
      FLAGS <flag list> 
         Replace the flags for the message (other than \Recent) with the 
         argument.  The new value of the flags is returned as if a FETCH 
         of those flags was done. 
 
      FLAGS.SILENT <flag list> 
         Equivalent to FLAGS, but without returning a new value. 
 
      +FLAGS <flag list> 
         Add the argument to the flags for the message.  The new value 
         of the flags is returned as if a FETCH of those flags was done. 
 
      +FLAGS.SILENT <flag list> 
         Equivalent to +FLAGS, but without returning a new value. 
 
      -FLAGS <flag list> 
         Remove the argument from the flags for the message.  The new 
         value of the flags is returned as if a FETCH of those flags was 
         done. 
 
      -FLAGS.SILENT <flag list> 
         Equivalent to -FLAGS, but without returning a new value.