I'm trying to read my GMail messages using the API provided by Google using Python 3.4.
I'm using this function that is provided by Google at this link:
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
However if I use this function as it is I get the following error:
TypeError: initial_value must be str or None, not bytes
So I changed the function a bit:
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('utf-8','ignore'))
print(msg_str)
mime_msg = email.message_from_string(msg_str.decode('utf-8','ignore'))
return mime_msg
except errors.HttpError:
print('An error occurred')
If I don't add the 'ignore' argument I get the following error:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xeb in position 2214: invalid continuation byte
If I use the 'ignore' argument then the content of the mail, for example a HTML text, has some weird characters into it, for example:
=09=09body=2C#bodyTable=2C#bodyCell{
=09=09=09height:100% !important;
=09=09=09margin:0;
=09=09=09padding:0;
=09=09=09width:100% !important;
=09=09}
My problem seems very similar to this one but, given that I'm not a Python expert and I need to use the GMail API, I cannot see how to fix it. Any idea?
It appears that the mail contents are in quote-print codification.
You can use the quopri module to handle it https://docs.python.org/2/library/quopri.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With