import type { Config } from "./config.js"; /** * Gabarit de courriel de marque, sans dependance ni feuille de style externe : * les clients de messagerie n'interpretent qu'une partie du CSS, tout est donc * ecrit en ligne et pose sur des tableaux, y compris pour Outlook. */ export interface Branding { appName: string; origin: string; logoUrl: string; color: string; /** Adresse d'expedition, reprise en pied pour dire ou ne pas repondre. */ footer: string; } export const branding = (config: Config): Branding => ({ appName: "CoHaiSe Cloud", origin: config.PUBLIC_ORIGIN, logoUrl: config.MAIL_LOGO_URL ?? new URL("/logo.png", config.PUBLIC_ORIGIN).toString(), color: "#b71c1c", footer: config.SMTP_FROM, }); export interface MailContent { /** Aperçu affiche par le client, avant ouverture (texte brut). */ preview: string; heading: string; intro: string; paragraphs?: string[]; /** Identifiants d'acces, presentes dans un encadre monospace. */ credentials?: { email: string; password: string }; cta?: { label: string; url: string }; note?: string; } export interface RenderedMail { html: string; text: string; } const escapeHtml = (value: string): string => value .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); const FONT = "-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif"; export const renderMail = (brand: Branding, content: MailContent): RenderedMail => { const paragraphs = (content.paragraphs ?? []) .map( (paragraph) => `

${escapeHtml(paragraph)}

`, ) .join(""); const credentials = content.credentials ? `

Vos identifiants

Adresse : ${escapeHtml(content.credentials.email)}

Mot de passe temporaire : ${escapeHtml(content.credentials.password)}

` : ""; const cta = content.cta ? `
${escapeHtml(content.cta.label)}
` : ""; const note = content.note ? `

${escapeHtml(content.note)}

` : ""; const html = ` ${escapeHtml(content.heading)}
${escapeHtml(content.preview)}
${escapeHtml(brand.appName)}

${escapeHtml(content.heading)}

${escapeHtml(content.intro)}

${paragraphs}${credentials}${cta}${note}

Message automatique : merci de ne pas repondre a cet e-mail.

${escapeHtml(brand.footer)}

${escapeHtml(brand.appName)} — ${escapeHtml(brand.origin.replace(/^https?:\/\//, ""))}

`; const lines = [content.intro, ...(content.paragraphs ?? [])]; if (content.credentials) { lines.push(`Adresse : ${content.credentials.email}`, `Mot de passe temporaire : ${content.credentials.password}`); } if (content.cta) lines.push(`${content.cta.label} : ${content.cta.url}`); if (content.note) lines.push(content.note); lines.push("", "Message automatique : merci de ne pas repondre a cet e-mail.", brand.footer); return { html, text: lines.join("\n") }; };