Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.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 imaplib/gmail如何在不标记为read的情况下下载完整信息(所有部分)_Python_Gmail_Imaplib - Fatal编程技术网

Python imaplib/gmail如何在不标记为read的情况下下载完整信息(所有部分)

Python imaplib/gmail如何在不标记为read的情况下下载完整信息(所有部分),python,gmail,imaplib,Python,Gmail,Imaplib,我无意中用以下python语句将收件箱中的所有邮件标记为已读: status, data = conn.uid('fetch', fetch_uids, '(RFC822)') 但我能够通过以下一组陈述来浏览信息的所有部分: email_message = email.message_from_string(data[0][1]) for part in email_message.walk(): print '\n' print 'Content-Type:',part.get_co

我无意中用以下python语句将收件箱中的所有邮件标记为已读:

status, data = conn.uid('fetch', fetch_uids, '(RFC822)')
但我能够通过以下一组陈述来浏览信息的所有部分:

email_message = email.message_from_string(data[0][1])
for part in email_message.walk():
  print '\n'
  print 'Content-Type:',part.get_content_type()
  print 'Main Content:',part.get_content_maintype()
  print 'Sub Content:',part.get_content_subtype()
输出:

Content-Type: multipart/mixed
Main Content: multipart
Sub Content: mixed


Content-Type: multipart/alternative
Main Content: multipart
Sub Content: alternative


Content-Type: text/plain
Main Content: text
Sub Content: plain


Content-Type: text/html
Main Content: text
Sub Content: html
我发现如果我用这句话来代替:

status, data = conn.uid('fetch', fetch_uids, '(RFC822.HEADER BODY.PEEK[1])')
我不会把我所有的信息都标记为已读。然而,我也不会得到信息的所有部分:

Content-Type: multipart/mixed
Main Content: multipart
Sub Content: mixed

我试图阅读imaplib的手册,但没有提到“peek”这个词。我的问题是,如何在不将邮件标记为已读的情况下获取邮件的所有部分?谢谢。

我想如果你继续尝试足够的组合,你会找到你的答案:

status, data = conn.uid('fetch', fetch_ids, '(RFC822 BODY.PEEK[])')

一路上,我在

中发现了很多信息,我想我是在以一种正式的方式自言自语。:)

我想这次我真的找到了答案:

status, data = conn.uid('fetch', fetch_ids, '(BODY.PEEK[])')
这就是我想要的一切。它不会将消息标记为已读(已看到),并检索消息的所有部分

看看RFC 1730手册,这似乎应该奏效:

status, data = conn.uid('fetch', fetch_ids, '(RFC822.PEEK BODY)')

但这也产生了一个错误???

如果您只需要标题,但仍希望邮件标记为未读(未看到),则需要两个命令fetch和store:

# get uids of unseen messages
result, uids = conn.uid('search', None, '(UNSEEN)')

# convert these uids to a comma separated list
fetch_ids = ','.join(uids[0].split())

# first fetch the headers, this will mark them read (SEEN)
status, headers = conn.uid('fetch', fetch_ids, '(RFC822.HEADER)')

# now mark each message unread (UNSEEN)
status1, flags = conn.uid('store', fetch_ids,'-FLAGS','\\Seen')

您还可以以只读模式打开邮箱。
选择(folder,readonly=True)

我收回它,它仍然将邮件标记为已读,这令人惊讶,因为我仍然使用
body.peek[]
。1730早已过时。等价物只是
BODY.PEEK[]
。这样做的结果是,页眉与正文并没有分开,我有点喜欢这样。整个页眉与正文一起显示。虽然这可能在有限的设置下工作,但通常的方法是使用只读或peek。任何非原子的东西在协议中都有明显的问题,其中并发访问是必需的特性。这不应该是公认的答案。