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