获取电子邮件未读内容,而不会影响未读状态

时间:2009-10-14 04:27:33

标签: python gmail imap pop3 imaplib

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

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

哪种访问方式最容易实现?如果IMAP会影响读取状态,我找不到任何信息,但似乎我可以手动将消息重置为未读。按定义弹出不会影响未读状态,但似乎没有人使用pop访问他们的Gmail,为什么?

6 个答案:

答案 0 :(得分:5)

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

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

就像fyi一样......这里是有关RFC标志的相关部分:

系统标志是在此预定义的标志名称    规格。所有系统标志都以“\”开头。某些系统    flags(\ 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.

答案 1 :(得分:3)

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

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

答案 2 :(得分:2)

使用BODY.PEEK时需要指定部分。章节在BODY [< section>]<< partial>>下的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 [])

答案 3 :(得分:1)

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

答案 4 :(得分:0)

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

答案 5 :(得分:0)

要跟进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()