A request form on a site, the mail an app sends, and the mail people send back all go through one mail service that you run in your own account. Each app registers itself from its own repository. Adding the twentieth app changes nothing in the service’s repository and does not redeploy it.
| Piece | Where it is declared | What it holds |
|---|---|---|
replicator.MailService | the repository that runs the service | the service, its registry, its two keys, the mail provider, the inbound domain |
replicator.MailClient | each app’s own replicator.config.ts | that app’s registry entry, its token, its inbound address and anti-abuse widget, removed with the app’s own stage |
How it scales
- An app is data, not code. Its stage writes one registry entry: its forms, its operator inboxes, its webhook, its limits. The service reads it at run time.
- One provider choice. The service chooses how mail goes out:
cloudflare,aws-ses,http(a mail API such asresendorpostmark, orgeneric), orlog. No app names a provider, so switching it is one line in one file. - Nothing shared between apps. Each app’s token is minted in its own stage and only its hash is registered. Webhooks are signed with the service’s key, and apps verify with its public half. A secret the service must use for an app (the widget’s key) is sealed so only the service can open it.
- Limits per app. Each app has its own rate limit, daily caps and delivery queue. One app reaching its cap does not affect another. The service sets the maximum; an app can only lower it.
- Inbound with no rule to add by hand. Each app gets
{alias}@{inbound domain}from its own stage. Receiving is turned on for a subdomain, so the apex domain’s own mail is never touched.
Run the service
In the repository that runs it, once per stage:
const zoneId = "<the zone's id>";
const mail = new replicator.MailService("mail", {
accountId: ACCOUNT_ID,
scriptName: "example-mail", // never changes after the first deploy
hostname: "mail.example.com",
zoneId,
from: "[email protected]",
outbound: { provider: "cloudflare", recipients: "verified" },
inbound: { zoneId, domain: "in.example.com" },
allowOpenRecipients: true, // apps may mail people other than their operators
protect: ctx.protect,
});
return { ...mail.outputs }; // plain outputs only: the URL, ids, public keys
recipients: "verified" means the account can reach only its own verified
addresses: operator inboxes and inbound forwarding work, and mail to anyone else
(a confirmation to the person who asked) is skipped, never failed. To reach
any address use recipients: "any" (it needs a paid plan and a verified sending
domain), or aws-ses or http:
outbound: {
provider: "aws-ses",
region: "eu-west-1",
accessKeyId: ctx.secret("mailSesAccessKeyId"),
secretAccessKey: ctx.secret("mailSesSecretAccessKey"),
},
// or
outbound: { provider: "http", preset: "resend", token: ctx.secret("mailHttpToken") },
Add an app
In the app’s own replicator.config.ts, after the service’s stage is deployed:
export default replicator.app({
name: "acme",
stages: { staging: {}, production: {} },
// The service's plain outputs this config reads.
dependsOn: replicator.mailServiceEdges("example", ["staging", "production"]),
run(ctx) {
const mail = new replicator.MailClient("mail", {
accountId: ACCOUNT_ID,
app: ctx.app,
stage: ctx.stage,
service: replicator.mailServiceFrom(ctx, "example", ctx.stage),
displayName: "Acme",
origins: ["https://acme.example"],
thanksUrl: "https://acme.example/demo/thanks",
privacyUrl: "https://acme.example/privacy",
notify: ["[email protected]"], // verified operator inboxes
webhook: { url: "https://api.acme.example/api/webhooks/mail" }, // optional
forms: {
demo: {
workEmail: true,
fields: [
{ name: "name", type: "text", required: true },
{ name: "email", type: "email", required: true },
{
name: "team_size",
type: "select",
options: ["1-10", "11-50", "51+"],
},
{ name: "message", type: "textarea", max: 2000 },
],
},
},
inbound: {}, // [email protected]
});
return { demoFormUrl: mail.formUrl("demo") };
// An app with a backend puts mail.env() in its stage's environment (__env):
// MAIL_SERVICE_URL, MAIL_CLIENT_ID, MAIL_CLIENT_TOKEN.
},
});
Then put the form on the site:
<form
method="post"
action="https://mail.example.com/v1/forms/acme.production/demo"
data-mail-form
>
<input name="name" required />
<p data-mail-error="name"></p>
<input name="email" type="email" required />
<p data-mail-error="email"></p>
<input name="_hp" tabindex="-1" autocomplete="off" hidden />
<p data-mail-error="_form"></p>
<button type="submit">Request a demo</button>
</form>
<div data-mail-thanks hidden>Thanks. We have your request.</div>
<script src="https://mail.example.com/v1/kit.js" defer></script>
Without the script the form still posts and the visitor lands on thanksUrl.
With it, errors show inline and the widget is rendered. The anti-abuse check
needs the script, so keep an email address on the page as the alternative.
A registry entry takes up to a minute to reach every location after the app’s deploy.
The wire, version 1
The definition is packages/replicator/src/mail/contract.ts; the shared test
vectors are packages/replicator/src/mail/vectors.json. A receiver in another
repository checks its verifier against a copy of them.
| Call | Who calls it | Authentication |
|---|---|---|
GET /v1/forms/{clientId}/{form} | the form script | none (public fields and the widget’s public key) |
POST /v1/forms/{clientId}/{form} | a browser | the app’s origins, the widget, limits |
POST /v1/send/{clientId} | an app’s backend | Authorization: Bearer mk_… |
POST {webhook.url} | the service | x-mail-signature (Ed25519) over v1.{ts}.{deliveryId}.{sha256(body)} |
mail to {alias}@{inbound domain} | anyone | forwarded to notify, then a kind: inbound webhook |
A clientId is {app}.{stage}. Errors are { error: { code, message, details? } }
and callers branch on code. A receiver refuses a webhook older than five
minutes or a deliveryId it has already stored.
Rotate
- An app’s token: replace the stage’s generated token (a new value) and deploy the app. The entry holds up to two hashes, so a rolling restart overlaps.
- The service’s keys: replace a seed and deploy the service, then redeploy every app (the sealed widget keys are re-sealed) and update every webhook receiver’s public key.
When something is refused
The service logs one line per refusal, {"event":"mail.refused","clientId","reason"},
and never logs a token, an address list or a field. The reasons are the error
codes: UNKNOWN_CLIENT (no entry yet, or not propagated), ORIGIN_NOT_ALLOWED
(add the origin), CAPTCHA_FAILED, INVALID_FIELD, RATE_LIMITED,
QUOTA_EXCEEDED, UNAUTHORIZED (a wrong token) and RECIPIENT_NOT_ALLOWED (the
provider cannot reach that address, or the app may mail only its operators).