Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I recieve a html email as a regular text?

Tags:

python

email

imap

Here is the code I have thus far:

import email, imaplib

user = 'some username'
pwd = 'some password'

m = imaplib.IMAP4_SSL("imap.gmail.com")
m.login(user, pwd)

m.select("[Gmail]/All Mail")

resp, data = m.fetch(1, "(RFC822)")

email_body = data[0][1]

mail = email.message_from_string(email_body)

print mail

I currently receive the email with a bunch of weird formatting. I would like to receive the email body as a plain text string.

like image 525
Richard Avatar asked Dec 02 '22 05:12

Richard


1 Answers

(I've just tried this with my Gmail account.) The problem isn't the HTML mail, it's that your messages are MIME multipart and you're printing the full string of this. This is because email is fundamentally a plain-text format (as was mentioned above); when people wanted to send rich content in emails they came up with MIME, which is a method to do this without modifying the email standard. When you print mail, you are printing the full MIME message, encoded so that it can be sent as an email. You want to extract the payload.

But -- you've already done all the hard work! Just get the payload of the parsed email.message.Message instance:

mail.get_payload()[ 0 ].get_payload()

(Note: I had to do this twice for the first message in my Gmail inbox because it was encoded as a MIMEMultipart, but with only one leaf. YMMV.)

like image 157
Katriel Avatar answered Dec 05 '22 01:12

Katriel