About
Fail-Safe Email Worker is a fully serverless email processing system built on Cloudflare's edge computing platform. It leverages Cloudflare Email Routing to receive emails, Cloudflare Workers to process them, and Cloudflare AI with Durable Objects to power an intelligent agent that analyzes customer emails and generates helpful replies. The agent can also interact with a Google Calendar MCP server (hosted as a separate Cloudflare Worker) for calendar integration. If delivery fails, emails are automatically saved to Cloudflare R2 Storage and alerts are sent via Discord webhook.
Key Features
🤖 AI-Powered Analysis
Uses Cloudflare AI agents to analyze incoming emails and generate intelligent, context-aware replies to customers.
📧 Smart Email Routing
Routes emails to different addresses based on recipient with support for exact matches, catch-all patterns, and global defaults.
💾 Automatic Backup
Automatically saves failed or spam emails to Cloudflare R2 bucket storage with full metadata for later retrieval and analysis.
🔔 Discord Alerts
Real-time notifications via Discord webhook when email delivery fails, including full error details and email content.
🔄 Fail-Safe Design
Comprehensive error handling ensures emails are never lost, with multiple fallback mechanisms in place.
📝 Email Threading
Maintains proper email threading with In-Reply-To headers and quoted message formatting for seamless conversations.
How It Works
- Email Reception: Cloudflare Email Routing receives incoming emails and routes them to the Cloudflare Worker.
- AI Analysis: The AI agent (running on Cloudflare Workers with Durable Objects) analyzes the email content to determine if an automated reply is appropriate. The agent can also leverage the Google Calendar MCP server (hosted as a separate Cloudflare Worker) for calendar-related queries.
- Reply Generation: If appropriate, the agent generates a helpful reply using Cloudflare AI and sends it to the customer via Mailgun.
- Email Forwarding: The original email (or agent-replied chain) is forwarded to the configured target address.
- Error Handling: If forwarding fails, the email is automatically saved to Cloudflare R2 Storage for backup and a Discord alert is sent.
Technology Stack
This application is built entirely on Cloudflare's edge computing platform, leveraging multiple Cloudflare products for a fully serverless, scalable solution:
Cloudflare Infrastructure
- Cloudflare Email Routing: Routes incoming emails directly to the worker, eliminating the need for traditional email servers.
- Cloudflare Workers: The main email processing worker handles all incoming emails and orchestrates the AI agent interactions. Additionally, a separate worker hosts the Google Calendar MCP (Model Context Protocol) server, providing calendar integration capabilities to the AI agent.
- Cloudflare AI: Powers the intelligent email analysis and automated reply generation using state-of-the-art language models.
- Cloudflare Durable Objects: Provides stateful, per-customer conversation context for the AI agent, enabling intelligent multi-turn conversations with email threading.
- Cloudflare R2 Storage: Automatically stores failed or spam emails in R2 buckets for backup, analysis, and compliance purposes. All email data is securely stored with full metadata.
Testing the Agent
You can test the AI-powered email agent by sending an email to agent@tanuj.xyz.
When you send an email to this address:
- Cloudflare Email Routing receives your email and routes it to the worker.
- The Cloudflare Worker extracts and analyzes your email content.
- The AI Agent (powered by Cloudflare AI and Durable Objects) processes your email and determines if an automated reply is appropriate.
- If the agent decides to reply, it generates a helpful response using AI and sends it back to you via Mailgun.
- The original email (with the agent's reply chain) is forwarded to the configured destination address.
- If any step fails, the email is automatically saved to Cloudflare R2 Storage for backup and analysis.
💡 Try it out: Send a customer service inquiry, question, or request to agent@tanuj.xyz and watch the AI agent respond intelligently!
Example Conversations
Product Inquiry
The agent answers product questions using the product catalog tools.
Consultation Scheduling
The agent schedules consultations using Google Calendar integration.
Configuration
Email routing is configured via environment variables in wrangler.jsonc:
{
"EMAIL_ROUTING": {
"user@domain.com": "target@domain.com",
"@domain.com": "catchall@domain.com",
"@default": "fallback@domain.com"
}
}
Supports:
- Exact matches: Route specific email addresses to target addresses
- Catch-all routing: Route all emails for a domain to a single address
- Global default: Fallback routing for unmatched emails
Monitoring & Alerts
The worker provides comprehensive monitoring capabilities:
- Cloudflare Workers logs for email processing status
- Cloudflare R2 bucket storage for failed email and spam backups
- Discord webhook alerts with detailed error information
- Real-time status tracking and error reporting
- Full email metadata stored in R2 for compliance and analysis
Source Code
This project is open source and available on GitHub:
The cf_ai_mail_sentinel repository contains the main email worker implementation, while the Google Calendar MCP Server repository contains the separate Cloudflare Worker that provides calendar integration capabilities.