@bilukit/mail
E-mail out of a step, through the outbox: the mail is written in the step's transaction and sent after the commit,
or never. The channel mail.send is dry unless the installation makes it live (BILUKIT_LIVE=mail.send); a dry mail
stays in the outbox fully rendered, and mail.message shows it.
// a module's step
const m = defineModule("invoices", {
texts: {
en: {
"issued.subject": "Invoice {number}",
"issued.text": "Dear {name},\n\nyour invoice {number} is attached.",
"issued.html": "<p>Dear {name},</p><p>your invoice {number} is attached.</p>",
},
},
});
run: async (ctx, { id }) => {
const invoice = await issueDraft(ctx.db, id);
await sendMail(ctx, {
...renderMail(
mailTemplate((k) => ctx.say(k), "invoices.issued"),
{ number: invoice.number, name: invoice.buyer },
),
to: { email: invoice.email, name: invoice.buyer },
attachments: [{ name: `${invoice.number}.pdf`, type: "application/pdf", content: pdf }],
key: `invoices.issued:${id}`,
});
// ...
};
// the app: the mail module after the connections module
defineApp({ name: "books", modules: [identity, connections, mail, invoices] });
MAIL_FROM=noreply@example.com MAIL_FROM_NAME="Example" MAIL_REPLY_TO=office@example.com
MAIL_TRANSPORT=scaleway MAIL_SCALEWAY_PROJECT_ID=... MAIL_SCALEWAY_REGION=fr-par # the default transport
MAIL_REDIRECT_TO=test@example.com # a test installation
BILUKIT_LIVE=mail.send
The kit sends through Scaleway Transactional Email's HTTP API. Its secret key is a key of the mail connection
(SCALEWAY_SECRET_KEY), set on the connections screen and sealed at rest - never configuration. Every try of one mail
carries the same Message-ID header, so a receiver tells a retry from a new mail.
A refusal for good (the API's 4xx: a bad address, a message it will not take) fails the mail at once; anything else -
no answer, a 5xx, 429, a wrong or missing key, a missing setting - is tried again with the outbox's backoff, so the
operator can still fix it.
Another HTTP provider is a transport the app gives, with its own connector for its key:
const acme = defineTransport({
id: "acme",
send: async (mail, c) => {
const key = await needKey(c, "acme_mail", "API_KEY");
const r = await outbound("https://api.example.com/send", { service: "acme", method: "POST", ... });
if (!r.ok) throw httpSendError("acme", r.status, await r.text());
},
});
m.module({ parts: { mail: [acme], connections: [acmeConnector] } }); // MAIL_TRANSPORT=acme
API
| what |
does |
mail |
the module: the mail.send channel, the mail connector, the scaleway transport |
sendMail(ctx, mail) |
puts a mail into the outbox in the step's transaction; checks the addresses (mail.address.invalid, 400) |
renderMail(template, values) |
fills {name} placeholders: as they are in the text, HTML-escaped in the HTML, the subject on one line |
mailTemplate(say, key) |
a template from the texts: <key>.subject, <key>.text, and <key>.html when there is one |
mailInvite(ctx, invite) |
the invite letter with identity.invite's link, sealed at rest (the outbox shows only recipient and subject) |
mail.message (GET /api/mail/[id]) |
one outgoing mail as it was rendered, a dry one too (right mail.view) |
mail.send_test (POST /api/mail/test) |
a test mail through the outbox (right mail.manage) |
defineTransport({ id, send }) |
an app's own provider: give it as parts: { mail: [t], connections: [its connector] }, pick it by MAIL_TRANSPORT |
needKey(c, connector, name) |
a connector's key at send time; a missing one keeps the mail waiting |
httpSendError(service, status, body) |
an HTTP API's failed answer as the right error: a 4xx permanent (but 401, 403, 408, 425, 429), the rest retried |
MailConfig, MailPayload, OutgoingMail |
the configuration, the outbox row's schema, what a transport is handed |
sendMail takes to (an address, { email, name }, or a list), cc, bcc, replyTo, subject, text, html,
attachments (bytes, at most 5 MiB together), key (unique across the app's mail: <module>.<what>:<id>),
summary and secret (the body carries a sign-in link: sealed at rest, never shown).
An invite by mail is the app's own step around the identity module's:
run: async (ctx, i) => {
const { userId, link, expiresAt } = await ctx.write(invite, i);
await mailInvite(ctx, { email: i.email, name: i.name, link, expiresAt });
return { result: { userId, expiresAt }, did: ..., irreversible: "the invite went out by mail" };
};
What it does not do
- No template language: placeholders only. Loops and conditions belong in the step's code.
- No attachments by file id: the sender does not see the files module's store; a step reads the bytes it attaches.
- No SMTP: the kit speaks to an HTTP API only; another provider is an app's transport.
- No incoming mail, no bounces, no tracking - and no check of the connection (a check sees only the keys, not
the project and region in the configuration):
mail.send_test proves the whole way.
- No
Name <address> strings: an address is bare; a name is its own field.
- The Scaleway API takes no idempotency key: a call without an answer is tried again later, so a rare mail can go
twice rather than not at all.