Question

I'm using the following code to export all the emails in a specific gmail folder.

Its working well, in that its pulling out all the emails I expect, but it (or me) seems to be mangle the encoding for CR / newlines.

Code:

import imaplib
import email
import codecs
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('myUser@gmail.com', 'myPassword')  #user / password
mail.list()
mail.select("myFolder") # connect to folder with matching label

result, data = mail.uid('search', None, "ALL") # search and return uids instead
i = len(data[0].split())

for x in range(i):
    latest_email_uid = data[0].split()[x]
    result, email_data = mail.uid('fetch', latest_email_uid, '(RFC822)')
    raw_email = email_data[0][1]
    email_message = email.message_from_string(raw_email)
    save_string = str("C:\\\googlemail\\boxdump\\email_" + str(x) + ".eml") #set to   save location
    myfile = open(save_string, 'a')
    myfile.write(email_message)
    myfile.close()

My problem is that by the time I get to the object its littered with '=0A', which I am assuming are incorrectly interpreted newline or carriage return flag.

I can find it in hex, [d3 03 03 0a] but because this is not 'characters' I can't find a way for str.replace() to take the parts out. I don't actually want the newline flags.

I could convert the whole string to hex, and do a do a replace of sorts / regex thing, but that seems like over kill - when the problem is in the encoding / reading of the source data

What I see:

====
CAUTION:  This email message and any attachments con= tain information that may be confidential and may be LEGALLY PRIVILEGED. If yo= u are not the intended recipient, any use, disclosure or copying of this messag= e or attachments is strictly prohibited. If you have received this email messa= ge in error please notify us immediately and erase all copies of the message an= d attachments. Thank you.
==== 

what I want:

====
CAUTION:  This email message and any attachments contain information that may be confidential and may be LEGALLY PRIVILEGED. If you are not the intended recipient, any use, disclosure or copying of this message or attachments is strictly prohibited. If you have received this email message in error please notify us immediately and erase all copies of the message and attachments. Thank you.
====  
Was it helpful?

Solution

What you are looking at is Quoted Printable encoding.

Try changing:

email_message = email.message_from_string(raw_email)

to:

email_message = str(email.message_from_string(raw_email)).decode("quoted-printable")

For more info see Standard Encodings in the Python codecs module.

OTHER TIPS

Just 2 additional items having gone thought the pain of this for a day. 1 do it at the payload level so you can process your email_message to get email addresses etc from your mail.

2 You need to decode the charset set also, I had trouble with people copying and pasting html from webpages and content from word docs etc into emails that I was then trying to process.

if maintype == 'multipart':
                    for part in email_message.get_payload():
                            if part.get_content_type() == 'text/plain':
                                text += part.get_payload().decode("quoted-printable").decode(part.get_content_charset())

Hope this helps someone !

Dave

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top