email_lite.py script synchronizes recent emails from Gmail to provide morning context. It’s designed to be lightweight and optional - only runs if configured.
Command Syntax
This script has no command-line options. All configuration is done through
config/email.yaml.Configuration
Createconfig/email.yaml to enable email sync:
Maximum number of recent emails to fetch from Gmail.
Gmail label to fetch messages from. Common values:
INBOX- Inbox messagesUNREAD- Unread messages onlySTARRED- Starred messagesSENT- Sent messages- Custom label names
Usage Examples
View recent emails
Output shows the first 5 emails with subject (truncated to 50 chars) and sender.
No configuration file
Ifconfig/email.yaml doesn’t exist:
Environment Variables
Email sync requires Google OAuth credentials:Google OAuth client ID from Google Cloud Console.
Google OAuth client secret.
OAuth refresh token with
gmail.readonly scope. Generated by setup-oauth.Email Metadata
The script fetches these fields for each email:- id: Gmail message ID
- subject: Email subject line
- from: Sender email and name
- date: Date/time the email was sent
Email content (body) is not fetched - only metadata. This keeps the sync fast and privacy-focused.
Demo Mode
For demos or testing without real Gmail access:-
Create
config/demo_gmail_fake.yaml(gitignored): -
Create
config/demo/emails_fake.json:
How It Works
- Check configuration: If
config/email.yamldoesn’t exist, exit early - Check demo mode: If
config/demo_gmail_fake.yamlexists, return fake data - Load credentials: Get OAuth refresh token from
.env - Refresh access token: Exchange refresh token for active access token
- Fetch messages: Call Gmail API
users.messages.listwith label filter - Fetch metadata: For each message ID, call
users.messages.getwithformat=metadata - Parse headers: Extract Subject, From, and Date from message headers
- Return results: List of email metadata objects
API Scope Required
Read-only access to Gmail messages and settings.API endpoint:
https://www.googleapis.com/auth/gmail.readonlyExit Codes
- 0: Success - emails fetched or skipped (no config)
- Non-zero: Not explicitly used - errors are caught and return empty list
Error Handling
The script handles errors gracefully:- Missing credentials: Returns empty list (
[]) - API errors: Returns empty list
- Network timeout: Returns empty list
- Invalid token: Returns empty list
To debug email sync issues, check:
GOOGLE_REFRESH_TOKENis set in.env- Token has
gmail.readonlyscope - Gmail API is enabled in Google Cloud Console
- You have internet connectivity
Privacy Considerations
This script is designed with privacy in mind:- ✅ Only fetches metadata (no email body content)
- ✅ Respects Gmail labels (can limit to specific folders)
- ✅ Uses read-only scope (
gmail.readonly) - ✅ Does not store emails locally (unless you extend it)
- ✅ Optional - disabled by default (requires
config/email.yaml)
Integration with Morning Routine
The email command is step 4 in the run-morning routine:- Identify urgent messages
- Prepare for meetings mentioned in emails
- Plan responses or follow-ups
Advanced Configuration
Fetch only unread emails
Fetch from multiple labels
The script currently supports one label. To fetch from multiple labels, run the script multiple times or modifyfetch_recent_emails() to accept a list.
Custom label names
Gmail allows custom labels. Use the exact label name:Related Commands
- setup-oauth - Configure Gmail API access
- run-morning - Full morning routine (includes email sync as step 4)
- calendar - Related morning context from calendar events
Function Reference
If you’re extending the script, these functions are available:get_email_config() -> dict
Reads config/email.yaml and returns configuration dictionary.
fetch_recent_emails(max_messages: int = 20, label: str = "INBOX") -> list
Fetches recent emails from Gmail API.
Parameters:
max_messages: Maximum number of emails to fetchlabel: Gmail label to filter by
id, subject, from, date
get_recent_emails_if_configured() -> list
Main entry point. Checks if config/email.yaml exists, handles demo mode, and fetches emails.
Returns: List of email metadata or empty list
Troubleshooting
No emails returned
-
Check
config/email.yamlexists: -
Verify credentials:
-
Test Gmail API access: