Connecting email
The bot can answer a mailbox. An email becomes the same message every chat
platform produces, so the bot needs no changes: it reads the question, replies,
and the reply lands in the sender's thread with Re: in front of the subject.
Email needs two things from Cloudflare, one for each direction.
1. Receiving: Email Routing
- On the domain the address belongs to, open Email Routing and enable it. Cloudflare adds the MX and SPF records for you.
- Under Routing rules, create a rule for the support address
(
[email protected]) with the action Send to a Worker, and pick the bot's Worker.
No webhook URL is involved. Cloudflare hands each message to the Worker's
email() handler, which every bot built with createBotRuntime already has.
Does the domain's mail already go to Google Workspace? Do not enable Email Routing on the domain itself: it points the domain's MX records at Cloudflare, and every mailbox would stop receiving mail. Follow the next section instead.
Keeping Google Workspace: route one address to the bot
A domain has one set of MX records, so its mail goes either to Google or to
Cloudflare. The way to have both is a subdomain. Google keeps acme.com;
Cloudflare receives only help.acme.com; and Google forwards the one address
the bot answers.
-
Cloudflare: Email Routing on the subdomain only. Open Email Routing for
acme.com, go to Settings → Subdomains, and addhelp. Cloudflare adds MX and SPF records tohelp.acme.comalone; the root domain's Google MX records stay as they are. Check them in DNS afterwards: theacme.comMX records must still name Google. -
Cloudflare: the routing rule. Under Routing rules, create a rule for
[email protected]with the action Send to a Worker, and pick the bot's Worker. (A catch-all rule exists only for the root domain, so name the address.) -
Google: forward the one address. In the Google Admin console, open Apps → Google Workspace → Gmail → Routing, select the top organisational unit, and under Email forwarding using recipient address map add a rule mapping
[email protected]to[email protected]for All incoming messages. Leave Also route to original destination unchecked unless people should keep a copy in Google too. If[email protected]is not a mailbox yet, add it as an alias of an existing user first. Tick Add X-Gm-Original-To header too: it records the address the customer wrote to even when they put it inBcc. Google applies the change within 24 hours, usually much sooner. -
The bot: name both addresses.
EMAIL_ADDRESS[email protected],[email protected]The bot accepts mail by the address it was delivered to, and after the forward that is
[email protected]. Listing[email protected]as well stops the bot copying its own public address on every reply, because the customer'sTo:still says[email protected].
What to expect:
- Replies come from
[email protected]by default, the address the mail was delivered to. A customer's answer to it goes straight to Cloudflare, without Google. Onboardhelp.acme.comfor sending (or verify it with SES/Resend), and setEMAIL_FROM_NAMEso the customer sees Acme Support. - To reply from
[email protected]instead, setEMAIL_REPLY_FROM=writtenand onboardacme.comfor sending as well (Cloudflare Email Sending adds its own records and leaves Google's MX alone). The bot then replies from the address the customer wrote to: theX-Gm-Original-Toheader, else the bot's address inTo:orCc:. The customer's answer goes back through Google and its forward. With several public addresses mapped to one bot (support@,billing@), each customer hears back from the one they used. - Forward with the recipient address map, not with a Google Group. A group
marks the mail it passes on as list mail (
Precedence: list), and the bot drops list mail as automatic. A Gmail user's own forwarding setting works but needs a mailbox kept alive for it. - A forwarded sender is still checked. Google forwards the message
unchanged, so the sender's DKIM signature usually survives and DMARC passes.
When it does not, the event's
user.unverifiedistrue, as below. - Test from an address outside
acme.comtoo: with Only external incoming messages chosen in step 3, colleagues' mail would reach the Google mailbox instead of the bot.
2. Sending: pick a provider
Cloudflare Email Sending is the default, and it needs no API key. Onboard the
domain you send from, either in
Email Service or with
npx wrangler email sending enable acme.com. Then add the binding to the bot's
Wrangler configs:
"send_email": [{ "name": "EMAIL" }]
To send through Amazon SES or Resend instead, set EMAIL_PROVIDER and
that provider's secrets. The From domain must be verified with the provider.
wrangler secret put RESEND_API_KEY # EMAIL_PROVIDER=resend
wrangler secret put SES_ACCESS_KEY_ID # EMAIL_PROVIDER=ses
wrangler secret put SES_SECRET_ACCESS_KEY # plus SES_REGION as a variable
3. Variables
| Variable | Meaning |
|---|---|
EMAIL_ADDRESS |
The address(es) the bot answers, comma-separated. Absent ⇒ email is not configured. The first one sends new threads. |
EMAIL_FROM_NAME |
The display name on the bot's mail: Acme Support. |
EMAIL_PROVIDER |
cloudflare (default), ses or resend. |
EMAIL_BINDING |
The send_email binding's name. Defaults to EMAIL. |
EMAIL_ALLOWED_SENDERS |
Only these senders are answered: addresses, or @acme.com for a whole domain. Everyone else gets a bounce. |
EMAIL_SUBJECT |
The subject of a thread the bot starts itself. Defaults to Message. |
EMAIL_REPLY_FROM |
delivered (default): a reply comes from the address the mail was delivered to. written: from the bot's address the person wrote to — useful when another mail system forwards to the bot. Only ever one of EMAIL_ADDRESS. |
If EMAIL_ADDRESS is set but there is no complete way to send, the deployment
fails with a message naming what is missing. A bot that cannot reply would be
worse than one that does not start.
Plus-addresses are answered by their mailbox: [email protected] reaches
[email protected].
What the bot sees
| In the event | From the email |
|---|---|
channel.id |
The address a reply goes to (Reply-To, else From) |
message.threadId |
The thread's first message. Every reply in a thread is one conversation. |
message.text |
The new part of the email as Markdown, without the quoted history below it |
message.subject |
The subject line |
message.html |
The original HTML, untouched. Never draw it into a page. |
message.attachments |
The attached files, readable with conversation.download() for seven days |
message.raw.writtenTo |
The bot's address the person wrote to ([email protected] behind a forward), or null. A sender can type it, so route on it, never grant on it. |
The operator console keeps the Markdown as the message and the subject beside it. It also offers the original HTML as a download.
A sender is not proven by From:. Anyone can type any address there. Unless
the receiving server recorded a DMARC pass for the sender's domain, the event's
user.unverified is true. Greet such a sender by name, but do not act on the
address: look up their account, send a transcript, grant something.
Sending a subject
A message can carry a subject line on any platform. It is shown only where the platform has one, and today that is email:
await conversation.reply(message("Your invoice is attached.").subject("Your March invoice"));
Elsewhere the subject is dropped, never refused, so set it unconditionally.
conversation.canSetSubject says whether it will be shown. Without a subject a
reply goes out as Re: the thread's subject.
What email cannot do
| Capability | Why not |
|---|---|
buttons, selects, forms |
An email has no controls that answer the bot, so a reply goes out without its buttons and selects rather than not at all. A link button still works: it becomes a link. Check conversation.canSendButtons when a button was the only way to offer something. |
editMessage, deleteMessage |
A sent email cannot be changed or taken back. |
typing, status |
There is nothing to show while the bot works. |
reactions, ephemeral, mentions |
None of these exist in email. A mention reads as @name. |
directMessages is declared: conversation.directMessage("[email protected]", …)
starts a new thread with that address. So is includePeople:
conversation.includePeople(["[email protected]"], …) copies Carol on the thread,
and every later reply copies her too — until the person's own reply leaves her
out of its Cc, as in any email thread.
Loops
Automatic mail (out-of-office replies, bounces, mailing-list robots) is
recognised by its headers and never reaches the bot. The bot's own replies are
marked Auto-Submitted: auto-replied, so a person's out-of-office does not
answer them. Mail from the bot's own address is ignored. Without these checks,
a bot and a responder would write to each other forever.
When nothing arrives
- The routing rule sends the address somewhere else, or Email Routing is not enabled on the domain (its MX records are missing).
EMAIL_ADDRESSdoes not include the address the mail was sent to. The sender gets a bounce saying No one answers at this address.- The sender is not in
EMAIL_ALLOWED_SENDERS. - With Google Workspace in front: the recipient address map is not saved yet
(allow up to 24 hours), it affects only external mail, or
EMAIL_ADDRESSlacks the subdomain address ([email protected]) the mail is delivered to. - The mail was an automatic one. Write from a real mailbox to test.
When replies do not go out, check that the sending domain is onboarded (or
verified with SES/Resend) and that the send_email binding is in both
Wrangler configs.
Related: Capabilities, Deploying a bot, Connecting Slack, Connecting Microsoft Teams.