How to retrieve the whole message body using Gmail API (python)

前端 未结 3 809
名媛妹妹
名媛妹妹 2020-12-16 05:34

I want to extract the whole message body of mail using gmail api. Right now i am using \'snippets\' but i need the entire text. I searched and found that it\'s something to

3条回答
  •  不知归路
    2020-12-16 06:04

    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.

提交回复
热议问题