A professional, threaded email cleanup tool for iCloud IMAP with GUI support. Safely moves promotional emails, newsletters, and automated messages to a review folder while protecting important emails like invoices and bills.
- ๐ High Performance: Multi-threaded processing with auto CPU detection
- ๐ก๏ธ Safe Operation: Dry-run mode, whitelist protection, keyword safeguards
- โ๏ธ Highly Configurable: JSON configuration with local overrides
- ๐ฏ Smart Detection: List-Unsubscribe headers, subject keywords, domain filtering
- ๐ฑ GUI Ready: Clean interface for building desktop applications
- ๐ง Developer Friendly: Modular design, type hints, comprehensive documentation
- Python 3.9 or higher
- iCloud account with app-specific password
- IMAP access enabled
-
Clone the repository
git clone https://github.com/username/imap-mail-cleanup.git cd imap-mail-cleanup -
Install dependencies
pip install -r requirements.txt
-
Set up credentials
cp .env.example .env # Edit .env with your iCloud credentials -
Configure settings
cp config.local.example.json config.local.json # Edit config.local.json as needed -
Run the cleanup
# Command line interface python imap_cleanup_cli.py # Or launch the GUI python imap_cleanup_gui.py
Create a .env file with your iCloud credentials:
IMAP_USER=your_email@icloud.com
IMAP_PASS=your_app_specific_passwordconfig.json- Main configuration (committed to repo)config.local.json- Local overrides (ignored by git)
Key settings:
{
"cleanup_settings": {
"dry_run": true,
"age_days": 365,
"max_workers": "auto",
"batch_size": 50
},
"mail_settings": {
"source_folders": ["INBOX", "Archive"],
"target_folder": "Review/Delete"
}
}# Launch the GUI application
python imap_cleanup_gui.py
# Or using installed package
pip install -e .
imap-cleanup-guiThe GUI provides:
- Configuration Editor: Edit all settings with a user-friendly interface
- Connection Testing: Verify IMAP credentials before processing
- Real-time Progress: Live progress bars and status updates
- Results Logging: Detailed processing logs and results
- Safe Operation: Built-in dry-run mode and confirmation dialogs
# Basic usage (dry run by default)
python imap_cleanup_cli.py
# Using as installed package
pip install -e .
imap-cleanupfrom imap_cleanup import ConfigManager, EmailProcessor
# Basic usage
config_manager = ConfigManager()
processor = EmailProcessor(config_manager)
candidates, moved = processor.run()
print(f"Found {candidates} candidates, moved {moved} emails")from imap_cleanup.gui_interface import GUIInterface, ProcessingCallback
class MyCallback(ProcessingCallback):
def on_progress(self, current, total, message=""):
print(f"Progress: {current}/{total}")
gui = GUIInterface()
gui.start_processing(MyCallback())The project uses a clean, modular architecture:
imap_cleanup/
โโโ config.py # Configuration management
โโโ imap_manager.py # IMAP connection pooling
โโโ email_analyzer.py # Email analysis logic
โโโ email_processor.py # Main processing orchestrator
โโโ cli.py # Command-line interface
โโโ gui_interface.py # GUI-ready interface
- ConfigManager: Handles configuration loading and validation
- IMAPConnectionPool: Thread-safe connection pooling
- EmailAnalyzer: Pure logic for email analysis
- EmailProcessor: Orchestrates the 3-phase processing pipeline
- GUIInterface: Provides callbacks and threading for GUI apps
Emails are selected for cleanup based on:
- List-Unsubscribe Header: Industry standard for newsletters
- Subject Keywords: Configurable list of promotional terms
- Sender Domains: Specific domains to target for cleanup
Emails are protected from cleanup if they contain:
- Whitelist: Trusted senders and domains
- Protected Keywords: Invoice, bill, tax, etc.
- Flagged/Starred: User-marked important emails
- Phase 1: Parallel header fetching using dedicated worker threads
- Phase 2: Concurrent email analysis and decision making
- Phase 3: Parallel email moves/actions with proper error handling
- Auto-scaling: Uses 50% of CPU cores by default
- Connection pooling: Efficient IMAP connection reuse
- Batched operations: Optimized for large mailboxes
- Memory efficient: Processes emails in configurable batches
Example performance on 16-core system:
- 8 processing workers + 8 header fetch workers
- ~1000 emails processed in under 2 minutes
- <100MB memory usage
.
โโโ imap_cleanup/ # Main package
โโโ examples/ # Usage examples
โโโ tests/ # Test suite
โโโ docs/ # Documentation
โโโ legacy/ # Old implementations
โโโ config.json # Main configuration
โโโ config.local.json # Local overrides
โโโ whitelist.txt # Email whitelist
โโโ requirements.txt # Dependencies
python tests/test_config.pyThis project follows Python best practices:
- Type hints throughout
- Comprehensive docstrings
- Modular, single-responsibility design
- Clean separation of concerns
- Dry Run Mode: Test without making changes (default: enabled)
- Age Protection: Only processes emails older than specified days
- Whitelist Protection: Never touches whitelisted senders
- Keyword Protection: Safeguards important emails (invoices, bills)
- Backup Recommended: Always backup your mailbox before bulk operations
The GUIInterface class provides everything needed for GUI development:
from imap_cleanup.gui_interface import GUIInterface
gui = GUIInterface()
# Test connection
success, message = gui.test_connection()
# Get current configuration
config = gui.get_config()
# Start processing with callbacks
gui.start_processing(callback)See examples/example_gui_usage.py for a complete implementation.
examples/basic_usage.py- Simple programmatic usageexamples/config_examples.py- Configuration managementexamples/example_gui_usage.py- GUI development example
-
Authentication Failed
- Ensure you're using an app-specific password, not your regular iCloud password
- Generate one at https://appleid.apple.com/
-
Connection Timeout
- Check your internet connection
- Increase
search_timeoutin configuration
-
No Emails Found
- Check
age_dayssetting (default: 365 days) - Verify folder names in
source_folders
- Check
Enable verbose logging:
{
"cleanup_settings": {
"verbose": true
}
}- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- Built with Python's standard library for maximum compatibility
- Inspired by email management best practices
- Generated with Claude Code
- ๐ Check the documentation
- ๐ Report issues on GitHub Issues
- ๐ก Feature requests welcome!