Node.js/Express-Server als Ersatz für Cloudflare Pages Functions + D1 (läuft auf eigenem Server statt Cloudflare)
Build & Deploy / deploy (push) Waiting to run
Build & Deploy / deploy (push) Waiting to run
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
/* =====================================================================
|
||||
lib/email.js — E-Mail-Versand über Resend. 1:1 portiert aus functions/_shared/email.js.
|
||||
sendeEmail() wirft NIE einen Fehler, gibt nur { ok, error? } zurück — eine fehlgeschlagene
|
||||
E-Mail darf nie eine Bestellung zum Scheitern bringen (siehe bestellung-erstellen.js).
|
||||
===================================================================== */
|
||||
|
||||
const RESEND_API_URL = "https://api.resend.com/emails";
|
||||
const STANDARD_ABSENDER = "Van's DIY & Bastelbedarf <[email protected]>";
|
||||
|
||||
export async function sendeEmail({ to, subject, html, text, from }) {
|
||||
if (!process.env.RESEND_API_KEY) {
|
||||
return { ok: false, error: "RESEND_API_KEY nicht gesetzt — E-Mail-Versand noch nicht eingerichtet." };
|
||||
}
|
||||
try {
|
||||
const res = await fetch(RESEND_API_URL, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${process.env.RESEND_API_KEY}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
from: from || process.env.RESEND_FROM || STANDARD_ABSENDER,
|
||||
to: Array.isArray(to) ? to : [to],
|
||||
subject,
|
||||
html,
|
||||
text,
|
||||
}),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const fehlertext = await res.text().catch(() => "");
|
||||
return { ok: false, error: `Resend antwortete mit Status ${res.status}: ${fehlertext}` };
|
||||
}
|
||||
return { ok: true };
|
||||
} catch (err) {
|
||||
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
||||
}
|
||||
}
|
||||
|
||||
export function escapeHtmlFuerEmail(str) {
|
||||
return String(str ?? "").replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[c]));
|
||||
}
|
||||
Reference in New Issue
Block a user