cloudflare email telegram
via PatrickJS/awesome-cursorrules
Set up email-to-Telegram forwarding via Cloudflare Email Routing and Workers using mail2tg CLI.
What is cloudflare email telegram?
This rule guides you through deploying a Cloudflare Worker that automatically forwards incoming emails to a Telegram bot. Use it when you need to route domain emails to Telegram without running local services, leveraging Cloudflare's free tier (100K emails/day).
- Deploy a Cloudflare Worker that parses incoming MIME emails and forwards headers, body, and attachments (up to 50 MB) to Telegram
- Create MX and SPF DNS records so Cloudflare receives mail for your domain
- Set up Email Routing rules directing configured mailbox addresses to the Worker
- Store Telegram bot credentials securely as Worker secrets
- Provide plan-first and interactive workflows with explicit approval gates before applying changes
- Diagnose setup issues with doctor checks and exit codes for troubleshooting
Applies to
File patterns this rule matches.
Rule definition (reference)
Source of truth, from the repository.
You are an expert at setting up email-to-Telegram forwarding using Cloudflare Email Routing and Workers.
When the user asks to set up email forwarding to Telegram, route domain email to Telegram, or mentions "mail2tg", "email to telegram", follow this workflow.
Tool
Use the mail2tg CLI published on npm: https://www.npmjs.com/package/mail2tg
Source: https://github.com/shatzibitten/mail2tg
Prerequisites
- Domain DNS managed by Cloudflare (nameservers pointing to Cloudflare, status "Active").
- Cloudflare API token with scopes: Zone Read, DNS Edit, Worker Scripts Edit, Email Routing Rules Edit. Create at dash.cloudflare.com/profile/api-tokens → "Create Custom Token".
- Telegram bot created via @BotFather, token copied. User has sent /start to the bot.
- Node.js >= 20.
Plan-first workflow
export CLOUDFLARE_API_TOKEN="<token>"
export TELEGRAM_BOT_TOKEN="<bot-token>"
# Replace <reviewed-version> only after reviewing the npm package and source.
MAIL2TG_DOMAIN=example.com \
MAIL2TG_MAILBOX=info@example.com \
npx -y mail2tg@<reviewed-version> init --json
npx -y mail2tg@<reviewed-version> plan --json
# Stop here, show the plan to the user, and wait for explicit approval.
npx -y mail2tg@<reviewed-version> apply --json
npx -y mail2tg@<reviewed-version> doctor --json
Interactive workflow
npx -y mail2tg@<reviewed-version> init
export CLOUDFLARE_API_TOKEN="<token>"
export TELEGRAM_BOT_TOKEN="<bot-token>"
npx -y mail2tg@<reviewed-version> plan
# Stop here, show the plan to the user, and wait for explicit approval.
npx -y mail2tg@<reviewed-version> apply
npx -y mail2tg@<reviewed-version> doctor
What it does
- Deploys a Cloudflare Worker that parses incoming emails (MIME) and forwards headers, body, and attachments (up to 50 MB) to a Telegram chat via Bot API.
- Creates MX and SPF DNS records so Cloudflare receives mail for the domain.
- Creates an Email Routing rule directing the configured address to the Worker.
- Sets Worker secrets (TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID).
After apply, nothing runs locally. Everything is serverless on Cloudflare's edge. Free tier covers 100K emails/day.
Exit codes
- 0: success
- 2: missing config or env vars
- 3: Cloudflare/Telegram API error
- 4: doctor checks failed
- 5: worker deployment failed
Common issues
- "Telegram chat_id not found" → user must send /start to the bot, then re-run.
- "Cloudflare zone not found" → domain not on Cloudflare or token lacks Zone Read scope.
- "Worker deployment failed" → check internet; run
npx wrangler whoamito debug.
Related rules
Senior full-stack TypeScript, React, Node.js guidance with clean architecture, testing, and WHY-oriented reasoning.
Quantitative factor research skills for designing, evaluating, and mining alpha factors in equities markets.
Android development with Jetpack Compose, clean architecture, and Material Design 3.
Angular development with Novo Elements UI library using standalone components.
Expert Angular 18 + TypeScript development with Jest, emphasizing clean code and performance.
Manage Kubernetes clusters, add-ons, stacks, and credentials via the Ankra CLI platform.