Extract body content of a Gmail with Python

803 views Asked by At

I am trying to extract the body of a Gmail email. The problem is that I am able to get all the content of the email, but I only want the body.

import imaplib
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('[email protected]', 'mypassword')
mail.list()
# Out: list of "folders" aka labels in gmail.
mail.select("inbox") # connect to inbox.
result, data = mail.search(None, "ALL")

ids = data[0] # data is a list.
id_list = ids.split() # ids is a space separated string
latest_email_id = id_list[-1] # get the latest

# fetch the email body (RFC822) for the given ID
result, data = mail.fetch(latest_email_id, "(RFC822)") 

raw_email = data[0][1] # here's the body, which is raw text of the whole   email
# including headers and alternate payloads
print raw_email

The problem is that with this code I get a lot of thing that I don't need. This is the output I get:

b'Delivered-To: [email protected]\r\nReceived: by 10.157.60.218 with SMTP id t26csp3600091otf;\r\n Sun, 10 Sep 2017 10:39:56 -0700 (PDT)\r\nX-Received: by 10.28.60.4 with SMTP id

................................................

j4mr2630503wma.46.1505065196879;\r\n Sun, 10 Sep 2017 10:39:56 -0700 (PDT)\r\nARC-Seal: i=1; a=rsa-sha256; t=1505065196; cv=none;\r\n d=google.com; s=arc-20160816;\r\n 9:55 -0700 (PDT)\r\nMessage-ID: <[email protected]>\r\nDate: Sun, 10 Sep 2017 10:39:55 -0700 (PDT)\r\nFrom: [email protected]\r\n\r\n/Camera\r\n'

If anyone has an idea to fix my code I would be very grateful. I also accept a new code that is working. Thanks in advance!

0

There are 0 answers