Export Outlook.com emails to Markdown files with attachments.
# Create conda environment
conda env create -f environment.yml
conda activate outlook-exportpython outlook_export.py --reauth- A URL and code will display
- Open https://microsoft.com/devicelogin in your browser
- Enter the code and sign in with your Microsoft account
- Token saves automatically to
.token_cache.json(valid ~90 days)
# List folders
python outlook_export.py --list-folders
# Export folder (outputs to ./mail/Inbox/)
python outlook_export.py -f Inbox
# Limit emails
python outlook_export.py -f Inbox --limit 100
# Batch size (default 50)
python outlook_export.py -f Inbox --batch-size 25
# Group by conversation thread
python outlook_export.py -f Inbox --threads
# Filter emails
python outlook_export.py -f Inbox --filter "flag/flagStatus eq 'flagged'"
python outlook_export.py -f Inbox --filter "isRead eq false"
python outlook_export.py -f Inbox --filter "receivedDateTime ge 2024-01-01"
# Show all filter options
python outlook_export.py -?Without threads:
./mail/Inbox/
20240130_Meeting_notes.md
20240129_Invoice/
message.md
attachments/
invoice.pdf
With threads (--threads):
./mail/Inbox/
20240115_Project_Discussion/
20240115_093022.md
20240116_141532.md
attachments/
20240116_report.pdf
.manifest.jsontracks downloaded message IDs- Re-running skips already downloaded emails
- Safe to interrupt and resume
For non-interactive use (CI, AI agents, scripts):
# Step 1: Get tokens interactively (once)
python outlook_export.py --auth-only
# Step 2: Use refresh token (~90 days, recommended)
python outlook_export.py --refresh-token "0.AYIA..." -f Inbox
# Or use token cache file
python outlook_export.py --token-cache /path/to/.token_cache.json -f Inbox
# Or use access token (~1 hour only)
python outlook_export.py --token "eyJ0eX..." -f Inbox| Option | Description |
|---|---|
-f, --folder |
Folder name to export |
-o, --output |
Output directory (default: ./mail/{folder}) |
-n, --limit |
Max emails to export |
-b, --batch-size |
Emails per API request (default: 50) |
-s, --skip |
Skip first N emails |
-t, --threads |
Group by conversation thread |
--filter |
OData filter query |
-?, --filter-help |
Show filter examples |
--list-folders |
List available folders |
--reauth |
Clear cache and re-authenticate |
--auth-only |
Print tokens and exit |
--token |
Use access token (~1 hour) |
--refresh-token |
Use refresh token (~90 days) |
--token-cache |
Use token cache file |
Expose email tools to Claude Desktop for prompts like "check my recent emails and list action items."
| MCP Server | Cloud Connector (Zapier, etc.) | |
|---|---|---|
| Data location | Stays on your machine | Flows through third-party servers |
| Privacy | Emails never leave your computer | Provider can access your data |
| Auth | You control tokens locally | OAuth grants access to connector service |
| Cost | Free | Often subscription-based |
| Latency | Direct API calls | Extra hop through cloud |
| Offline | Works with cached tokens | Requires internet to connector |
MCP (Model Context Protocol) is Anthropic's open standard for AI-tool integration. The server runs locally alongside Claude Desktop - when Claude needs email data, it calls your local MCP server, which fetches directly from Microsoft's API. Your emails are read by Claude but never stored or transmitted elsewhere.
-
Authenticate first:
python outlook_export.py --auth-only
-
Configure Claude Desktop (
~/Library/Application Support/Claude/claude_desktop_config.json):{ "mcpServers": { "outlook-email": { "command": "/Users/{username}/miniconda3/envs/outlook-export/bin/python", "args": ["/Users/{username}/source/email/mcp_server.py"] } } } -
Restart Claude Desktop
| Tool | Description |
|---|---|
list_email_folders |
List all folders in mailbox |
get_recent_emails |
Get emails from last N days (filter by unread/flagged) |
search_emails |
Search with OData filter query |
get_email_by_id |
Get full email content by ID |
- "Check my inbox for emails from the last 3 days and summarize any action items"
- "Show me my unread emails"
- "Find all flagged emails and list what needs my attention"
- "Search for emails from boss@company.com"