Skip to content
refs
History
114 lines · 5.8 KBRaw
1export interface Mail {
2 readonly to: string;
3 readonly subject: string;
4 readonly heading: string;
5 readonly paragraphs: readonly string[];
6 readonly link: { readonly label: string; readonly href: string };
7 readonly note?: string;
8 readonly reason: string;
9}
10
11const FONT = "-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif";
12const MONO = "ui-monospace,SFMono-Regular,Menlo,Consolas,monospace";
13
14function escape(value: string): string {
15 return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
16}
17
18function html(mail: Mail): string {
19 const href = escape(mail.link.href);
20 const home = escape(new URL(mail.link.href).origin);
21 const paragraphs = mail.paragraphs.map((paragraph) => `<p style="margin:0 0 16px;font-size:15px;line-height:24px;color:#3f3f46;">${escape(paragraph)}</p>`).join("");
22 const note = mail.note ? `<p style="margin:24px 0 0;font-size:13px;line-height:20px;color:#71717a;">${escape(mail.note)}</p>` : "";
23 return `<!doctype html>
24<html lang="en">
25<head>
26<meta charset="utf-8">
27<meta name="viewport" content="width=device-width,initial-scale=1">
28<meta name="color-scheme" content="light">
29<meta name="supported-color-schemes" content="light">
30<title>${escape(mail.subject)}</title>
31</head>
32<body style="margin:0;padding:0;background-color:#f4f4f5;">
33<div style="display:none;max-height:0;overflow:hidden;opacity:0;">${escape(mail.paragraphs[0] ?? mail.heading)}</div>
34<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="background-color:#f4f4f5;">
35<tr><td align="center" style="padding:40px 16px;">
36<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="max-width:560px;">
37<tr><td style="padding:0 4px 20px;font-family:${MONO};font-size:20px;font-weight:700;letter-spacing:-0.5px;color:#18181b;">refs<span style="color:#f6821f;">://</span></td></tr>
38<tr><td style="background-color:#ffffff;border:1px solid #e4e4e7;border-radius:12px;padding:40px 40px 36px;font-family:${FONT};">
39<h1 style="margin:0 0 20px;font-size:22px;line-height:30px;font-weight:600;color:#18181b;">${escape(mail.heading)}</h1>
40${paragraphs}
41<table role="presentation" cellpadding="0" cellspacing="0" border="0" style="margin:28px 0 0;">
42<tr><td style="border-radius:8px;background-color:#2563eb;"><a href="${href}" style="display:inline-block;padding:12px 24px;font-family:${FONT};font-size:15px;font-weight:600;line-height:20px;color:#ffffff;text-decoration:none;border-radius:8px;">${escape(mail.link.label)}</a></td></tr>
43</table>
44${note}
45<p style="margin:28px 0 0;padding-top:20px;border-top:1px solid #f4f4f5;font-size:12px;line-height:18px;color:#a1a1aa;">If the button does not work, paste this link into your browser:<br><a href="${href}" style="color:#2563eb;word-break:break-all;">${href}</a></p>
46</td></tr>
47<tr><td style="padding:20px 4px 0;font-family:${FONT};font-size:12px;line-height:18px;color:#a1a1aa;">${escape(mail.reason)}<br><a href="${home}" style="color:#71717a;text-decoration:none;">refs</a> &middot; You architect. Agents build.</td></tr>
48</table>
49</td></tr>
50</table>
51</body>
52</html>`;
53}
54
55function text(mail: Mail): string {
56 return [mail.heading, ...mail.paragraphs, `${mail.link.label}: ${mail.link.href}`, ...(mail.note ? [mail.note] : []), "--", mail.reason].join("\n\n");
57}
58
59export function compose(from: string, mail: Mail): EmailMessageBuilder {
60 return { from: { email: from, name: "refs" }, to: mail.to, subject: mail.subject, text: text(mail), html: html(mail) };
61}
62
63/** Sends after the response, and never fails the request: a lost email only costs a notification. */
64export function send(env: Env, ctx: ExecutionContext, mail: Mail): void {
65 ctx.waitUntil(env.EMAIL.send(compose(env.EMAIL_FROM, mail)).then(
66 () => undefined,
67 (error: { code?: string; message?: string }) => console.error("email failed", mail.subject, error.code, error.message),
68 ));
69}
70
71export function waitlisted(origin: string, to: string, handle: string): Mail {
72 return {
73 to,
74 subject: "You're on the refs waitlist",
75 heading: "You're on the waitlist",
76 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."],
77 link: { label: "Learn about refs", href: origin },
78 reason: "You received this email because you signed up for refs.",
79 };
80}
81
82export function newSignup(origin: string, to: string, person: { name: string; email: string; handle: string }): Mail {
83 return {
84 to,
85 subject: `${person.name} joined the refs waitlist`,
86 heading: "New waitlist signup",
87 paragraphs: [`${person.name} (${person.email}) joined the waitlist and wants the username ${person.handle}.`],
88 link: { label: "Review the waitlist", href: `${origin}/admin/waitlist` },
89 reason: "You received this email because you are a refs administrator.",
90 };
91}
92
93export function approved(origin: string, to: string, handle: string): Mail {
94 return {
95 to,
96 subject: "Your refs account is ready",
97 heading: "Your account is ready",
98 paragraphs: ["You're off the waitlist. Welcome to refs.", `Your repositories live at ${origin}/${handle}.`],
99 link: { label: "Sign in to refs", href: `${origin}/login` },
100 reason: "You received this email because you signed up for refs.",
101 };
102}
103
104export function invited(to: string, org: string, inviter: string, link: string): Mail {
105 return {
106 to,
107 subject: `${inviter} invited you to ${org} on refs`,
108 heading: `Join ${org} on refs`,
109 paragraphs: [`${inviter} invited you to join the ${org} organization on refs.`],
110 link: { label: `Join ${org}`, href: link },
111 note: "This invitation works once and expires in 7 days.",
112 reason: `You received this email because ${inviter} invited ${to}. If you did not expect it, you can ignore it.`,
113 };
114}