How to retrieve the whole message body using Gmail API (python)
same as noogui said but I found that snippet wont return the whole body
when the snippet exceed 200~ chars you will get it under payload.body.data you can find the whole body using payload.body.data
the catch is that you are getting it base64encoded so you need to decode it :)
the following code will do the trick
import base64
mail = service.users().messages().get(userId=user_id, id=id, format="full").execute()
def parse_msg(msg):
if msg.get("payload").get("body").get("data"):
return base64.urlsafe_b64decode(msg.get("payload").get("body").get("data").encode("ASCII")).decode("utf-8")
return msg.get("snippet")
Use Users.messages.get from the docs where there's a Python snippet:
import base64
import email
from apiclient import errors
def GetMessage(service, user_id, msg_id):
try:
message = service.users().messages().get(userId=user_id, id=msg_id).execute()
print 'Message snippet: %s' % message['snippet']
return message
except errors.HttpError, error:
print 'An error occurred: %s' % error
def GetMimeMessage(service, user_id, msg_id):
try:
message = service.users().messages().get(userId=user_id, id=msg_id, format='raw').execute()
print 'Message snippet: %s' % message['snippet']
msg_str = base64.urlsafe_b64decode(message['raw'].encode('ASCII'))
mime_msg = email.message_from_string(msg_str)
return mime_msg
except errors.HttpError, error:
print 'An error occurred: %s' % error
What your trying to access is the payload.body or if you want to go further, payload.body.data.