【问题标题】:Python imaplib doesnt mark messages as unseenPython imaplib 不会将消息标记为看不见
【发布时间】:2016-12-09 06:02:01
【问题描述】:

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

【问题讨论】:

  • 最近和imaplib一起工作,我可以确认SO问题的状态非常可怜。
  • mail.store(uid, '-FLAGS', '(\\SEEN)') 的输出是什么?
  • 不,这行不通。
  • “将不起作用” - 这是否意味着它会产生错误?什么都不生产?因为mail.store 应该 会产生结果。你可以做print(mail.store(uid, '-FLAGS', '(\\SEEN)'),它应该产生something,即使那是一个回溯。如果什么都没有发生,那么就会发生一些非常奇怪的事情。
  • 我的意思是结果是一个元组('OK', [None])并且邮箱中的邮件仍然标记为已读。

标签: python imaplib


【解决方案1】:

我相信你想要

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.

【讨论】:

  • 是的,这正是我想要做的。将电子邮件标记为UNSEEN。我以为标题很清楚,我现在就更正一下。
  • 那么,您尝试过 -FLAGS (\\Seen) 吗?您没有使用原始字符串,因此您需要转义反斜杠。
猜你喜欢
  • 2013-06-26
  • 2011-08-27
  • 2021-08-31
  • 2019-05-05
  • 2011-01-16
  • 2017-10-03
  • 2017-12-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多