This script lets me sync GMail to .mbox via the API (not IMAP), allowing me to analyze it offline.
Clone this repository:
git clone https://github.com/sanand0/gmailmbox.git
cd gmailmboxOn Google Cloud Console, download credentials.json into gmailmbox/:
- Create a new project
- Enable the Gmail API
- Create OAuth2 Client ID for a Desktop app
- Download the JSON as
credentials.json
Then install and run:
# Fetch all emails (excludes spam, trash, and invites by default)
uv run gmailmbox.py --mbox emails.mbox
# Refresh only new emails
uv run gmailmbox.py --mbox emails.mbox --update
# Refresh only new emails matching a query
uv run gmailmbox.py --mbox emails.mbox --update --q "from:user@example.com"This will sync all emails that match the query to the .mbox file.
Parameters:
--mbox: Path to the .mbox file (default: "emails.mbox")--q: Gmail search query (default: "in:anywhere -in:spam -in:trash -invite")--update: Only update new emails (default: False)