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

时间:2016-08-03 13:21:50

标签: 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而不是(\看)。

编辑:

我尝试做的是创建一个脚本,允许用户将电子邮件标记为unseen(未读取),这会重置Seen标志,而不是允许它将电子邮件标记为seen(已阅读)。

1 个答案:

答案 0 :(得分:2)

我相信你想要

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.