I get emails using imaplib in Python / Django.
My goal is to read simple text and HTML messages.
I use:
mail.select('inbox', readonly=True) result, data = mail.uid('fetch', email_uid, '(RFC822)') raw_email = data[0][1] email_message = email.message_from_string(raw_email) #print "EMAIL:",email_message #print "HEADERS",email_message.items() subject = get_decoded_header(email_message['Subject']) from_address = get_decoded_header(email_message['From']) date = email_message['Date'] date = parse_date(date) body = ''+get_first_text_block(email_message)
And the code for get_first_text_block (obtained from the Internet):
def get_first_text_block(email_message_instance): maintype = email_message_instance.get_content_maintype() if maintype == 'multipart': for part in email_message_instance.get_payload(): if part.get_content_maintype() == 'text': return part.get_payload() elif maintype == 'text': return email_message_instance.get_payload()
Now the problem is that the text is not displayed in formatted form. In particular: If it is a text email, the text appears as one large consolidated line instead of gaps, paragraphs and blank lines between lines.
If it is HTML text, HTML is not displayed at all, instead it is displayed as plain text with HTML fragments inside (even using a safe filter on Django).
I assume that something like the wrong conversion of the email payload to a string or the like may happen, but I checked everything and could not find out what might be wrong.
What am I doing wrong?
source share