1

I just started up with APIs and figured I'd play around with Gmail's. I'm looking to scrape all emails sent to me in the past month for some text analysis. I might just be being thick here or have missed some documentation somewhere (probably), but I can't figure out how to get the body of emails that had attachments. I'm not interested in the attachment, just the body of the email.

results = gmail.users().messages().list(labelIds=['INBOX'], q='to:me after: '+str(date),userId='me').execute()
mssg_list = results['messages']
for mssg in mssg_list:
    m_id = mssg['id']
    message = gmail.users().messages().get(userId='me', id=m_id).execute()
    body = message['payload']['parts'][1]['body']
    final_body = base64.urlsafe_b64decode(body['data'].decode("utf-8"))

For messages with attachments, it returns only attachmentId and size, rather than size and data. I tried reading the attachmentId to see if perhaps data was kept there instead, but no dice; it appears to just refer to the attachment. Where is the actual text body living?

kodachrome
  • 330
  • 1
  • 3
  • 18
  • Bit offtopic: raw ``except``s are almost never a good idea. Better only catch the exception you might expect and let all the other, rather unexpected ones raise. – linusg Oct 14 '17 at 20:45
  • Thanks--good point. I don't anticipate keeping the except; I'll remove to eliminate any potential confusion. – kodachrome Oct 14 '17 at 20:48
  • Have you stumbled upon [**this question**](https://stackoverflow.com/questions/32655874/cannot-get-the-body-of-email-with-gmail-php-api/32660892#32660892)? It is a PHP question, and the example algorithm is written in JavaScript, but it might be of some help. – Tholle Oct 14 '17 at 21:51

0 Answers0