import {MailService} from '../../common/common.mail.js'; import {ADMIN_APP_URL} from './admin.config.js'; /** * The two transactional mails the admin module sends. Both go out through the * shared MailService (Salesforce relay, see common.mail.ts), which never throws * on a delivery failure - the invitation row and the reset token are already * committed by the time we get here. * * HTML plus a plain-text body: the text part is not a fallback afterthought, * it is what allowlist-based receivers and text-only clients actually show. */ const escapeHtml = (value: string): string => { return value .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"'); }; // `heading` is escaped here; `paragraphs` are not, because callers pass markup // (a around the expiry date) and escape their own interpolations. const layout = (heading: string, paragraphs: string[], buttonLabel: string, buttonUrl: string): string => { const body = paragraphs.map(p => `

${p}

`).join(''); return `

${escapeHtml(heading)}

${body}

${escapeHtml(buttonLabel)}

Falls der Button nicht funktioniert, kopiere diesen Link in deinen Browser:
${escapeHtml(buttonUrl)}

`; }; /** * Invitation mail. The link carries the raw token in the query string; the * admin app strips it from the URL as soon as it has read it (see the plan's * §3b - the token must never reach an API access log or a Referer header). */ export const sendInvitationMail = async ( recipientAddress: string, name: string, token: string, expiresAt: Date ): Promise => { const url = `${ADMIN_APP_URL}/accept-invite?token=${encodeURIComponent(token)}`; const expiry = expiresAt.toLocaleDateString('de-DE', {day: '2-digit', month: '2-digit', year: 'numeric'}); const subject = 'Dein Zugang zu Nachklang'; const text = [ `Hallo ${name},`, '', 'du wurdest eingeladen, ein Nachklang-Konto anzulegen. Über diesen Link vergibst du dein Passwort:', '', url, '', `Der Link ist bis zum ${expiry} gültig.`, '', 'Wenn du damit nichts anfangen kannst, ignoriere diese E-Mail einfach.', '', 'Viele Grüße', 'Nachklang e.V.' ].join('\n'); const html = layout( `Hallo ${name},`, [ 'du wurdest eingeladen, ein Nachklang-Konto anzulegen. Über den Button vergibst du dein Passwort.', `Der Link ist bis zum ${escapeHtml(expiry)} gültig.`, 'Wenn du damit nichts anfangen kannst, ignoriere diese E-Mail einfach.' ], 'Konto einrichten', url ); return MailService.sendMail(recipientAddress, subject, text, {html}); }; /** * Password reset. better-auth builds the URL (it embeds its own token and the * redirectTo the admin app passed), so this only wraps it in our templates. */ export const sendPasswordResetMail = async ( recipientAddress: string, name: string, url: string ): Promise => { const subject = 'Passwort zurücksetzen'; const text = [ `Hallo ${name},`, '', 'über diesen Link kannst du ein neues Passwort vergeben:', '', url, '', 'Der Link ist eine Stunde gültig.', '', 'Wenn du kein neues Passwort angefordert hast, ist nichts passiert - ignoriere diese E-Mail.', '', 'Viele Grüße', 'Nachklang e.V.' ].join('\n'); const html = layout( `Hallo ${name},`, [ 'über den Button kannst du ein neues Passwort vergeben.', 'Der Link ist eine Stunde gültig.', 'Wenn du kein neues Passwort angefordert hast, ist nichts passiert - ignoriere diese E-Mail.' ], 'Neues Passwort vergeben', url ); return MailService.sendMail(recipientAddress, subject, text, {html}); };