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

标签 python imaplib

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

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

相关文章:

python - 在列表中查找组合的更优雅的方法是什么?

python - 复制后如何唯一标识 IMAP 邮件?

python - 使用 imaplib 获取电子邮件主题和发件人?

Python Tkinter 多线程函数

python - imaplib.错误 : command FETCH illegal in state AUTH

python - 如何使用 imaplib 创建电子邮件并将其发送到特定邮箱

python - Tkinter 标签不显示图像

python - 通过数据框进行高效搜索

python 循环使用 JSON,而不是手动打印出每个匹配项

python - 属性错误 : 'Tokenizer' object has no attribute 'oov_token' in Keras