Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/email-routing/configuration.md
1# Email Routing Configuration23## Wrangler Configuration45### Basic Email Worker67```jsonc8// wrangler.jsonc9{10"name": "email-worker",11"main": "src/index.ts",12"compatibility_date": "2025-01-01",13"send_email": [{ "name": "EMAIL" }]14}15```1617```typescript18// src/index.ts19export default {20async email(message, env, ctx) {21await message.forward("[email protected]");22}23} satisfies ExportedHandler;24```2526### With Storage Bindings2728```jsonc29{30"name": "email-processor",31"send_email": [{ "name": "EMAIL" }],32"kv_namespaces": [{ "binding": "KV", "id": "abc123" }],33"r2_buckets": [{ "binding": "R2", "bucket_name": "emails" }],34"d1_databases": [{ "binding": "DB", "database_id": "def456" }]35}36```3738```typescript39interface Env {40EMAIL: SendEmail;41KV: KVNamespace;42R2: R2Bucket;43DB: D1Database;44}45```4647## Local Development4849```bash50npx wrangler dev5152# Test with curl53curl -X POST 'http://localhost:8787/__email' \54--header 'content-type: message/rfc822' \55--data 'From: [email protected]56To: [email protected]57Subject: Test5859Body'60```6162## Deployment6364```bash65npx wrangler deploy66```6768**Connect to Email Routing:**6970Dashboard: Email > Email Routing > [domain] > Settings > Email Workers > Select worker7172API:73```bash74curl -X PUT "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/email/routing/settings" \75-H "Authorization: Bearer $API_TOKEN" \76-d '{"enabled": true, "worker": "email-worker"}'77```7879## DNS (Auto-Created)8081```dns82yourdomain.com. IN MX 1 isaac.mx.cloudflare.net.83yourdomain.com. IN MX 2 linda.mx.cloudflare.net.84yourdomain.com. IN MX 3 amir.mx.cloudflare.net.85yourdomain.com. IN TXT "v=spf1 include:_spf.mx.cloudflare.net ~all"86```8788## Secrets & Variables8990```bash91# Secrets (encrypted)92npx wrangler secret put API_KEY9394# Variables (plain)95# wrangler.jsonc96{ "vars": { "THRESHOLD": "5.0" } }97```9899```typescript100interface Env {101API_KEY: string;102THRESHOLD: string;103}104```105106## TypeScript Setup107108```bash109npm install --save-dev @cloudflare/workers-types110```111112```json113// tsconfig.json114{115"compilerOptions": {116"target": "ES2022",117"module": "ES2022",118"lib": ["ES2022"],119"types": ["@cloudflare/workers-types"],120"moduleResolution": "bundler",121"strict": true122}123}124```125126```typescript127import type { ForwardableEmailMessage } from "@cloudflare/workers-types";128129export default {130async email(message: ForwardableEmailMessage, env: Env, ctx: ExecutionContext): Promise<void> {131await message.forward("[email protected]");132}133} satisfies ExportedHandler<Env>;134```135136## Dependencies137138```bash139npm install postal-mime140```141142```typescript143import PostalMime from 'postal-mime';144145export default {146async email(message, env, ctx) {147const parser = new PostalMime();148const email = await parser.parse(await message.raw.arrayBuffer());149console.log(email.subject);150await message.forward("[email protected]");151}152} satisfies ExportedHandler;153```154155## Multi-Environment156157```bash158# wrangler.dev.jsonc159{ "name": "worker-dev", "vars": { "ENV": "dev" } }160161# wrangler.prod.jsonc162{ "name": "worker-prod", "vars": { "ENV": "prod" } }163164npx wrangler deploy --config wrangler.dev.jsonc165npx wrangler deploy --config wrangler.prod.jsonc166```167168## CI/CD (GitHub Actions)169170```yaml171# .github/workflows/deploy.yml172name: Deploy173on:174push:175branches: [main]176jobs:177deploy:178runs-on: ubuntu-latest179steps:180- uses: actions/checkout@v3181- uses: actions/setup-node@v3182- run: npm ci183- run: npx wrangler deploy184env:185CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}186```187