Gmail Email Fetch Script

Description

This Python script uses the Gmail API to authenticate a user and retrieve the last 1000 emails from their Gmail account. The email data is saved as a JSON file on the local system. Below is a breakdown of the process and the complete script.


Script Breakdown


#!/usr/bin/env python

import os
import json
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build

# Define the OAuth scope for read-only Gmail access
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']

# Authenticate and create Gmail API service
def authenticate_gmail():
    creds = None
    if os.path.exists('token.json'):
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)

    # If there are no valid credentials, authenticate the user
    if not creds or not creds.valid:
        flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
        creds = flow.run_local_server(port=0)
        # Save credentials for future runs
        with open('token.json', 'w') as token_file:
            token_file.write(creds.to_json())

    return build('gmail', 'v1', credentials=creds)

# Fetch the last 1000 emails and save them to a JSON file
def fetch_and_dump_emails():
    service = authenticate_gmail()
    results = service.users().messages().list(userId='me', maxResults=1000).execute()
    messages = results.get('messages', [])

    emails = []
    for msg in messages:
        msg_detail = service.users().messages().get(userId='me', id=msg['id']).execute()
        email_data = {
            'id': msg_detail['id'],
            'snippet': msg_detail.get('snippet'),
            'internalDate': msg_detail.get('internalDate'),
            'payload': msg_detail.get('payload', {})
        }
        emails.append(email_data)

    # Save the emails to a JSON file
    with open('last_1000_emails.json', 'w') as f:
        json.dump(emails, f, indent=2)

    print("Successfully dumped the last 1000 emails to 'last_1000_emails.json'.")

# Run the script
if __name__ == "__main__":
    fetch_and_dump_emails()


Example Output

ID Snippet Internal Date Payload
1782345678abcdef This is an example snippet from an email. 1689465678901 { ... }
1456fghij789klm Another email snippet with important data. 1697462877654 { ... }

Conclusion

This script provides a simple way to automate the retrieval of email data from Gmail using the Gmail API. The data is saved locally as a JSON file, which can be used for further analysis or archival purposes.