Receive email
Goal: receive mail sent to an address, and decide per message whether to accept it, reject it, forward it to a person, or reply.
An email service goes in a service inside a context. The address is bound
on the header, from email("…"); the handler is an on message that takes the
InboundEmail and returns an EmailVerdict.
Decide a message
Section titled “Decide a message”context shop.support
consumes bynk.cloudflare { InboundEmail, EmailVerdict, EmailReply }
on message(mail: InboundEmail) -> Effect[EmailVerdict] { if mail.size > 10000000 { Reject("too large") } else if mail.subject == "refund" { } else { Reply(EmailReply { text: Some("Thanks, we have your message.") }) } }}The handler never calls a mail API: it returns a verdict and the runtime acts
on it. Accept ends it quietly, Reject(reason) refuses it for good,
Forward(to) passes it to one verified destination, and Reply(message)
answers the sender, with From, To and the threading headers filled in for
you.
Catch a whole domain
Section titled “Catch a whole domain”from email("*@shop.example") claims every address on the domain that no other
service names. Two services cannot claim the same address; a named address and
the catch-all together are fine, and the named address wins.
Do not trust from
Section titled “Do not trust from”mail.from is the envelope sender, and anyone can write any address there. The
handler’s default actor is Sender, which carries the address and whether
Cloudflare found the sender’s domain authenticated with DMARC
(DmarcPass, DmarcFail or DmarcNone). Forward or reply on a claim; act
on the evidence.
Deploy it
Section titled “Deploy it”bynk build --target workers gives the Worker an email() export and lists the
addresses in its wrangler.toml; bynk deploy pushes it, and Cloudflare Email
Routing delivers mail for those addresses to it. Turn Email Routing on for the
domain first, and verify any address you Forward to: a forward to an address
the account has not verified is refused (and falls back to
Reject("forward refused")).
The build’s package.json lists postal-mime, which the Worker parses messages
with, at an exact version. bynk dev, bynk deploy and bynk test install it
for you, with the package manager you already use, into the build directory
(.bynk/dev, .bynk/deploy, out), and only when it is missing. For the
output directory of a plain bynk compile, run your package manager’s install
there yourself. See Installing npm packages.
A reply goes out only for a sender whose mail passed DMARC, and only once per message. The runtime does the sending, so the handler stays a pure decision.
Try it locally
Section titled “Try it locally”bynk dev runs the Worker under wrangler dev, which takes a raw message:
--data-binary @message.emlWrite a small RFC 5322 file for message.eml. It must have a Message-ID header.
Test it
Section titled “Test it”suite shop.support { case "a refund goes to billing" { let r <- support.message(InboundEmail { subject: "refund", }) }}Fields after to have defaults, so a case writes only what it cares about.
bynk test runs it with no Worker and no mail.
See also: Reference — Inbound email.