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.