Files
gerbeur/api/services/email-service.ts
khannurien f20ca83f0c v4: every email is rendered by its template in api/mail/ on one layout that takes the instance's style, and sent one way — the welcome email was built and sent inside the register route and the reset link in its service, each writing Markdown, running marked, and calling a nodemailer wrapper whose cc, bcc, replyTo and send result nothing used. Now api/mail/ holds one template per email (welcome.ts, password-reset.ts) on layout.ts's EmailKit, which draws each block with inline styles in a style from styles.ts: the six site styles' palettes, copied from src/themes/, and what makes each recognisable in a mail client (Brutalist's hard border and shadow, GeoCities' ridge and rainbow rules, Times' underlined ink links, Kanagawa's colored quotes, dates and wordmark, Melange's spice bar). On the "auto" scheme the light palette is inlined and the dark one swapped in by a prefers-color-scheme block; a chosen scheme sends only its own. User-written text is escaped where it lands. strings.ts holds every email's words in English and French. GERBEUR_EMAIL_LOCALE picks the language and GERBEUR_DEFAULT_STYLE the instance's style, which the welcome and reset emails are drawn in; GERBEUR_WELCOME_EMAIL_BODY still replaces the welcome's Markdown, whose blocks now take the style too. email-service sends a rendered email with its recipient and headers, refuses to load when GERBEUR_SMTPS_URL is set without GERBEUR_FROM_EMAIL (sends used to fail one by one), and takes a test transport, through which the tests now read what was sent. The welcome send moves to user-service's sendWelcomeEmail. The style and scheme names move to shared/theme.ts, which the server now needs. README, mod.ts and .env.example describe mail/ and the new variables, and drop the "notifications, invites" email that never existed. 221 backend tests pass (new: the layout's schemes, styles and escaping, both templates, the welcome on sign-up, a reset link from the email that works), and deno check, deno lint, doc:lint, deno fmt, tsc and eslint are clean.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-04 21:11:54 +00:00

77 lines
2.4 KiB
TypeScript

/**
* Sends the site's email through the SMTP server of {@link SMTPS_URL}, which
* may be unset. The welcome email, the password reset link and the digests go
* through it, each rendered first by its module in `api/mail/`. They check
* {@link isEmailEnabled} before rendering.
*
* Importing this module throws when {@link SMTPS_URL} is set without
* {@link FROM_EMAIL}, so a misconfigured server doesn't start rather than fail
* at its first send.
*
* @module
*/
import nodemailer from "nodemailer";
import { FROM_EMAIL, SMTPS_URL } from "../config.ts";
import type { RenderedEmail } from "../mail/layout.ts";
if (SMTPS_URL && !FROM_EMAIL) {
throw new Error(
"GERBEUR_FROM_EMAIL environment variable is required when GERBEUR_SMTPS_URL is set.",
);
}
/**
* Passed to {@link sendEmail}: a rendered email, its recipient, and the
* headers some emails add (the digests' `List-Unsubscribe`).
*/
export interface OutgoingEmail extends RenderedEmail {
to: string;
headers?: Record<string, string>;
}
/**
* Hands a message to a mail server. nodemailer's SMTP transport is one, and
* the tests put a recording one in its place with {@link setEmailTransport}.
*/
export interface EmailTransport {
sendMail(message: OutgoingEmail & { from: string }): Promise<unknown>;
}
let transport: EmailTransport | null = SMTPS_URL
? nodemailer.createTransport(SMTPS_URL)
: null;
/**
* Checks whether email can be sent. Without an SMTP server, sign-up sends no
* welcome email, a password reset request does nothing, and no digest goes
* out.
*/
export function isEmailEnabled(): boolean {
return transport !== null;
}
/**
* Replaces the transport, so that email is enabled and every message goes to
* `next`; null disables email again. The tests use it to read what was sent.
*/
export function setEmailTransport(next: EmailTransport | null): void {
transport = next;
}
/**
* Sends `email` from {@link FROM_EMAIL} and resolves once the SMTP server has
* accepted it. The welcome and reset emails don't wait for it: a slow server
* mustn't hold up the request.
*
* @throws {Error} When email is disabled, or when the server refuses the
* message or can't be reached.
*/
export async function sendEmail(email: OutgoingEmail): Promise<void> {
if (!transport) {
throw new Error("Email is disabled: GERBEUR_SMTPS_URL is unset.");
}
await transport.sendMail({ ...email, from: FROM_EMAIL });
}