/** * The emails Accounts sends (1.4.0), styled as Cross Events' (apps/events/backend/src/utils/email.js): * a header in the church's colour with its logo or name, a white card, a button for the one thing * to do with the link written out under it, and a footer with the church's contact details. * * Every email also has the plain text it always had (some mail apps show only that, and the tests * read codes and links from it). Everything put into the HTML is escaped; colours and links are * checked first. */ import type { Ctx } from '../context.js'; import { church } from '../church/profile.js'; export type Built = { subject: string; text: string; html: string }; type Brand = { name: string; colour: string; accent: string; logoUrl: string | null; email: string | null; phone: string | null; url: string }; const FONT = `-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif`; const MONO = `'SFMono-Regular',Consolas,'Liberation Mono',Menlo,monospace`; const HEX = /^#[0-9a-fA-F]{6}$/; export const esc = (s: unknown) => String(s ?? '') .replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"').replace(/'/g, '''); /** Only http(s) links go in an href. */ const safeUrl = (u: string) => (/^https?:\/\//i.test(u) ? u : '#'); /** A darker shade of a #rrggbb colour, for the header's gradient. */ function darker(hex: string, by = 0.25): string { const n = parseInt(hex.slice(1), 16); const ch = (shift: number) => Math.max(0, Math.round(((n >> shift) & 255) * (1 - by))); return `#${[ch(16), ch(8), ch(0)].map((c) => c.toString(16).padStart(2, '0')).join('')}`; } async function brand(ctx: Ctx): Promise { const base = ctx.config.publicUrl.replace(/\/+$/, ''); try { const c = await church(ctx); const logo = c.branding.logoUrl; return { name: c.name || 'Your church', colour: HEX.test(c.branding.colours.primary) ? c.branding.colours.primary : '#1f4e79', accent: c.branding.colours.accent && HEX.test(c.branding.colours.accent) ? c.branding.colours.accent : '#f2a900', logoUrl: logo ? (logo.startsWith('/') ? `${base}${logo}` : /^https:\/\//i.test(logo) ? logo : null) : null, email: c.contact.email ?? null, phone: c.contact.phone ?? null, url: base, }; } catch { return { name: 'Your church', colour: '#1f4e79', accent: '#f2a900', logoUrl: null, email: null, phone: null, url: base }; } } /** The page around an email's body (Cross Events' layout). */ function layout(b: Brand, body: string, preheader: string): string { const header = b.logoUrl ? `${esc(b.name)}` : `

${esc(b.name)}

`; const contact = [ esc(b.name), b.email ? `${esc(b.email)}` : '', b.phone ? esc(b.phone) : '', ].filter(Boolean).join(' • '); return ` ${esc(b.name)}
${esc(preheader)}‌ ‌ ‌ 
${header}

Your ${esc(b.name)} account

${body}

${contact}

You're getting this because you have an account with ${esc(b.name)}, used to sign in to its sites.

`; } const title = (t: string, sub?: string) => `

${esc(t)}

` + (sub ? `

${esc(sub)}

` : ''); const hello = (name?: string | null) => `

Hello ${esc(name || 'there')},

`; const para = (html: string) => `

${html}

`; const divider = () => `
`; const small = (html: string) => `

${html}

`; function button(b: Brand, label: string, url: string): string { const href = esc(safeUrl(url)); return `
${esc(label)}

Or copy this link: ${esc(url)}

`; } /** Info, a warning ("if this wasn't you") or neutral: these colours never follow the church's. */ function callout(html: string, kind: 'info' | 'warning' | 'success' = 'info'): string { const s = { info: ['#eff6ff', '#3b82f6', '#1e40af'], warning: ['#fffbeb', '#f59e0b', '#92400e'], success: ['#f0fdf4', '#22c55e', '#166534'] }[kind]; return `
${html}
`; } const code6 = (code: string) => `

${esc(code)}

`; const contactLine = (b: Brand) => (b.email ? ` at ${esc(b.email)}` : ''); // ─── The emails ─────────────────────────────────────────────────────────────── /** A sign-in or confirm-your-email code (the text is the WhatsApp message too). */ export async function codeEmail(ctx: Ctx, o: { name?: string | null; code: string; text: string; purpose: 'sign_in' | 'verify_email' | 'setup'; minutes: number }): Promise { const b = await brand(ctx); const what = o.purpose === 'verify_email' ? 'confirm your email address' : 'sign in'; const body = title('Your code', o.purpose === 'verify_email' ? 'To confirm your email address' : `To sign in to ${b.name}`) + hello(o.name) + para(`Enter this code to ${esc(what)}. It works for ${o.minutes} minutes.`) + code6(o.code) + divider() + small(`Never share this code with anyone, not even someone from the church. If you didn't ask for it, you can ignore this email: nobody can sign in without your password${o.purpose === 'sign_in' ? ' and this code' : ''}.`); return { subject: `Your code: ${o.code}`, text: o.text, html: layout(b, body, `Your code is ${o.code}. It works for ${o.minutes} minutes.`) }; } export async function resetPasswordEmail(ctx: Ctx, o: { name?: string | null; url: string; minutes: number }): Promise { const b = await brand(ctx); const body = title('Reset your password', 'Someone (we hope you) asked to reset it.') + hello(o.name) + para(`Choose a new password with the button below. The link works for ${o.minutes} minutes, once.`) + button(b, 'Choose a new password', o.url) + divider() + small(`If you didn't ask, you can ignore this email: your password stays the same. Worried? Let the church office know${contactLine(b)}.`); const text = `Someone (we hope you) asked to reset the password for your account.\n\nChoose a new password here (the link works for ${o.minutes} minutes):\n${o.url}\n\nIf you didn't ask, you can ignore this email: your password stays the same.`; return { subject: 'Reset your password', text, html: layout(b, body, `Choose a new password. The link works for ${o.minutes} minutes.`) }; } /** A product (Cross Events) made someone an account: choose a password. */ export async function newAccountEmail(ctx: Ctx, o: { name: string; url: string; days: number }): Promise { const b = await brand(ctx); const body = title(`Welcome to ${b.name}`, 'An account has been made for you.') + hello(o.name) + para(`${esc(b.name)} has made you an account. It signs you in to all of the church's sites, such as events and registrations. Choose your password to start using it.`) + button(b, 'Choose my password', o.url) + callout(`The link works for ${o.days} days. After that, use Forgot your password? on the sign-in page.`, 'info') + small('If you weren\'t expecting this, you can ignore this email.'); const text = `Hello ${o.name},\n\n${b.name} has made you an account. Choose your password here (the link works for ${o.days} days):\n${o.url}\n\nIf you weren't expecting this, you can ignore this email.`; return { subject: `Your account at ${b.name}`, text, html: layout(b, body, `Choose your password for your ${b.name} account.`) }; } export async function confirmNewEmail(ctx: Ctx, o: { name?: string | null; url: string }): Promise { const b = await brand(ctx); const body = title('Confirm your new email address') + hello(o.name) + para('You asked to sign in with this email address from now on. Confirm it\'s yours with the button below; until then you keep signing in with your old one.') + button(b, 'Confirm this address', o.url) + divider() + small('The link works for 24 hours. If you didn\'t ask for this, you can ignore this email.'); const text = `Confirm this is your new email address for signing in:\n${o.url}\n\nThe link works for 24 hours.`; return { subject: 'Confirm your new email address', text, html: layout(b, body, 'Confirm your new sign-in email address.') }; } export async function emailChangedNotice(ctx: Ctx, o: { name?: string | null; newEmail: string }): Promise { const b = await brand(ctx); const body = title('Your sign-in email address changed') + hello(o.name) + para(`Your account now signs in with ${esc(o.newEmail)}. Emails about your account go there from now on.`) + callout(`Wasn't you? Contact the church office straight away${contactLine(b)}.`, 'warning'); const text = `Your account now signs in with ${o.newEmail}. If you didn't do this, contact the church office straight away.`; return { subject: 'Your sign-in email address changed', text, html: layout(b, body, `Your account now signs in with ${o.newEmail}.`) }; } export async function exportReadyEmail(ctx: Ctx, o: { name?: string | null; url: string; days: number }): Promise { const b = await brand(ctx); const body = title('Your data is ready', 'The copy you asked for') + hello(o.name) + para('The copy of your data you asked for is ready. Download it from your account page.') + button(b, 'Download my data', o.url) + callout(`It's kept for ${o.days} days, then deleted. You need to be signed in to download it.`, 'info'); const text = `The copy of your data you asked for is ready. Download it from your account page (it's kept for ${o.days} days):\n${o.url}`; return { subject: 'Your data is ready to download', text, html: layout(b, body, 'The copy of your data you asked for is ready.') }; } /** A test from the admin page or from a linked product (`from`: "Cross Events"). */ export async function testEmail(ctx: Ctx, o: { from?: string } = {}): Promise { const b = await brand(ctx); const body = title('Email works', o.from ? `A test sent from ${o.from}` : 'A test from Accounts') + para(`This is a test from ${esc(b.name)}'s sign-in (Accounts)${o.from ? `, sent from ${esc(o.from)}` : ''}.`) + callout('Email is set up: sign-in codes, password links and security notices can be sent.', 'success'); const text = o.from ? `This is a test from ${b.name}'s sign-in (Accounts), sent from ${o.from}. Email works: sign-in codes and password links can be sent.` : 'Email from Accounts works.'; return { subject: o.from ? `Test email from ${b.name}'s sign-in` : 'Test email from Accounts', text, html: layout(b, body, 'A test email: email works.') }; } /** A sign-in happened (1.6.0, as Cross Events' "New login detected"). `whatsapp`: the same in a few lines. */ export async function newSignInEmail(ctx: Ctx, o: { name?: string | null; when: string; location: string; device: string; site: string | null }): Promise { const b = await brand(ctx); const row = (k: string, v: string) => `${esc(k)}${esc(v)}`; const body = title('New sign-in', `Someone signed in to your ${b.name} account`) + hello(o.name) + `` + row('When', o.when) + (o.site ? row('Signed in to', o.site) : '') + row('Where (roughly)', o.location) + row('Device', o.device) + '
' + callout(`Not you? Change your password straight away from your account page, and let the church office know${contactLine(b)}.`, 'warning') + small('If this was you, there\'s nothing to do.'); const lines = [`When: ${o.when}`, ...(o.site ? [`Signed in to: ${o.site}`] : []), `Where (roughly): ${o.location}`, `Device: ${o.device}`]; const text = `Hello ${o.name || 'there'},\n\nSomeone signed in to your ${b.name} account.\n\n${lines.join('\n')}\n\nIf this wasn't you, change your password straight away and let the church office know${b.email ? ` (${b.email})` : ''}.\nIf it was you, there's nothing to do.`; const whatsapp = `🔐 *New sign-in*\n\nHello ${o.name || 'there'}, someone signed in to your *${b.name}* account.\n\n${lines.join('\n')}\n\nNot you? Change your password straight away and let the church office know.`; return { subject: `New sign-in to your ${b.name} account`, text, html: layout(b, body, `New sign-in: ${o.when}, ${o.device}.`), whatsapp }; }