150 lines
7.3 KiB
TypeScript
150 lines
7.3 KiB
TypeScript
import "server-only";
|
|
import { randomBytes } from "node:crypto";
|
|
import { mkdir, writeFile } from "node:fs/promises";
|
|
import path from "node:path";
|
|
import nodemailer from "nodemailer";
|
|
import type { PoolClient } from "pg";
|
|
import { withHousehold } from "./db";
|
|
import { unseal } from "./vault";
|
|
|
|
// Mails verschicken (26.11.40): je Haushalt ein SMTP-Zugang, Standard Resend
|
|
// (smtp.resend.com, 465, Benutzer „resend“, Passwort = API-Schlüssel). Der
|
|
// Schlüssel liegt nur verschlüsselt in household_mail (lib/vault.ts).
|
|
// Nur für die Tests: MAIL_TEST_DIR schreibt jede Mail als JSON statt zu senden.
|
|
|
|
export const RESEND = { host: "smtp.resend.com", port: 465, secure: true, username: "resend" } as const;
|
|
|
|
export type MailSettings = {
|
|
host: string;
|
|
port: number;
|
|
secure: boolean;
|
|
username: string;
|
|
passwordEnc: Buffer | null;
|
|
fromAddress: string;
|
|
fromName: string | null;
|
|
};
|
|
|
|
export type Mail = { to: string; subject: string; text: string; html: string };
|
|
|
|
export class MailNotConfigured extends Error {
|
|
constructor() {
|
|
super("notConfigured");
|
|
}
|
|
}
|
|
|
|
export async function loadMail(client: PoolClient): Promise<MailSettings | null> {
|
|
const { rows } = await client.query(
|
|
`select host, port, secure, username, password_enc as "passwordEnc", from_address as "fromAddress", from_name as "fromName"
|
|
from household_mail`,
|
|
);
|
|
return rows[0] ?? null;
|
|
}
|
|
|
|
/** Schickt eine Mail über den Zugang des Haushalts. Wirft MailNotConfigured oder den SMTP-Fehler. */
|
|
export async function sendMail(householdId: string, mail: Mail): Promise<void> {
|
|
const settings = await withHousehold(householdId, loadMail);
|
|
if (!settings?.passwordEnc) throw new MailNotConfigured();
|
|
// Kein Anführungszeichen, kein Zeilenumbruch: nichts, was einen Kopf der Mail aufbrechen könnte.
|
|
const display = settings.fromName?.replace(/["\\\x00-\x1f\x7f]/g, "").trim();
|
|
const from = display ? `"${display}" <${settings.fromAddress}>` : settings.fromAddress;
|
|
const dir = process.env.MAIL_TEST_DIR;
|
|
if (dir) {
|
|
await mkdir(dir, { recursive: true });
|
|
const name = `${Date.now()}-${randomBytes(4).toString("hex")}.json`;
|
|
await writeFile(path.join(dir, name), JSON.stringify({ from, ...mail }), { mode: 0o600 });
|
|
return;
|
|
}
|
|
const transport = nodemailer.createTransport({
|
|
host: settings.host,
|
|
port: settings.port,
|
|
secure: settings.secure,
|
|
auth: { user: settings.username, pass: await unseal(settings.passwordEnc) },
|
|
connectionTimeout: 10_000,
|
|
greetingTimeout: 10_000,
|
|
socketTimeout: 20_000,
|
|
});
|
|
await transport.sendMail({ from, to: mail.to, subject: mail.subject, text: mail.text, html: mail.html });
|
|
}
|
|
|
|
const escape = (s: string) => s.replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c]!);
|
|
|
|
/** Eine schlichte Mail: Satz, Knopf, Hinweis. Keine Bilder von außen, kein Tracking. */
|
|
export function buttonMail(p: { to: string; subject: string; lead: string; button: string; url: string; note: string }): Mail {
|
|
const text = `${p.lead}\n\n${p.url}\n\n${p.note}\n\n— foodplanhero`;
|
|
const html = `<!doctype html><html><body style="margin:0;padding:24px;background:#faf8ff;font-family:Poppins,Helvetica,Arial,sans-serif;color:#1f1d2b">
|
|
<div style="max-width:480px;margin:0 auto;background:#ffffff;border-radius:16px;padding:28px">
|
|
<p style="margin:0 0 8px;font-size:20px;font-weight:700">foodplanhero</p>
|
|
<p style="margin:0 0 24px;font-size:16px;line-height:1.5">${escape(p.lead)}</p>
|
|
<p style="margin:0 0 24px"><a href="${escape(p.url)}" style="display:inline-block;background:#5b4a9e;color:#ffffff;text-decoration:none;font-weight:600;padding:14px 24px;border-radius:999px">${escape(p.button)}</a></p>
|
|
<p style="margin:0;font-size:13px;line-height:1.5;color:#5e5a70">${escape(p.note)}</p>
|
|
</div></body></html>`;
|
|
return { to: p.to, subject: p.subject, text, html };
|
|
}
|
|
|
|
/**
|
|
* Mails zur Runde (26.11.53): Kopf in Koralle, große Frist-Box, Knopf, kurze Liste,
|
|
* Fuß mit Abmelde-Link. Nur Inline-Stile, keine Bilder von außen, kein Tracking —
|
|
* die Gerichtsfotos sind privat und bleiben auf der Box.
|
|
*/
|
|
export function roundMail(p: {
|
|
to: string;
|
|
subject: string;
|
|
preheader: string;
|
|
eyebrow: string;
|
|
title: string;
|
|
lead: string;
|
|
box?: { big: string; small: string } | null;
|
|
points?: string[];
|
|
listTitle?: string;
|
|
list?: { title: string; meta: string }[];
|
|
button: string;
|
|
url: string;
|
|
footer: string;
|
|
unsubscribe: { label: string; url: string };
|
|
}): Mail {
|
|
const lines = [
|
|
p.title,
|
|
"",
|
|
p.lead,
|
|
...(p.box ? ["", `${p.box.big} — ${p.box.small}`] : []),
|
|
...(p.points?.length ? ["", ...p.points.map((x) => `• ${x}`)] : []),
|
|
...(p.list?.length ? ["", p.listTitle ?? "", ...p.list.map((x) => `• ${x.title}${x.meta ? ` (${x.meta})` : ""}`)] : []),
|
|
"",
|
|
`${p.button}: ${p.url}`,
|
|
"",
|
|
p.footer,
|
|
`${p.unsubscribe.label}: ${p.unsubscribe.url}`,
|
|
];
|
|
const box = p.box
|
|
? `<div style="margin:0 0 24px;padding:20px;border-radius:16px;background:#fff3ee;border:2px solid #f39380;text-align:center">
|
|
<p style="margin:0;font-size:30px;font-weight:700;color:#1f1d2b">${escape(p.box.big)}</p>
|
|
<p style="margin:6px 0 0;font-size:14px;color:#5e5a70">${escape(p.box.small)}</p></div>`
|
|
: "";
|
|
const points = p.points?.length
|
|
? `<ul style="margin:0 0 24px;padding:0 0 0 20px;font-size:15px;line-height:1.6">${p.points.map((x) => `<li>${escape(x)}</li>`).join("")}</ul>`
|
|
: "";
|
|
const list = p.list?.length
|
|
? `<p style="margin:0 0 8px;font-size:13px;font-weight:700;letter-spacing:.08em;text-transform:uppercase;color:#5b4a9e">${escape(p.listTitle ?? "")}</p>
|
|
<table role="presentation" style="width:100%;border-collapse:collapse;margin:0 0 24px">${p.list
|
|
.map(
|
|
(x) =>
|
|
`<tr><td style="padding:10px 0;border-bottom:1px solid #ece8f6;font-size:15px;font-weight:600">${escape(x.title)}</td><td style="padding:10px 0;border-bottom:1px solid #ece8f6;font-size:13px;color:#5e5a70;text-align:right">${escape(x.meta)}</td></tr>`,
|
|
)
|
|
.join("")}</table>`
|
|
: "";
|
|
const html = `<!doctype html><html><body style="margin:0;padding:0;background:#faf8ff;font-family:Poppins,Helvetica,Arial,sans-serif;color:#1f1d2b">
|
|
<span style="display:none;max-height:0;overflow:hidden">${escape(p.preheader)}</span>
|
|
<div style="max-width:520px;margin:0 auto;padding:24px 16px">
|
|
<div style="background:#f39380;border-radius:20px 20px 0 0;padding:24px 28px">
|
|
<p style="margin:0;font-size:13px;font-weight:700;letter-spacing:.1em;text-transform:uppercase;color:#3b1d14">${escape(p.eyebrow)}</p>
|
|
<p style="margin:6px 0 0;font-size:26px;line-height:1.25;font-weight:700;color:#1f1d2b">${escape(p.title)}</p></div>
|
|
<div style="background:#ffffff;border-radius:0 0 20px 20px;padding:28px">
|
|
<p style="margin:0 0 24px;font-size:16px;line-height:1.6">${escape(p.lead)}</p>
|
|
${box}${points}${list}
|
|
<p style="margin:0 0 8px;text-align:center"><a href="${escape(p.url)}" style="display:inline-block;background:#5b4a9e;color:#ffffff;text-decoration:none;font-weight:600;font-size:17px;padding:16px 32px;border-radius:999px">${escape(p.button)}</a></p>
|
|
</div>
|
|
<p style="margin:20px 8px 0;font-size:12px;line-height:1.6;color:#5e5a70;text-align:center">${escape(p.footer)}<br><a href="${escape(p.unsubscribe.url)}" style="color:#5e5a70">${escape(p.unsubscribe.label)}</a></p>
|
|
</div></body></html>`;
|
|
return { to: p.to, subject: p.subject, text: lines.join("\n"), html };
|
|
}
|