Skip to content

Rovey/icloud-imap-cleanup

Repository files navigation

IMAP Mail Cleanup

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.

Python 3.9+ License: MIT Code style: black

โœจ Features

  • ๐Ÿš€ 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

๐Ÿš€ Quick Start

Prerequisites

  • Python 3.9 or higher
  • iCloud account with app-specific password
  • IMAP access enabled

Installation

  1. Clone the repository

    git clone https://github.com/username/imap-mail-cleanup.git
    cd imap-mail-cleanup
  2. Install dependencies

    pip install -r requirements.txt
  3. Set up credentials

    cp .env.example .env
    # Edit .env with your iCloud credentials
  4. Configure settings

    cp config.local.example.json config.local.json
    # Edit config.local.json as needed
  5. Run the cleanup

    # Command line interface
    python imap_cleanup_cli.py
    
    # Or launch the GUI
    python imap_cleanup_gui.py

๐Ÿ“‹ Configuration

Environment Variables

Create a .env file with your iCloud credentials:

IMAP_USER=your_email@icloud.com
IMAP_PASS=your_app_specific_password

Configuration Files

  • config.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"
  }
}

๐Ÿ› ๏ธ Usage

Graphical User Interface

# Launch the GUI application
python imap_cleanup_gui.py

# Or using installed package
pip install -e .
imap-cleanup-gui

The 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

Command Line Interface

# Basic usage (dry run by default)
python imap_cleanup_cli.py

# Using as installed package
pip install -e .
imap-cleanup

Programmatic Usage

from 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")

GUI Development

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())

๐Ÿ—๏ธ Architecture

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

Key Components

  • 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

๐ŸŽฏ How It Works

Email Detection

Emails are selected for cleanup based on:

  1. List-Unsubscribe Header: Industry standard for newsletters
  2. Subject Keywords: Configurable list of promotional terms
  3. Sender Domains: Specific domains to target for cleanup

Protection Mechanisms

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

Processing Pipeline

  1. Phase 1: Parallel header fetching using dedicated worker threads
  2. Phase 2: Concurrent email analysis and decision making
  3. Phase 3: Parallel email moves/actions with proper error handling

๐Ÿ“Š Performance

  • 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

๐Ÿ”ง Development

Project Structure

.
โ”œโ”€โ”€ 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

Running Tests

python tests/test_config.py

Code Style

This project follows Python best practices:

  • Type hints throughout
  • Comprehensive docstrings
  • Modular, single-responsibility design
  • Clean separation of concerns

๐Ÿ›ก๏ธ Safety Features

  • 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

๐ŸŽฎ GUI Development

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

  • examples/basic_usage.py - Simple programmatic usage
  • examples/config_examples.py - Configuration management
  • examples/example_gui_usage.py - GUI development example

๐Ÿ› Troubleshooting

Common Issues

  1. Authentication Failed

  2. Connection Timeout

    • Check your internet connection
    • Increase search_timeout in configuration
  3. No Emails Found

    • Check age_days setting (default: 365 days)
    • Verify folder names in source_folders

Debug Mode

Enable verbose logging:

{
  "cleanup_settings": {
    "verbose": true
  }
}

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

  • Built with Python's standard library for maximum compatibility
  • Inspired by email management best practices
  • Generated with Claude Code

๐Ÿ“ž Support


โš ๏ธ Important: Always run in dry-run mode first and backup your mailbox before bulk operations. This tool is designed to be safe, but email is irreplaceable.

About

No description, website, or topics provided.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages