【问题标题】:get email unread content, without affecting unread state [duplicate]获取电子邮件未读内容,而不影响未读状态[重复]
【发布时间】:2023-04-02 03:25:01
【问题描述】:

现在它是一个 gmail 邮箱,但迟早我希望它能够扩展。

我想在其他地方同步实时个人邮箱(收件箱和发件箱)的副本,但我不想影响任何未读邮件的unread 状态。

什么类型的访问会使这变得最简单?如果 IMAP 会影响阅读状态,我找不到任何信息,但似乎我可以手动将邮件重置为未读。根据定义,pop 不会影响未读状态,但似乎没有人使用 pop 访问他们的 gmail,为什么?

【问题讨论】:

    标签: python gmail imap pop3 imaplib


    【解决方案1】:

    没有人使用 POP,因为他们通常想要 IMAP 的额外功能,例如跟踪消息状态。当该功能只会妨碍您并需要解决方法时,我认为使用 POP 是您的最佳选择!-)

    【讨论】:

    • POP 完全不适合如果您需要文件夹或并发访问——当您的常规交互式客户端已经连接时,您的程序无法连接,反之亦然;或者,在最坏的情况下,您可以,但服务器不知道如何处理您,并且做了一些超出您想要和预期的事情。
    【解决方案2】:

    在 IMAP 世界中,每条消息都有标志。您可以在每条消息上设置单独的标志。当您获取消息时,实际上可以读取消息,而无需应用 \Seen 标志。

    大多数邮件客户端将在阅读邮件时应用 \Seen 标志。因此,如果消息已经在您的应用程序之外被阅读,那么您将需要删除 \Seen 标志。

    仅供参考...这里是有关 RFC 中标志的相关部分:

    系统标志是在此预定义的标志名称 规格。所有系统标志都以“\”开头。某些系统 标志(\Deleted 和 \Seen)具有描述的特殊语义 别处。当前定义的系统标志是:

        \Seen
           Message has been read
    
        \Answered
           Message has been answered
    
        \Flagged
           Message is "flagged" for urgent/special attention
    
        \Deleted
           Message is "deleted" for removal by later EXPUNGE
    
        \Draft
           Message has not completed composition (marked as a draft).
    
        \Recent
           Message is "recently" arrived in this mailbox.  This session
           is the first session to have been notified about this
           message; if the session is read-write, subsequent sessions
           will not see \Recent set for this message.  This flag can not
           be altered by the client.
    
           If it is not possible to determine whether or not this
           session is the first session to be notified about a message,
           then that message SHOULD be considered recent.
    
           If multiple connections have the same mailbox selected
           simultaneously, it is undefined which of these connections
           will see newly-arrived messages with \Recent set and which
           will see it without \Recent set.
    

    【讨论】:

    • 使用PEEK 避免了直接操作标志的需要。
    【解决方案3】:

    IMAP 中的 FETCH 命令上有一个 .PEEK 选项,它不会明确设置 /Seen 标志。

    查看the FETCH command in RFC 3501 并向下滚动到第 57 页或搜索“BODY.PEEK”。

    【讨论】:

      【解决方案4】:

      如果它对任何人有帮助,GAE 允许您receive email as an HTTP request,所以现在我只是在那里转发电子邮件。

      【讨论】:

        【解决方案5】:

        要跟进Dan Goldstein's answer above,在python中使用“.PEEK”选项的语法是调用IMAP4.fetch并将其传递给“BODY.PEEK”

        将此应用于python docs 中的示例:

        import getpass, imaplib
        
        M = imaplib.IMAP4()
        M.login(getpass.getuser(), getpass.getpass())
        M.select()
        typ, data = M.search(None, 'ALL')
        for num in data[0].split():
            typ, data = M.fetch(num, '(BODY.PEEK)')
            print 'Message %s\n%s\n' % (num, data[0][5])
        M.close()
        M.logout()
        

        【讨论】:

          【解决方案6】:

          使用 BODY.PEEK 时需要指定截面。部分在 BODY[

          ]>

          下的 IMAP Fetch Command 文档中进行了解释
          import getpass, imaplib
          
          M = imaplib.IMAP4()
          M.login(getpass.getuser(), getpass.getpass())
          M.select()
          typ, data = M.search(None, 'ALL')
          for num in data[0].split():
              typ, data = M.fetch(num, '(BODY.PEEK[])')
              print 'Message %s\n%s\n' % (num, data[0][5])
          M.close()
          M.logout()
          

          PS:我想修复给定Gene Wood 的答案,但由于编辑小于 6 个字符(BODY.PEEK -> BODY.PEEK[])

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2023-03-23
            • 2016-03-12
            • 2020-01-21
            • 2017-01-21
            • 1970-01-01
            • 2020-04-28
            • 2021-06-03
            • 2015-03-16
            相关资源
            最近更新 更多