1

I am writing Python script which should extract mails from gmail. I am using Gmail Api. The code is:

def get_credentials():
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'gmail-python-quickstart.json')

    store = Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else: # Needed only for compatibility with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials

def main():
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('gmail', 'v1', http=http)

    results = service.users().threads().list(userId='me').execute()
    result = results.get('threads', [])
    for each in result:
        print(each['snippet']+"\n")

I receive about 200 character messages. As I read documentation here. I wanted to use "payload" and "body", but it seems like there is no such possibility as far as there is no value "payload". Any ideas how to get full message?

toecsnar42
  • 413
  • 3
  • 13
  • In https://developers.google.com/gmail/api/v1/reference/users/messages/get, did you try the following: `format string The format to return the message in. Acceptable values are: "full": Returns the full email message data with body content parsed in the payload field; the raw field is not used. (default) [...] "raw": Returns the full email message data with body content in the raw field as a base64url encoded string; the payload field is not used.` ? Also, http://stackoverflow.com/questions/25484791/gmail-api-users-messages-list might help. – boardrider Oct 20 '16 at 18:27
  • With your method I receive coded message. As I tried to encode it with base64 I got some kind (probably) of html. – toecsnar42 Oct 21 '16 at 13:02
  • An email message is a MIME structure. Each MIME part has an encoding. You need to understand this structure and be prepared to handle the palette of available encodings. – tripleee Jan 15 '18 at 12:47

0 Answers0