5

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?

2 Answers 2

2

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

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

Comments

0

As Arkanus suggested the problem was related to the quote-printable codification.

Instead of using quopri I used the decode argument implementing a code that is similar to this one.

The first error was caused by the fact that I'm using Python 3.4. I'm not sure about the reason but using Python 2.7 it works fine.

3 Comments

A general good practice when handling mails is to check the Content-Type and Content-Transfer-Encoding headers; I don't know how GMail API formats it's output, but you may also encounter mails with other charsets and encodings
yap... that's why I'm using the decode argument in the email package instead of quopri. It will take care of of the different encodings that I may have :)
Would have been nice if you actually shared your code! :)

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.