Files
place4bees.com/backend/apps/backend/src/lib/contact-email.ts
T
2026-09-17 16:02:51 +02:00

77 lines
2.3 KiB
TypeScript

/**
* Die Mail, die eine Anfrage aus einem Kontaktformular auslöst.
*
* Sie geht an den Shop, nicht an die Kundin — deshalb steht hier alles, was
* zum Antworten nötig ist, und nichts zum Schmücken. Die Absenderadresse
* reist als Reply-To mit, damit eine Antwort direkt bei ihr landet.
*/
export type Anfrage = {
name: string;
email: string;
nachricht: string;
/** Woher die Anfrage kam, z. B. "Kontakt" oder "Ihr Projekt". */
quelle: string;
/** Die Seite, auf der das Formular stand. */
seite?: string | null;
};
function entschaerfen(text: string): string {
return text
.replace(/&/g, "&")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;");
}
export function anfrageMail(anfrage: Anfrage) {
const { name, email, nachricht, quelle, seite } = anfrage;
const zeilen = [
["Name", name],
["E-Mail", email],
["Formular", quelle],
...(seite ? [["Seite", seite] as [string, string]] : []),
];
const html = `<!doctype html>
<html lang="de"><head><meta charset="utf-8" /></head>
<body style="margin:0;background:#f6f5f1;padding:24px;font-family:Arial,Helvetica,sans-serif;color:#1a1a1a;font-size:15px;line-height:1.55">
<div style="max-width:600px;margin:0 auto;background:#ffffff;padding:32px">
<h1 style="margin:0 0 20px;font-size:20px">Neue Anfrage über ${entschaerfen(quelle)}</h1>
<table style="width:100%;border-collapse:collapse">
${zeilen
.map(
([k, v]) => `<tr>
<td style="padding:6px 12px 6px 0;color:#6b6b66;white-space:nowrap;vertical-align:top">${entschaerfen(k)}</td>
<td style="padding:6px 0">${entschaerfen(String(v))}</td>
</tr>`
)
.join("")}
</table>
<h2 style="margin:24px 0 8px;font-size:16px">Nachricht</h2>
<p style="margin:0;white-space:pre-wrap">${entschaerfen(nachricht)}</p>
<p style="margin:28px 0 0;padding-top:16px;border-top:1px solid #dcdcd8;color:#6b6b66;font-size:13px">
Antworten geht direkt an ${entschaerfen(email)}.
</p>
</div>
</body></html>`;
const text = [
`Neue Anfrage über ${quelle}`,
"",
...zeilen.map(([k, v]) => `${k}: ${v}`),
"",
"Nachricht:",
nachricht,
"",
`Antworten geht direkt an ${email}.`,
].join("\n");
return {
subject: `Anfrage über ${quelle}: ${name}`,
html,
text,
};
}