Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 195 additions & 0 deletions docs/architecture/decisions/0029-email-replies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
# ADR-0029: Replies by email when the visitor has left, and answers by email back into the chat

**Status:** Accepted, 2026-12-04, by the owner, who chose: every visitor who gave an email address gets reply
emails, not only those who ticked the follow-up box; the switch is on by default; the assistant's replies are emailed
as members' are; and whether the visitor is connected decides how soon the email goes. One choice, what to do with an
answer from [a different address](#still-open), is open until step 2.

## Context

A website visitor who closes the page does not see the team's reply until they come back to that website in the
same browser. The only email OpenChat sends them is the follow-up: one fixed text with no message in it, to visitors
who ticked a box, when a conversation has been quiet for a set time
([amendment A15](../spec-review.md#amendments-to-the-specification)). Answering that email does nothing, because
OpenChat receives no email.

The owner asked for the two halves that other inboxes have: the team's reply goes to the visitor's email when they
have left, and the visitor's answer to that email continues the same conversation.

What the platform gives, read from Cloudflare's documentation on 2026-12-04 and **not yet seen working**:

- **Receiving.** Cloudflare Email Routing, now part of Email Service, hands an incoming email to a Worker's
`email(message, env, ctx)` handler, with the envelope sender and recipient, the headers, and the raw message as a
stream that can be read once. Up to 25 MiB. `setReject` refuses the email while the sending server is still
connected. It is on the free and paid plans.
- **Addresses.** A rule for a literal address can send to a Worker, on a subdomain too. A catch-all works only on the
apex domain. Subaddressing can be turned on, and then `reply+anything@mail.example.com` matches the rule for
`reply@mail.example.com`, with the whole address kept in `message.to`. Wrangler 4.113 and later sets the rules from
an `addresses` field on deploy. The project pins 4.162.
- **Sending and receiving on one domain.** Sending puts its MX, SPF and DKIM records under `cf-bounce.` names, and
receiving puts MX on the domain itself with another DKIM selector. The documentation does not say outright that one
subdomain can do both.
- **Who sent it.** Cloudflare checks SPF, DKIM and DMARC before the Worker runs and adds an `Authentication-Results`
header. The documentation does not say that failing email is refused.
- **Headers on what we send.** `In-Reply-To`, `References`, `Auto-Submitted` and any `X-` header are allowed.
`Message-ID` is Cloudflare's to set. Reply-To is a field of its own, which
[`EmailProvider`](../../../packages/email/src/index.ts) already carries.
- **Parsing.** Cloudflare recommends the `postal-mime` package for reading an email in a Worker.
- **Resend** can receive too: an `email.received` webhook, signed, without the body, which is then fetched by API.

## Decision

Both halves are built for **website conversations only**. WhatsApp, Messenger and Instagram deliver replies
themselves.

### Sending a reply by email

A workspace has a new switch on its **Email** tab, **Email replies to visitors who have left**, on unless turned
off, with a wait in minutes, 5 unless changed. It does nothing on an installation without email.

When a public message from a member or from the AI assistant is committed in a website conversation whose contact has
an email address, a check is scheduled for one minute later, by a column and the sweep that follow-ups already use.

- **The read cursor decides whether.** An email is sent only if the visitor's read cursor is still behind that
message. A visitor who has read the reply never gets it by email.
- **The connection decides when.** The check asks the conversation's `ConversationRoom` whether the visitor has a
socket open. With none, the visitor has left, and the email goes then, a minute after the reply. With one, the
visitor is still on the website with the reply unread, perhaps in another tab, and the check comes back at the end
of the wait and sends then if it is still unread.

A socket is a hint, not a fact ([rule 3](../../../AGENTS.md#rules-that-are-easy-to-break)): a phone that locks drops
it, and a tab left open keeps it. So it only ever moves the email earlier or later within the wait, and the minute
lets a visitor who is moving between pages connect again. Follow-ups go on ignoring sockets
([M4](../../plan/milestones.md#m4-exit-check)).

Every visitor who gave an email address gets reply emails. A reply to their own question is a service message, not
marketing, so it does not need the follow-up tick box, which stays for follow-ups. The widget says under its email
field that replies are emailed to a visitor who has left. The owner checks that wording with legal advice, as for the
consent wording.

One email carries every public message from the team and the assistant that the visitor has not read and that no
earlier email carried. So five quick replies make one email, not five.

The email:

- is from the workspace's name at the system address, as follow-ups are;
- has one subject for the whole conversation, so that mail apps keep it in one thread, with `In-Reply-To` and
`References` if the first real sends show that the provider's message ID can be used for them;
- shows each reply with who wrote it, and marks the assistant's as the assistant's. If the reply was translated for
the visitor ([ADR-0024](0024-automatic-translation.md)), it shows the translation they would have seen in the widget;
- **never quotes what the visitor wrote.** The address was typed into the widget and never proven. An email that
repeated the visitor's text would let anyone send words of their choice to any address from our domain;
- carries no internal note, and is built only from `visibility = 'public'` rows
([rule 4](../../../AGENTS.md#rules-that-are-easy-to-break));
- has its fixed words in the visitor's language, a link back to the website, and the unsubscribe link and headers
follow-ups have. `X-Auto-Response-Suppress` asks mail servers not to answer with an out-of-office reply;
- lists attachments by name, with a line saying they are in the chat. It attaches nothing.

It is sent through `deliverOnce`, keyed by the conversation and the last message it carries, so a repeated job sends
nothing twice. An address on the workspace's list of addresses not to email gets nothing, checked just before
sending. A conversation gets at most 20 reply emails a day, and an address at most 50, so that a mistake or an abuser
cannot flood a mailbox.

**Follow-ups stay.** When a reply email was sent for a conversation's last message, the follow-up for that same
message is skipped, so the visitor does not get two emails about one reply.

This supersedes two things: amendment A15's "email has two uses", which becomes three, and the M4 limit that email
has no message text.

### Receiving the answer

When replies by email are set up for the installation, the reply email's Reply-To is
`reply+<token>@<reply domain>`. The token is 32 random bytes, made when a conversation's first reply email is sent,
written in lower-case base32 because mail servers may change the case of an address. Only its SHA-256 hash is stored,
in a new table `conversation_reply_addresses`, which goes with its conversation, so erasure and retention reach it.
The token is the proof that the sender was sent the email. It names no workspace and no conversation.

The Worker's `email` handler does as little as it can:

1. An address without a known token is refused with `setReject`. So is an email over 10 MiB, and one for a
conversation that has been deleted.
2. An automatic email is dropped without an answer: one with `Auto-Submitted` other than `no`, `Precedence` of
`bulk`, `list` or `junk`, or an empty return path. This is what stops an out-of-office reply and a bounce from
becoming messages, and stops two systems writing to each other for ever.
3. Anything else is written to R2 as it came, and an `email.inbound` job is queued. The handler returns only after
both, so an accepted email is not lost.

The job reads the email with `postal-mime` and writes a message in the conversation as the visitor, in one batch, with
the token and the email's `Message-ID` as its idempotency key, so a redelivered email or a retried job writes nothing
twice ([rule 7](../../../AGENTS.md#rules-that-are-easy-to-break)). Then it deletes the stored email. An email whose job
keeps failing stays for 7 days, for the failed-jobs page to replay, and is then removed by a lifecycle rule.

- **The text** is the plain part, or the HTML part turned into text. What the visitor's mail app quoted beneath their
answer is cut: everything from the line that introduces the quote. When the cut is not certain, more is kept, not
less.
- **Attachments** are kept as attachments, under the widget's own limits on kind and size. One that is refused
becomes a line saying an attachment came by email and was not kept.
- **It is a visitor message in every other way.** It is marked **by email** in the inbox. A resolved conversation
opens again, as it does when a visitor writes on the website. Members get their push notification. The assistant
answers if it is answering that conversation, translation runs, and the answer goes back by email after the wait,
since the visitor is not in the widget to read it.
- **A different sender.** People answer from another address than the one they typed, and forward email. The message
is accepted on the token alone, and when the sender's address is not the contact's, the inbox shows which address it
came from. Later reply emails still go only to the contact's address.
- A token takes at most 30 emails an hour.

No full message, address or token is logged ([rule 11](../../../AGENTS.md#rules-that-are-easy-to-break)).

### Providers and setting up

- **Cloudflare first.** An installation that sends with Cloudflare Email Sending turns receiving on with one more
option at deploy, naming the reply address, such as `reply@mail.company.com`. `oc deploy` writes the `addresses`
field and the R2 lifecycle rule. Turning Email Routing and subaddressing on for the domain is a step in the guide
if the CLI cannot do it.
- **Without receiving** — an installation on Resend, or on Cloudflare without the option — reply emails are still
sent. They have no Reply-To token, and say that the way to answer is on the website.
- **Resend receiving** is a later step and a separate decision.
- **OpenChat Cloud** receives on a subdomain of `openchat.run`, because the apex has the company's own mailboxes.

`postal-mime` is a new dependency, which this ADR approves if accepted
([working agreement](../../../AGENTS.md#working-agreement)). It has no dependencies of its own and is under the MIT No
Attribution licence.

### Data and privacy

- New: `conversation_reply_addresses` (workspace, conversation, token hash, created); the workspace's two settings;
a per-conversation column for when the reply check is due and which message the last email reached; a message's
metadata saying it came by email and, when it differs, from which address. Every migration is additive
([rule 8](../../../AGENTS.md#rules-that-are-easy-to-break)).
- **Message content now passes through the email provider.** Until now it got an address and a fixed text. With this
on, it gets the team's replies, and Cloudflare Email Routing handles the visitor's answers on their way in. The
[privacy guide](../../guide/privacy.md) changes with step 1, and Cloud's privacy notice and data processing
agreement need a new version before the switch can be used there.
- The email log keeps a hash of the address and no content, as now.

### Steps

1. **Reply emails out.** The setting, the check, the email in the widget's 11 languages, the follow-up skip, limits,
guide and privacy guide. Verified with real sends, including what threads in Gmail and Apple Mail.
2. **Answers in.** The handler, tokens, parsing, the inbox label, `oc deploy`. Verified on real Cloudflare first of
all for the three things the documentation leaves open: sending and receiving on one subdomain, subaddressing on a
subdomain, and what Cloudflare does with an email that fails DMARC. If one of them fails, this step stops and
the ADR is amended before more is built.
3. **Cloud and the apps.** The reply subdomain and the new legal versions on Cloud, and the **by email** label in
the phone apps.

## Still open

**An answer from a different address.** The reply address holds the conversation's token, so an answer reaches the
right conversation whichever address it was sent from. The question is only whether to take it. This document assumes
the recommendation: accept it and show the address it came from. The other choice is to refuse it, which is safer
against a forwarded email and loses real answers from people with several addresses. The owner decides before step 2.

## Consequences

- A visitor can go on with a conversation from any device, by email, without the website.
- The inbox gets messages whose text was cut from an email by rules that will sometimes be wrong. The rule to keep
more when unsure makes the mistake a long message, not a lost one.
- An unproven address can now receive the team's words. Never quoting the visitor, the daily limits and the
unsubscribe link bound what a prankster can do with that. They do not remove it.
- Email becomes a way into the inbox for anyone holding a reply email. Tokens, the hourly limit and dropping automatic
email are the whole defence, and they need tests of their own.
- More email is sent. Cloudflare includes 3,000 a month and then charges $0.35 per 1,000
([Cloudflare validation](../cloudflare-validation.md)). Cloud's plans may need an email allowance, as Messenger got
one. That is a later decision.
- Installations on Resend get half the feature until Resend receiving is built.
Loading