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.
(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:(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.)
It is highly unlikely that you won't be able to do this: What if it isn't ascii? Even if everything is in English it might contain characters outside the ascii range.
imaplib
's documentation is not going into any encoding questions, so I suppose the data arrives as byte strings, and I'd expect them to be utf-8 encoded. Other libraries might provide you with Unicode strings, which you can encode in any encoding you fancy (but utf-8 is a good choice).However, I suppose you don't really mean "ascii" but what you think of as "plain text" -- a text, not html, version of the email. It might be a multipart mail, in which case you can extract the "text/plain" part from the string you receive. If it isn't, and the mail body was html only, you can use a third-party library. There are a number of options, discussed in this SO thread.
In Python 3.x you can do it in a very easy way by importing 'imaplib' and 'email' packages. Although this is an older post but maybe my answer can help new comers on this post.
Now you can print body variable and it will be in plaintext format :) If it is good enough for you then it would be nice to select it as accepted answer.
EMail is a text-only format, it doesn't know about formatting. So if you get a HTML mail, then that formatting has been "smuggled" into the pure-text body or you have a multi-part mail where the first part is pure text and the second part is the HTML formatted version.
So check if you have a multipart mail (see the docs) and filter accordingly. If it's not a multipart mail, use a HTML library like Beautiful Soup to get the text.
Getting the right html/text isn't that easy and straightforward. As email can have html as attachment or even multiple HTML. Python 3 gives you a simple method to do that