A comprehensive Python application for managing Cloudflare Email Routing rules using object-oriented programming principles.
- Create Email Rules: Generate multiple random email aliases that forward to a destination
- Update Destinations: Bulk update forwarding destinations for existing rules
- Delete Rules: Remove email routing rules with filtering options
- List Rules: View all rules with filtering and summary information
- File Operations: Save/load operations results to/from JSON files
- Interactive Mode: User-friendly CLI interface
- Command Line Mode: Direct command execution for automation
├── main.py # Main application entry point
├── config.py # Configuration file
├── models/
│ ├── __init__.py
│ └── email_models.py # Data models for email rules and operations
├── services/
│ ├── __init__.py
│ └── cloudflare_service.py # Cloudflare API interaction service
├── managers/
│ ├── __init__.py
│ └── email_manager.py # High-level email rule management
├── utils/
│ ├── __init__.py
│ └── helpers.py # Utility functions and classes
└── requirements.txt # Python dependencies
pip install -r requirements.txtCreate a .env file in the project root (copy from .env.example):
cp .env.example .envThen edit the .env file with your Cloudflare credentials:
API_TOKEN=your_cloudflare_api_token
ZONE_ID=your_domain_zone_id
DOMAIN=yourdomain.comGetting your credentials:
- Log in to your Cloudflare dashboard
- Go to "My Profile" > "API Tokens"
- Create a token with Email Routing permissions or use Global API Key
- Get your Zone ID from your domain's overview page
Run the application without arguments for an interactive menu:
python main.pypython main.py create destination@email.com 10python main.py update new-destination@email.com
python main.py update new-destination@email.com "filter_text"python main.py delete
python main.py delete "filter_text"python main.py list
python main.py list "filter_text"python main.py print email_rules_2024-01-01_12-00-00.json- EmailMatcher: Represents email matching criteria
- EmailAction: Represents forwarding actions
- EmailRule: Complete email routing rule
- OperationResult: Result of API operations
- EmailConfig: Configuration for API access
- CloudflareEmailService: Low-level API interaction with Cloudflare
- EmailRuleManager: High-level business logic for email rule operations
- EmailGenerator: Random email generation
- FileManager: JSON file operations
- RuleFilter: Rule filtering and summary functions
- UserInteraction: User input handling
The following files are kept for backward compatibility but are deprecated:
app.py- Usemain.pyinsteadchange_destination.py- Usemain.py updateinsteaddelete_emails.py- Usemain.py deleteinsteadprint_emails.py- Usemain.py printinstead
- Store API tokens securely
- Don't commit credentials to version control
- Use environment variables for production deployments
- The API token provides access to your Cloudflare account
The application includes comprehensive error handling:
- API rate limiting with configurable delays
- Validation of input parameters
- Graceful handling of network issues
- Detailed error messages and logging
All operations save results to timestamped JSON files:
email_rules_YYYY-MM-DD_HH-MM-SS.json- Created rulesupdated_rules_YYYY-MM-DD_HH-MM-SS.json- Updated rulesdeleted_rules_YYYY-MM-DD_HH-MM-SS.json- Deleted rules