A practical field guide from Automation Ace.
The short answer
With Cloudflare Email Routing and Email Workers, mail sent to an address on your domain can run code. Enable Email Routing for the domain, create a Worker with an email() handler, and route an address (like invoices@yourdomain.com) to it. The handler can read the sender, recipient, headers, and raw message, then forward it, reject it, or trigger any workflow by calling an API or webhook.
Compare with mailbox-based approaches in automating email without Gmail or Outlook and choosing a Zapier email tool.
How Email Workers work
- Email Routing receives mail for your domain (Cloudflare manages the MX records).
- A routing rule sends a specific address, or a catch-all, to your Worker.
- The
email()handler runs with a message object that exposes the sender, recipient, headers, raw content, and size. - Your code decides: forward to a verified address, reject with a reason, reply where supported, or call other services.
Setup
- In the Cloudflare dashboard, enable Email Routing for your domain and add the DNS records it suggests. If the domain already receives mail elsewhere, plan carefully: changing MX records moves all incoming mail. A subdomain can isolate automation mail.
- Add and verify any destination address you will forward to.
- Create a Worker with the handler below and deploy it.
- Create a routing rule that sends your chosen address to the Worker.
// src/index.js — route and process incoming email
export default {
async email(message, env, ctx) {
const from = message.from;
const to = message.to; // e.g. invoices@yourdomain.com
const subject = message.headers.get('subject') || '';
// Block unwanted senders
if (from.endsWith('@spam.example')) {
message.setReject('Address not accepted');
return;
}
// Read the raw message (headers + body) for parsing
const raw = await new Response(message.raw).text();
if (to.startsWith('invoices@')) {
// Log to a webhook (Zapier Catch Hook, Slack, or your own API)
ctx.waitUntil(fetch(env.INVOICE_WEBHOOK_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ from, subject, received: new Date().toISOString(), size: message.rawSize }),
}));
}
// Always keep a copy in a human inbox (must be a verified destination)
await message.forward(env.FORWARD_TO);
},
};
Parsing the message
message.raw is the full MIME message. For simple needs, read headers like subject and date directly. For bodies and attachments, use a MIME parsing library bundled with your Worker, then extract what you need with string logic, a regex, or Workers AI. Always check sensitive content before sending it to AI; see detecting PII.
Email-triggered automation ideas
- Invoice intake: vendors email
invoices@; the Worker logs each one and forwards to accounting. - Lead capture: website builders that only email leads can feed a CRM. See connecting two apps.
- Support routing: classify messages and forward to the right team.
- Per-workflow addresses: unique addresses per client or form, similar to plus addressing.
- Alert ingestion: system emails become Slack alerts or tickets.
- Spam and sender control: reject unknown senders for automation-only addresses.
Reliability and security tips
- Keep a human copy: forward to a real inbox while you build confidence.
- Treat email as untrusted: senders can be spoofed; do not take high-risk actions from email alone. Check authentication results in headers where appropriate, and add a human approval for sensitive actions.
- Use
ctx.waitUntil()for follow-up calls so the handler finishes quickly. - Deduplicate by Message-ID when a downstream step must run once. See deduplication.
- Heavier processing: hand off to a Workflow or Queue.
Frequently asked questions
Can Cloudflare trigger a workflow when an email arrives?
Yes. Enable Email Routing for your domain, route an address to an Email Worker, and its email() handler runs for each message, where you can forward, reject, or call APIs and webhooks.
What can an Email Worker do with a message?
It can read the sender, recipient, headers, size, and raw MIME content, forward the message to a verified destination address, reject it with a reason, and call other services.
Do I need to change my MX records to use Email Workers?
Email Routing handles incoming mail for the domain, which involves Cloudflare-managed MX records. If your domain already receives mail elsewhere, consider using a subdomain for automation addresses.
How do I parse email bodies and attachments in a Worker?
Read message.raw and parse the MIME content with a parsing library bundled into your Worker, then extract fields with string logic, regex, or AI.
Is Cloudflare Email Routing free?
Cloudflare offers Email Routing on its free plan at the time of writing, and Email Workers run under Workers pricing and limits. Check Cloudflare's current documentation.
Disclaimer: Zapier features, plan availability, and settings can change. Confirm current details in Zapier's help documentation and Cloudflare's developer documentation and pricing pages. Limits and plan features change over time. Code samples are simplified starting points; add your own validation, error handling, and security review before production use before relying on a specific setting. This article may include links to apps, products, or services; some links may be affiliate links, which means Automation Ace may earn a commission at no extra cost to you.