A powerful command-line tool for exporting, importing, and managing Gmail emails with advanced filtering capabilities.
- Export emails from Gmail with advanced filtering (supports all Gmail search operators)
- Import emails into Gmail accounts (supports cross-account transfers)
- Cleanup emails from source account after export
- Multiple formats: EML, JSON, mbox
- Parallel processing for high performance
- Progress tracking with real-time indicators
- Resumable operations with state management
- Comprehensive metrics collection (JSON and Prometheus formats)
- OAuth 2.0 authentication with Google Gmail API
- Cross-account support for migrating between Gmail accounts
- Go 1.19 or later
- Gmail API credentials (see Authentication section)
git clone https://github.com/octasoft-ltd/gmail-exporter.git
cd gmail-exporter
go build -o gmail-exporter ./cmd/gmail-exporterGmail Exporter supports two authentication scenarios:
-
Set up Gmail API credentials:
- Go to Google Cloud Console
- Create a new project or select existing one
- Enable Gmail API
- Create OAuth 2.0 credentials (Desktop application)
- Important: Add yourself as a test user to avoid "unverified app" issues
- Download the credentials JSON file
-
Authenticate:
./gmail-exporter auth setup --credentials gmail-credentials.json ./gmail-exporter auth login
π Detailed Setup Guide: See AUTHENTICATION_SETUP.md for step-by-step instructions including handling Google's "unverified app" warnings.
For migrating emails between different Gmail accounts:
-
Set up credentials for source account (export):
./gmail-exporter auth setup --credentials source-credentials.json --token source-token.json ./gmail-exporter auth login --token source-token.json
-
Set up credentials for destination account (import):
./gmail-exporter auth setup --credentials dest-credentials.json --token dest-token.json ./gmail-exporter auth login --token dest-token.json
-
Export from source account:
./gmail-exporter export \ --credentials-file source-credentials.json \ --token-file source-token.json \ --output-dir exports/ \ --from "important-sender@example.com"
-
Import to destination account:
./gmail-exporter import \ --input-dir exports/ \ --import-credentials dest-credentials.json \ --import-token dest-token.json
# Export all emails to EML format
./gmail-exporter export --output-dir exports/
# Export with filters
./gmail-exporter export \
--output-dir exports/ \
--from "sender@example.com" \
--subject "Important" \
--date-after "2024-01-01" \
--has-attachment# Complex filter example
./gmail-exporter export \
--output-dir exports/ \
--from "notifications@github.com" \
--includes-words "pull request merged" \
--date-within "30d" \
--size-greater-than "1MB" \
--labels "work,important" \
--exclude-chats# 1. Export from source account
./gmail-exporter export \
--credentials-file source-creds.json \
--token-file source-token.json \
--output-dir migration/ \
--search-scope "all_mail"
# 2. Import to destination account
./gmail-exporter import \
--input-dir migration/ \
--import-credentials dest-creds.json \
--import-token dest-token.json
# 3. Optional: Clean up source account
# Note: The export process automatically creates processed_emails.json
./gmail-exporter cleanup \
--credentials-file source-creds.json \
--token-file source-token.json \
--action archive \
--filter-file migration/processed_emails.jsonIf you have an existing exports directory but no filter file for cleanup:
# Generate filter file from exports directory
./gmail-exporter generate-filter --input-dir exports/
# This creates exports/processed_emails.json which can be used for cleanup
./gmail-exporter cleanup \
--filter-file exports/processed_emails.json \
--action archive \
--dry-run# Test with a small number of messages first
./gmail-exporter export --output-dir test/ --limit 5
./gmail-exporter import --input-dir test/ --limit 5--output-dir, -o: Output directory for exported emails--format: Export format (eml, json, mbox) [default: eml]--organize-by-labels: Organize emails by labels in folder structure--parallel-workers: Number of parallel workers [default: 3]--include-attachments: Include email attachments [default: true]--limit, -l: Limit number of messages to process (useful for testing)
--input-dir, -i: Input directory containing exported emails--import-credentials: Gmail API credentials for destination account--import-token: OAuth token file for destination account--parallel-workers: Number of parallel workers [default: 3]--preserve-dates: Preserve original email dates [default: true]--limit, -l: Limit number of messages to process (useful for testing)
--action: Action to perform (archive, delete) [default: archive]--filter-file: JSON file containing processed email IDs--dry-run: Show what would be done without making changes--limit, -l: Limit number of messages to process
--input-dir, -i: Input directory containing exported emails--output-file, -o: Output filter file path (default: input-dir/processed_emails.json)
All Gmail search operators are supported:
--to: Recipient email address--from: Sender email address--subject: Subject contains text--includes-words: Email body contains words--excludes-words: Email body excludes words--size-greater-than: Email size greater than (e.g., 5MB)--size-less-than: Email size less than (e.g., 10MB)--date-within: Date within period (e.g., 30d, 1w, 6m)--date-after: After specific date (YYYY-MM-DD)--date-before: Before specific date (YYYY-MM-DD)--has-attachment: Has attachments--no-attachment: No attachments--exclude-chats: Exclude chat messages [default: true]--labels: Specific labels (comma-separated)--search-scope: Search scope (all_mail, inbox, sent, drafts, spam, trash)
Standard email format that preserves all email data including headers, body, and attachments.
Structured format containing the complete Gmail API message object.
Unix mailbox format for compatibility with email clients.
- OAuth 2.0: Secure authentication without storing passwords
- Local storage: All credentials and tokens stored locally
- API permissions: Requests only necessary Gmail API scopes
- No data transmission: All processing happens locally
π Security Documentation: See SECURITY.md for detailed information about:
- What permissions are requested and why
- How to verify the code is safe
- Handling Google's "unverified app" warnings
- Security best practices
# Check authentication status
./gmail-exporter auth status
# Refresh expired tokens
./gmail-exporter auth refresh
# Re-authenticate if needed
./gmail-exporter auth login- "Invalid credentials": Ensure credentials file is valid JSON from Google Cloud Console
- "Token expired": Run
./gmail-exporter auth refresh - "Permission denied": Ensure Gmail API is enabled in Google Cloud Console
- "Rate limit exceeded": Reduce parallel workers with
--parallel-workers 1
- Wrong account: Verify you're using correct credentials/token files
- Cross-contamination: Use separate credential files for each account
- Token confusion: Always specify both credentials and token files explicitly
- Parallel processing: Configurable worker pools for optimal performance
- Resumable operations: Continue interrupted exports/imports
- Progress tracking: Real-time progress indicators
- Memory efficient: Streams large emails without loading entirely into memory
The tool collects comprehensive metrics:
{
"operation": "export",
"start_time": "2024-01-15T10:30:00Z",
"end_time": "2024-01-15T10:35:30Z",
"duration": "5m30s",
"total_matched": 1500,
"total_processed": 1500,
"total_failed": 0,
"total_size": 2147483648,
"performance": {
"emails_per_second": 4.5,
"bytes_per_second": 6553600
}
}- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
MIT License - see LICENSE file for details.
- Issues: Report bugs and feature requests on GitHub
- Documentation: See USAGE.md for detailed examples
- Security: Report security issues privately (see SECURITY.md)
- Authentication: See AUTHENTICATION_SETUP.md for detailed setup instructions