Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取电子邮件未读内容,而不影响未读状态_Python_Gmail_Imap_Pop3_Imaplib - Fatal编程技术网

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

Python 获取电子邮件未读内容,而不影响未读状态,python,gmail,imap,pop3,imaplib,Python,Gmail,Imap,Pop3,Imaplib,现在它是一个gmail邮箱,但迟早我希望它能扩展 我想在其他地方同步实时个人邮箱(收件箱和发件箱)的副本,但我不想影响任何未读邮件的未读状态 什么类型的访问将使这变得最容易?我找不到IMAP是否会影响读取状态的任何信息,但似乎我可以手动将消息重置为未读。Pop按定义并不影响未读状态,但似乎没有人使用Pop访问他们的gmail,为什么?没有人使用Pop,因为他们通常需要IMAP的额外功能,例如跟踪邮件状态。当这个功能只是在妨碍你,需要解决时,我认为使用POP是你最好的选择!) 在IMAP世界中,每

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

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


什么类型的访问将使这变得最容易?我找不到IMAP是否会影响读取状态的任何信息,但似乎我可以手动将消息重置为未读。Pop按定义并不影响未读状态,但似乎没有人使用Pop访问他们的gmail,为什么?

没有人使用Pop,因为他们通常需要IMAP的额外功能,例如跟踪邮件状态。当这个功能只是在妨碍你,需要解决时,我认为使用POP是你最好的选择!)

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

大多数邮件客户端在读取邮件时都会应用\Seen标志。因此,如果消息已在应用程序之外读取,则需要删除\Seen标志

仅供参考……以下是有关RFC标志的相关部分:

系统标志是在此文件中预定义的标志名称 规范。所有系统标志都以“\”开头。某系统 标志(\Deleted and\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.

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


查看并向下滚动到第57页或搜索“BODY.PEEK”。

如果它对任何人都有帮助,GAE允许您这样做,所以现在我只是在那里转发电子邮件。

在python中,使用“.PEEK”选项的语法是调用并传递它

要将此应用于中的示例,请执行以下操作:


使用BODY.PEEK时需要指定节。章节在正文[]下的文件中解释


PS:我想修复给出的答案,但不允许,因为编辑少于6个字符(BODY.PEEK->BODY.PEEK[])

如果您需要文件夹或并发访问,POP是完全不合适的-当您的常规交互客户端已连接时,您的程序无法连接,反之亦然;或者,在最坏的情况下,您可以这样做,但服务器不知道如何处理您,并且执行了超出您希望和预期的操作。使用
PEEK
无需直接操作标志。
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()
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()