export interface Mail {
readonly to: string;
readonly subject: string;
readonly heading: string;
readonly paragraphs: readonly string[];
readonly link: { readonly label: string; readonly href: string };
readonly note?: string;
readonly reason: string;
}
const FONT = "-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif";
const MONO = "ui-monospace,SFMono-Regular,Menlo,Consolas,monospace";
function escape(value: string): string {
return value.replace(/&/g, "&").replace(//g, ">").replace(/"/g, """);
}
function html(mail: Mail): string {
const href = escape(mail.link.href);
const home = escape(new URL(mail.link.href).origin);
const paragraphs = mail.paragraphs.map((paragraph) => `
${escape(paragraph)}
`).join("");
const note = mail.note ? `${escape(mail.note)}
` : "";
return `
${escape(mail.subject)}
${escape(mail.paragraphs[0] ?? mail.heading)}
| refs:// |
${escape(mail.heading)}
${paragraphs}
${note}
If the button does not work, paste this link into your browser: ${href}
|
${escape(mail.reason)} refs · You architect. Agents build. |
|
`;
}
function text(mail: Mail): string {
return [mail.heading, ...mail.paragraphs, `${mail.link.label}: ${mail.link.href}`, ...(mail.note ? [mail.note] : []), "--", mail.reason].join("\n\n");
}
export function compose(from: string, mail: Mail): EmailMessageBuilder {
return { from: { email: from, name: "refs" }, to: mail.to, subject: mail.subject, text: text(mail), html: html(mail) };
}
/** Sends after the response, and never fails the request: a lost email only costs a notification. */
export function send(env: Env, ctx: ExecutionContext, mail: Mail): void {
ctx.waitUntil(env.EMAIL.send(compose(env.EMAIL_FROM, mail)).then(
() => undefined,
(error: { code?: string; message?: string }) => console.error("email failed", mail.subject, error.code, error.message),
));
}
export function newSignup(origin: string, to: string, person: { name: string; email: string; handle: string }): Mail {
return {
to,
subject: `${person.name} signed up for refs`,
heading: "New sign-up",
paragraphs: [`${person.name} (${person.email}) signed up with the username ${person.handle}.`],
link: { label: "Open admin", href: `${origin}/admin` },
reason: "You received this email because you are a refs administrator.",
};
}
export function passwordReset(to: string, link: string): Mail {
return {
to,
subject: "Reset your refs password",
heading: "Reset your password",
paragraphs: ["Use the button below to choose a new password. Signing in with it ends your other sessions."],
link: { label: "Choose a new password", href: link },
note: "This link works once and expires in 1 hour.",
reason: `You received this email because someone asked to reset the password of ${to}. If it was not you, ignore this email. Your password does not change.`,
};
}
export function invited(to: string, org: string, inviter: string, link: string): Mail {
return {
to,
subject: `${inviter} invited you to ${org} on refs`,
heading: `Join ${org} on refs`,
paragraphs: [`${inviter} invited you to join the ${org} organization on refs.`],
link: { label: `Join ${org}`, href: link },
note: "This invitation works once and expires in 7 days.",
reason: `You received this email because ${inviter} invited ${to}. If you did not expect it, you can ignore it.`,
};
}