2

I have a problem. I am trying to read emails from gmail using the gmail api. I followed the instructions from here https://developers.google.com/gmail/api/v1/reference/users/messages/get

I made a few changes to run it on python 3, so I ended up with the following code:

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('utf8'))
  mime_msg = email.message_from_bytes(msg_str)
  print(mime_msg)

  return mime_msg
except errors.HttpError as error:
  print ('An error occurred: %s' % error)

Now the output is very close to what I would love to have, but there is a problem that the Hungarian accented characters in the output are weird: G=C3=A1bor instead of Gábor

And also the html tags are kind broken:

Follow us:          =09=09=09=09=09=09=09=09=09<a href=3D"http=

I have already figured out that this is related to fact how the message is encoded, see header of the email:

Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

The problem is, that I can't seem to find a way to decode it properly. Ever help is appreciated.

2 Answers 2

5

After hours of struggling with the same problem myself, I finally found the solution (at least for my case). Even through the thread is old, I'm posting it here so that others may find it later. Here it goes:

# Get your message
message = service.users().messages().get(userId=user_id, id=msg_id, format='full').execute()

# Get the right body data item (this part was my Heureka-moment as there are several 'data' keys)
content = message['payload']['parts'][0]['parts'][1]['body']['data']

# Encode
msg_body = base64.urlsafe_b64decode(content).decode('utf-8')

This sequence got the de/encoding working for me. There are as many different solution suggestions out there as there are threads about the decoding/encoding issues with base64 and Google Gmail API but it seems that each case is unique. Hope this solution also helps you!

Sign up to request clarification or add additional context in comments.

1 Comment

I spent a whole day trying to solve this. Thanks a lot! This should be covered by Google in their documentation.
0

Thanks Tuppitappi. The Gmail API doc for Python could be a bit clearer. The above solutions works for me too. I could retrieve the content the following way:

 msgs = message['payload']['parts']
 msg = msgs[1]['body']['data']

The MessagePart may consist of different parts:

"parts": [
      {
        "partId": "0",
        "mimeType": "text/plain",
        "filename": "",
        "headers": [
          { "name": "Content-type", "value": "text/plain;charset=utf-8" },
          { "name": "Content-Transfer-Encoding", "value": "quoted-printable" }
        ],
        "body": {
          "size": 1887,
          "data": "ascii string to be decoded"
        }
      },
      {
        "partId": "1",
        "mimeType": "text/html",
        "filename": "",
        "headers": [
          { "name": "Content-type", "value": "text/html;charset=utf-8" },
          { "name": "Content-Transfer-Encoding", "value": "quoted-printable" }
        ],
        "body": {
          "size": 66970,
          "data": "longer ascii string to be decoded"
        }
      }
    ]

Extract from the Doc for the full payload:

Resource representations An email message.

{   "id": string,   "threadId": string,   "labelIds": [
    string   ],   "snippet": string,   "historyId": unsigned long,   "internalDate": long,   "payload": {
    "partId": string,
    "mimeType": string,
    "filename": string,
    "headers": [
      {
        "name": string,
        "value": string
      }
    ],
    "body": users.messages.attachments Resource,
    "parts": [
      (MessagePart)
    ]   },   "sizeEstimate": integer,   "raw": bytes }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.