⚠️ AI-Generated Code: This project, including the code and this README, was generated using Claude (Anthropic's AI assistant). While functional, please review and test thoroughly before use in production environments.
A CLI tool to extract emails to/from specific email addresses from your Gmail account. The tool organizes extracted emails into folders and generates CSV files with email metadata.
- Interactive Setup Wizard - Guided Gmail API setup in 3-5 minutes
- Continuous Interactive Menu - Simple menu that loops after each operation, no need to restart
- Attachment Download - Optional download of email attachments
- Extract emails where specific addresses appear in From, To, or CC fields
- Case-insensitive email matching - Handles emails regardless of capitalization
- Automatic folder organization by email address
- HTML email files with headers and formatting
- CSV file with email metadata (subject, date, from, to, cc, attachments)
- Support for Google Workspace accounts
- Handles pagination for large email volumes
- Enhanced error messages with helpful troubleshooting
- Setup validation to verify configuration
- Easy authentication reset
- Automatic deduplication of email addresses
./run.shThat's it! The script automatically:
- Creates a virtual environment if needed
- Installs dependencies if needed
- Runs the gmail extractor
When the menu appears, select option 1 (Setup wizard). The wizard will:
- Guide you through Google Cloud Console setup
- Automatically open the necessary pages in your browser
- Validate your credentials
- Test authentication
- Setup time: 3-5 minutes
./run.sh # Run again- Select option 2 (Initialize) to create
email_addresses.txt - Edit the file and add email addresses (one per line)
- Run
./run.shagain and select option 4 (metadata only) or 5 (with attachments)
Done! Your emails will be in the extracted_emails/ folder.
- Python 3.7 or higher
- A Google account with Gmail
- Google Cloud Console access (free - no billing required)
The setup wizard automatically guides you through the process:
./run.shThen select option 1 (Setup wizard) from the menu. The wizard will:
- Check your current setup status
- Open Google Cloud Console pages in your browser
- Provide step-by-step instructions for:
- Creating a Google Cloud project
- Enabling Gmail API
- Creating OAuth 2.0 credentials
- Downloading credentials.json
- Validate your credentials file
- Test authentication
- Confirm everything is working
If you prefer to set up manually:
- Go to Google Cloud Console
- Create a new project or select an existing one
- Enable the Gmail API:
- Click "Enable APIs and Services"
- Search for "Gmail API"
- Click "Enable"
- Create OAuth 2.0 credentials:
- Go to "Credentials" in the left sidebar
- Click "Create Credentials" → "OAuth client ID"
- If prompted, configure the OAuth consent screen:
- Choose "Internal" for Workspace or "External" for personal Gmail
- Fill in app name: "Gmail Extractor"
- Add your email
- Click through the steps
- Choose "Desktop app" as the application type
- Give it a name (e.g., "Gmail Extractor CLI")
- Click "Create"
- Download the credentials:
- Click the download icon next to your OAuth client
- Save the file as
credentials.jsonin this directory
Then validate your setup:
./run.sh
# Select option 3 (Validate setup)The tool uses an interactive menu system that loops continuously. Simply run:
./run.shNote: You can also use python3 gmail_extractor.py if you've manually set up a venv
You'll see a menu with these options:
- Setup wizard - Configure Gmail API (first-time setup)
- Initialize - Create email_addresses.txt template
- Validate setup - Check configuration status
- Extract emails (metadata only) - Extract emails without attachments
- Extract emails (with attachments) - Extract emails and download attachments
- Reset authentication - Delete saved tokens
- Exit - Quit the program
The menu will automatically return after each operation, allowing you to perform multiple tasks in one session. Select option 7 when you're done to exit.
Run the script and select option 2 (Initialize)
Edit email_addresses.txt and add email addresses (one per line):
john.doe@company.com
jane.smith@example.com
# Lines starting with # are comments
# Email addresses are case-insensitive
# These are treated as the same:
# John.Doe@Company.com
# john.doe@company.com
Note: Email addresses are automatically normalized to lowercase and duplicates are removed.
Run the script:
./run.shThen choose:
- Option 4 for metadata-only extraction (faster, no attachments)
- Option 5 to extract emails with attachments
On first extraction, the tool will:
- Open your browser for Google authentication
- Ask you to sign in and grant permissions
- Save authentication token (in
token.pickle)
Future runs use the saved token automatically.
When selecting option 5 (Extract emails with attachments):
- Attachments are saved in an
attachments/subfolder for each email - Attachment filenames are recorded in the CSV metadata file
- All attachment types are supported (PDFs, images, documents, etc.)
- Duplicate attachment filenames are automatically handled
extracted_emails/
├── john.doe@company.com/
│ ├── emails.csv
│ ├── 0001_Meeting_notes.html
│ ├── 0002_Project_update.html
│ ├── 0003_Invoice/
│ │ └── attachments/
│ │ ├── invoice.pdf
│ │ └── receipt.png
│ └── ...
└── jane.smith@example.com/
├── emails.csv
├── 0001_Introduction.html
└── ...
Note: When attachments are downloaded, emails with attachments get their own subfolder containing an attachments/ directory.
Each emails.csv contains:
| Filename | Subject | From | To | Cc | Date | Message ID | Attachments |
|---|---|---|---|---|---|---|---|
| 0001_Meeting_notes.html | Meeting notes | sender@email.com | you@email.com | colleague@email.com | 2025-01-15 14:30:00 | abc123... | |
| 0003_Invoice.html | Invoice for January | billing@company.com | you@email.com | 2025-01-16 09:15:00 | def456... | invoice.pdf, receipt.png |
Each email is saved as an HTML file with:
- Email headers (Subject, From, To, Cc, Date) in a styled header section
- Full email body content
- Viewable in any web browser
The tool uses a unified interactive menu that continuously loops, allowing you to perform multiple operations without restarting. Simply run:
python gmail_extractor.pyMenu options:
- Setup wizard - First-time Gmail API configuration (guided)
- Initialize - Create email_addresses.txt template file
- Validate setup - Check your configuration and authentication status
- Extract emails (metadata only) - Fast extraction without attachments
- Extract emails (with attachments) - Full extraction including attachments
- Reset authentication - Delete saved tokens (useful for switching accounts)
- Exit - Quit the program
After each operation completes, you'll be returned to the menu. This allows you to chain operations together (e.g., setup → validate → extract) in a single session.
First-time setup (all in one session):
./run.sh
# The menu appears - select options in sequence:
# 1. Select option 1 (Setup wizard) - Follow the guided setup
# 2. Menu returns - Select option 2 (Initialize) - Creates email_addresses.txt
# 3. Exit (option 7) and edit email_addresses.txt to add addresses
# 4. Run ./run.sh again
# Now extract:
# 5. Select option 5 (Extract emails with attachments)
# 6. Select option 7 (Exit) when doneTypical usage (after setup):
./run.sh
# Menu appears - choose what you need:
# - Option 4 or 5 to extract emails
# - Option 3 to validate setup
# - Option 6 to reset authentication if issues occur
# - Option 7 to exit
# The menu returns after each operation, so you can:
# - Extract multiple times
# - Validate between operations
# - Perform multiple tasks without restartingrun.sh- Convenience script that handles venv automatically (included)venv/- Virtual environment (auto-created by run.sh)email_addresses.txt- List of email addresses to extractcredentials.json- Google API credentials (you provide this)token.pickle- Saved authentication token (auto-generated)extracted_emails/- Directory containing all extracted emails
./run.sh
# Select option 3 (Validate setup)This will show the status of all required files and authentication.
Solution:
- Run the script and select option 1 (Setup wizard)
- Or manually download OAuth 2.0 Desktop App credentials from Google Cloud Console
- Make sure the file is named exactly
credentials.json - Use option 3 (Validate setup) to check status
Solution:
- Ensure Gmail API is enabled in your Google Cloud project
- Run the script and select option 1 (Setup wizard)
- Check that you selected the correct project in Google Cloud Console
Solution:
./run.sh
# Select option 6 (Reset authentication) to delete old tokens
# Then run ./run.sh again and select option 4 or 5 to extract (will re-authenticate)If you're extracting a very large number of emails, you might hit Gmail API rate limits. The script will show errors for failed requests. Simply re-run to continue - it will skip already downloaded emails.
The wizard will print URLs if it can't open your browser automatically. Copy and paste them into your browser manually.
If you encounter issues:
- Run the script and select option 3 (Validate setup) to check your configuration
- Check that you're using Python 3.7 or higher:
python --version - Ensure all dependencies are installed:
pip install -r requirements.txt - Review the error messages - they include helpful troubleshooting tips
- Your credentials and tokens are stored locally
- The app only requests read-only access to Gmail
- No data is sent to any third-party servers
- Email data is saved locally on your computer
This project includes a test suite to ensure code quality and robustness.
# Activate virtual environment
source venv/bin/activate
# Run all tests
pytest test_gmail_extractor.py -v
# Run tests with coverage report
pytest test_gmail_extractor.py --cov=gmail_extractor --cov-report=term-missing
# Run specific test class
pytest test_gmail_extractor.py::TestEmailAddressLoading -vThe test suite covers:
- ✅ Credentials file validation
- ✅ Email address loading and normalization
- ✅ Case-insensitive duplicate removal
- ✅ Comment and whitespace handling
- ✅ Filename sanitization
- ✅ Gmail query building
- ✅ Authentication status checking
- ✅ Attachment extraction (with attachmentId)
- ✅ Inline attachment handling
- ✅ Duplicate attachment filename handling
- ✅ Nested multipart message attachments
- ✅ Edge cases (unicode, empty strings, etc.)
Current coverage includes core utility functions and attachment extraction logic
When contributing, please:
- Add tests for new features
- Ensure existing tests still pass
- Aim for meaningful coverage of critical paths
- Use pytest fixtures for test data
As a Google Workspace administrator:
- You can extract emails from any account you have access to
- Use "Internal" OAuth consent screen type for easier setup
- Consider creating a service account if you need to automate this for multiple users
- Email archival: Backup emails from specific contacts
- Legal/compliance: Extract communications for discovery or audit
- Research: Analyze email patterns or content
- Migration: Export emails before switching platforms
- Personal organization: Create local backups of important correspondence
Contributions are welcome! This is an open-source project under the MIT License.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Implement incremental extraction (only new emails)
- Add support for filtering by date range
- Add export to other formats (PDF, TXT, MBOX)
- Improve error handling and retry logic
- Add progress bars for large extractions
- Support for batch processing multiple accounts
- Add command-line arguments for automation/scripting
- Created by: Ben Richardson
- Generated with: Claude (Anthropic AI)
- Built with:
MIT License - see LICENSE file for details.
Copyright (c) 2025 Ben Richardson
Note: This software is provided "as is", without warranty of any kind. See the LICENSE file for full terms.
This tool accesses your Gmail account with read-only permissions. Your credentials are stored locally and never transmitted to third parties. However:
- Always review code before granting access to your email
- Use at your own risk
- The authors are not responsible for any data loss or security issues
- Ensure you comply with your organization's policies before extracting emails
- Be mindful of privacy laws and regulations when handling email data
For issues, questions, or contributions:
- Open an issue on GitHub
- Submit a pull request
- Check existing issues for solutions
AI Generation Notice: This entire project (code, documentation, and README) was created using Claude AI. While care has been taken to ensure quality and security, users should review the code and understand its functionality before use.