Apps email their own users, with typed templates (React Email components, from canica/email).
// server/emails.tsx
import { email, Button, Heading, Text } from "canica/email"
import { string } from "canica/server"
export const emails = {
listShared: email(
{ props: { listName: string(), by: string() }, subject: ({ listName }) => `${listName} was shared with you`, preview: { listName: "Groceries", by: "Ana" } },
({ listName, by }) => <Text>{by} shared <b>{listName}</b> with you. Open it in the app.</Text>,
),
}
// server/index.ts: app({ emails, … }); then in a mutation or an action (it returns the email's id):
const sent = yield* (yield* Email).send("listShared", { to: friend, props: { listName, by: me.profile?.name ?? "Someone" } })
// a query the page uses to follow it, live (useQuery("emailStatus", { id })):
emailStatus: query({ id: emailId() }, function* ({ id }) {
const status = yield* (yield* Email).status(id) // NotFound for an unknown id
if (status.to !== (yield* Viewer).userId) return yield* new NotFound({ table: "emails", id }) // only the recipient
return status // { state, reason, message, attempts, … }
}),
tois a user id, never an address: emails go only to people who signed in to this app with Google (the address Google verified) and whom its access still lets in. There are no raw addresses, HTML or text bodies; the template name and props are typed. Why: an app only emails people who chose it, so mail from canica.run stays trusted for every app.- From a mutation it is queued with its writes and sent after the commit (a rolled-back mutation sends nothing);
from an action, at once. Typed failures (nothing queued):
NotARecipient,Unsubscribed,EmailLimitReached(100 a day per app, 10 a day to one person, each email counted once however many tries;canica usage). - Their messages say "you" when the person emails themselves (
self: trueonNotARecipient), and "this person" when the email is for someone else.NotARecipient'sreason:not-signed-in,sign-in-again(they signed in before the app kept the sign-in service's proof; a fresh sign-in stores it) orno-access. For your own email,ActionStatusshows "To get emails from, confirm your email by signing in again" with a Sign in again button (back to the same page; one tap at Google, or none); <EmailNeedsSignIn />is that block, for where you show an email's state yourself (emailNeedsSignIn(error)tells when). - How the queue works, in four steps: (1)
sendchecks and queues the email (queued) and returns its id; (2) a job hands it to the platform's mail relay (sending); (3) the relay sends it (sent) or refuses it (failed, withreason:unsubscribed,not-a-recipient,limit,bounced,complained, …); (4) a try that does not get through (relay down, a timeout) isqueuedagain and tried later (2 min, 10 min, 50 min, ~4 h, 16 h: six tries in about a day, thenfailedwithreason: "relay"); refusals are never tried again. Every try keeps the same id and the relay sends an id once, so a retry after a timeout never sends a second copy. Each try shows incanica logs. - Never tell the person "Sent" when
sendreturns: it is only queued. Show the status (Queued…,Sent,Failed (why)): keep the idsendreturned (return it from the action) and show theemailStatusquery's state next to the button. - The template is only the body (no
<Html>/<Body>): Canica adds the app's name and icon above it and the footer "You're getting this because you use· Unsubscribe" (one click; the app can no longer email that person). The plain-text version is made for you. Mail comes from <app>@mail.canica.runand replies go nowhere: say in the email what to do in the app. - Dev: nothing is sent.
/_canica/dev/emails(devtools: ✉ Emails) shows what the app sent and every template with itspreviewprops, as HTML and text, at phone and desktop width;canica emailswrites them to.canica/emails/. Tests:t.emails()→[{ id, template, to, props, subject, status, reason, attempts, render() }](queueduntil jobs run:await t.advance(0), ormeanwhile(() => t.advance(0))in a scene; thensent, as deployed);t.as("bob").unsubscribeFromEmails().