我正在开发一个从电子邮件收集数据的功能,并有一个开关来将消息标记为不可见。在开发过程中它开始失败我不知道为什么。我在文档中查找过它,我搜索过 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 标志,并且不允许将电子邮件标记为 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.

关于Python imaplib 不会将消息标记为不可见,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38744885/

10-12 21:17