Skip to content

About

CLI for bulk-managing Cloudflare Email Routing rules: create aliases, update destinations, list and delete.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

Cloudflare Email Routing Manager

A comprehensive Python application for managing Cloudflare Email Routing rules using object-oriented programming principles.

Features

  • 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

Project Structure

├── 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

Setup Instructions

1. Install Dependencies

pip install -r requirements.txt

2. Configure API Credentials

Create a .env file in the project root (copy from .env.example):

cp .env.example .env

Then edit the .env file with your Cloudflare credentials:

API_TOKEN=your_cloudflare_api_token
ZONE_ID=your_domain_zone_id
DOMAIN=yourdomain.com

Getting your credentials:

  1. Log in to your Cloudflare dashboard
  2. Go to "My Profile" > "API Tokens"
  3. Create a token with Email Routing permissions or use Global API Key
  4. Get your Zone ID from your domain's overview page

Usage

Interactive Mode

Run the application without arguments for an interactive menu:

python main.py

Command Line Mode

Create Email Rules

python main.py create destination@email.com 10

Update Destinations

python main.py update new-destination@email.com
python main.py update new-destination@email.com "filter_text"

Delete Rules

python main.py delete
python main.py delete "filter_text"

List Rules

python main.py list
python main.py list "filter_text"

Print Email Mappings from File

python main.py print email_rules_2024-01-01_12-00-00.json

Object-Oriented Design

Models (models/email_models.py)

  • EmailMatcher: Represents email matching criteria
  • EmailAction: Represents forwarding actions
  • EmailRule: Complete email routing rule
  • OperationResult: Result of API operations
  • EmailConfig: Configuration for API access

Services (services/cloudflare_service.py)

  • CloudflareEmailService: Low-level API interaction with Cloudflare

Managers (managers/email_manager.py)

  • EmailRuleManager: High-level business logic for email rule operations

Utilities (utils/helpers.py)

  • EmailGenerator: Random email generation
  • FileManager: JSON file operations
  • RuleFilter: Rule filtering and summary functions
  • UserInteraction: User input handling

Legacy Files

The following files are kept for backward compatibility but are deprecated:

  • app.py - Use main.py instead
  • change_destination.py - Use main.py update instead
  • delete_emails.py - Use main.py delete instead
  • print_emails.py - Use main.py print instead

Security Notes

⚠️ Important:

  • 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

Error Handling

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

File Output

All operations save results to timestamped JSON files:

  • email_rules_YYYY-MM-DD_HH-MM-SS.json - Created rules
  • updated_rules_YYYY-MM-DD_HH-MM-SS.json - Updated rules
  • deleted_rules_YYYY-MM-DD_HH-MM-SS.json - Deleted rules

About

CLI for bulk-managing Cloudflare Email Routing rules: create aliases, update destinations, list and delete.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages