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)} |
| ${escapeHtml(content.cta.label)} |
${escapeHtml(content.note)}
` : ""; const html = `|
${escapeHtml(brand.appName)} — ${escapeHtml(brand.origin.replace(/^https?:\/\//, ""))} |