83 lines
3.8 KiB
TypeScript
83 lines
3.8 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 };
|
|
}
|