imaplib / gmail如何在不标记读取的情况下下载完整消息(所有部分) [英] imaplib/gmail how to download full message (all parts) while not marking read

查看:143
本文介绍了imaplib / gmail如何在不标记读取的情况下下载完整消息(所有部分)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无意中将收件箱中的所有邮件标记为使用此python语句读取:

  status,data = conn.uid ('fetch',fetch_uids,'(RFC822)')

但是我能够遍历所有这些消息的部分包含以下一组语句:

  email_message = email.message_from_string(data [0] [1]) 
作为email_message.walk()的一部分:
print'\\\
'
print'Content-Type:',part.get_content_type()
print'Main Content:' ,part.get_content_maintype()
print'Sub Content:',part.get_content_subtype()

输出:

 内容类型:multipart / mixed 
主要内容:multipart
子内容:混合


内容类型:multipart / alternative
主要内容:multipart
子内容:alternative


Content-类型:text / plain
主要内容:text
子内容:plain


Content-Type:text / html
主要内容:text
Sub Content:html

我发现如果我使用这个语句:

  status,data = conn。 uid('fetch',fetch_uids,'(RFC822.HEADER BODY.PEEK [1])')

我不会标记我读的所有消息。然而,我也不会收到消息的所有部分:

 内容类型:multipart / mixed 
主要内容:multipart
Sub Content:mixed

我试着阅读imaplib < here ,但没有提到peek这个词。我的问题是,如何获取消息的所有部分,而不将我的消息标记为已读?如果你只想要标题,但仍然希望留言被标记为未读(UNSEEN),那就需要这样做。

两个命令获取并存储:

 #获取未看到消息的uids 
结果,uids = conn.uid('搜索',None,'(UNSEEN)')

#将这些uid转换为逗号分隔列表
fetch_ids =','。join(uids [0] .split())

#首先获取标题,这会标记它们读取(SEEN)
status,headers = conn.uid('fetch',fetch_ids,'(RFC822.HEADER)')

#现在标记每条消息未读(UNSEEN)
status1,flags = conn.uid('store',fetch_ids,' - FLAGS','\\Seen')


I inadvertently marked all the messages in my inbox as read with this python statement:

status, data = conn.uid('fetch', fetch_uids, '(RFC822)')

But I was able to walk through all the parts of the message with the following set of statments:

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()

The output:

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

I found that if I used this statement instead:

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

that I wouldn't mark all of my messages read. However, I also wouldn't get all the parts of the message:

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

I tried to read the manual for imaplib here, but the word "peek" is not mentioned. My question is, how do I get all the parts of the message while not marking my messages as read? Thanks.

解决方案

If you want just the headers, but still want the message to be left marked unread (UNSEEN), it requires two commands fetch and then 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')

这篇关于imaplib / gmail如何在不标记读取的情况下下载完整消息(所有部分)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆