Route emails
Set up email routing to forward incoming emails to existing mailboxes or process them with Workers.
Route incoming emails sent to your domain to existing mailboxes, Workers for processing, or other destinations.
- Sign up for a Cloudflare account ↗.
- Install
Node.js↗.
Node.js version manager
Use a Node version manager like Volta ↗ or nvm ↗ to avoid permission issues and change Node.js versions. Wrangler, discussed later in this guide, requires a Node version of 16.17.0 or later.
Before using Email Routing, you need to configure your domain.
-
In the Cloudflare dashboard, go to Email Routing.
Go to Email Routing -
Select Onboard Domain.
-
Choose a domain from your Cloudflare account.
-
Select Continue to proceed with DNS configuration.
-
Select Add records and onboard. This will add the following DNS records to your root domain:
- MX records to route incoming emails to Cloudflare.
- TXT record for SPF to authorize email routing.
- TXT record for DKIM to provide authentication for routed emails.
Once your domain is onboarded, you can start routing emails.
You can route your first email by setting up forwarding rules in the dashboard, or by processing emails with Workers.
The simplest way to route emails is forwarding them to existing email addresses.
-
In the Cloudflare dashboard, go to Email Routing.
Go to Email Routing -
Select the domain you want to create an email address for.
-
Select the Routing Rules tab.
-
Select Create Address.
-
Configure your first rule (for instance, forwarding emails to
support@yourdomain.comto your personal email address):- Custom address: Enter the local part of the email (for example,
supportforsupport@yourdomain.com) - Action: Send to an email
- Destination: Your personal email address (for example,
your-email@gmail.com)
- Custom address: Enter the local part of the email (for example,
-
Select Save.
Verify that your routing rule is working:
-
Send an email from another email account to your newly created address (for example,
support@yourdomain.com). -
Check the destination inbox for the forwarded email.
-
If you do not see the email right away, check your spam folder.
Use Workers to process emails with custom logic before forwarding or responding.
-
Create a new Worker project:
Terminal window npm create cloudflare@latest email-processorWhen prompted, select "Hello World" Worker as the template. Then navigate to the project directory:
Terminal window cd email-processor -
Install the required package for creating email replies:
Terminal window npm install mimetext -
Add the
nodejs_compatcompatibility flag to your Wrangler configuration file. This is required for themimetextpackage:JSONC {"$schema": "./node_modules/wrangler/config-schema.json","compatibility_flags": ["nodejs_compat"]}TOML compatibility_flags = ["nodejs_compat"] -
Create your email handler in
src/index.ts:TypeScript import { EmailMessage } from "cloudflare:email";import { createMimeMessage } from "mimetext";// ============================================// Configuration - Update these values// ============================================const YOUR_DOMAIN = "yourdomain.com"; // Replace with your verified domainconst FORWARD_TO_EMAIL = "your-team@company.com"; // Replace with where you want emails forwardedexport default {async email(message, env, ctx): Promise<void> {const sender = message.from;const recipient = message.to;const subject = message.headers.get("subject") || "";console.log(`Processing email from ${sender} to ${recipient} with subject ${subject}`,);// Route based on recipientif (recipient.includes("support@")) {// Send auto-replyconst msg = createMimeMessage();const messageId = message.headers.get("Message-ID");if (messageId) {msg.setHeader("In-Reply-To", messageId);}msg.setSender({name: "Support Team",addr: `support@${YOUR_DOMAIN}`,});msg.setRecipient(message.from);msg.setSubject("We received your message");// Add plain text versionmsg.addMessage({contentType: "text/plain",data: "Thank you for contacting support. Your ticket number is 123.\n\nA member of our support team will get back to you shortly.",});// Add HTML versionmsg.addMessage({contentType: "text/html",data: "<p>Thank you for contacting support. Your ticket number is <strong>123</strong>.</p><p>A member of our support team will get back to you shortly.</p>",});const replyMessage = new EmailMessage(`support@${YOUR_DOMAIN}`,message.from,msg.asRaw(),);await message.reply(replyMessage);// Forward to support teamawait message.forward(FORWARD_TO_EMAIL);} else {// Default: forward to adminawait message.forward(FORWARD_TO_EMAIL);}},} satisfies ExportedHandler<Env>; -
Deploy your Worker:
Terminal window npm run deploy
-
In the Cloudflare dashboard, go to Email Routing.
Go to Email Routing -
Select the domain you want to configure routing for.
-
Select the Routing Rules tab.
-
Select Create Address.
-
Configure Worker routing:
- Custom address: Enter the local part of the email (for example,
supportforsupport@yourdomain.com) - Action: Send to a Worker
- Worker: Select your
email-processorWorker
- Custom address: Enter the local part of the email (for example,
-
Select Save.
After configuring the routing rule, test that it works:
-
Send an email from your personal email account to the address you configured (for example,
support@yourdomain.com). -
Check your
FORWARD_TO_EMAILinbox for the forwarded email. -
If the recipient email was
support@, you should also receive an auto-reply at your personal email address.
Now that you can route emails, explore advanced features:
- Send outbound emails - Send emails from your applications
- API reference - Complete routing API documentation
- Examples - Real-world routing patterns