Imaplib / gmail how to download the full message (all parts) until marking read

I accidentally marked all the messages in my mailbox as read with this python statement:

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

But I managed to go through all parts of the message with the following set of statuses:

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

Exit:

 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 expression:

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

that I will not mark all my posts read. However, I would also not receive all 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 can I get all parts of a message without marking my messages as read? Thanks.

+4
source share
4 answers

If you want only headers, but still want the message to remain marked as unread (UNSEEN), it requires two commands to retrieve and save:

 # 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') 
+1
source

You can also open the mailbox in readonly mode. select (folder, readonly = True)

+3
source

I assume that if you just try enough combinations, you will find the answer:

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

Along the way, I found a lot of information in the RFC 1730 manual .

+2
source

I think I'm talking to myself, formally. :)

I think I really found the answer this time:

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

It does everything that I was looking for. It does not mark the message as read (see), and it retrieves all parts of the message.

Looking at the RFC 1730 manual, it seemed like this should work:

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

but it also caused an error.

+2
source

All Articles