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}
${escape(mail.link.label)}
${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 waitlisted(origin: string, to: string, handle: string): Mail { return { to, subject: "You're on the refs waitlist", heading: "You're on the waitlist", paragraphs: [`Thanks for signing up for refs. We saved the username ${handle} for you.`, "We'll email you again as soon as your account is approved."], link: { label: "Learn about refs", href: origin }, reason: "You received this email because you signed up for refs.", }; } export function newSignup(origin: string, to: string, person: { name: string; email: string; handle: string }): Mail { return { to, subject: `${person.name} joined the refs waitlist`, heading: "New waitlist signup", paragraphs: [`${person.name} (${person.email}) joined the waitlist and wants the username ${person.handle}.`], link: { label: "Review the waitlist", href: `${origin}/admin/waitlist` }, reason: "You received this email because you are a refs administrator.", }; } export function approved(origin: string, to: string, handle: string): Mail { return { to, subject: "Your refs account is ready", heading: "Your account is ready", paragraphs: ["You're off the waitlist. Welcome to refs.", `Your repositories live at ${origin}/${handle}.`], link: { label: "Sign in to refs", href: `${origin}/login` }, reason: "You received this email because you signed up for refs.", }; } 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.`, }; }