0
import imaplib
import email

m = imaplib.IMAP4_SSL("imap.gmail.com")
m.login('*******', '*******')
m.select('[Gmail]/All Mail')

resp, items = m.search(None, 'ALL') 
items = items[0].split()


for emailid in items:  
    resp, data = m.fetch(emailid, "(RFC822)") 
    raw_email = data[0][1]
    email_message = email.message_from_string(raw_email)

    for part in email_message.walk():
        if part.get_content_type() == 'text/plain': 
            msg = part.get_payload() 

            print msg

When I print msg I get one string over three lines ex:

"This is a test

This is also a test

This is not a test"

The problem is that I am having trouble separating the string and assigning variables to each of these lines. for example msg[0] returns: "T

T

T"

Without the quotes.

Any help would be appreciated

Codezilla
  • 95
  • 1
  • 7
  • I think you need to `get_payload(decode=True)`, and maybe after that you need to `.encode('urf8', 'replace')`. from: http://www.vineetdhanawat.com/blog/2012/06/how-to-extract-email-gmail-contents-as-text-using-imaplib-via-imap-in-python-3/ and https://gist.github.com/miohtama/5389146 – TessellatingHeckler Jun 24 '15 at 03:42
  • @TessellatingHeckler Thank you so much! – Codezilla Jun 24 '15 at 18:20

0 Answers0