Özgün çalışan dosya
netlify/functions/mini-mail.mjs
Kodlardaki adlar ve çalışan örnek sayfalardaki metinler özgün halleriyle korunmuştur. Ders ve dosya açıklamaları bunları Türkçe olarak açıklar.
export const handler = async (event) => {
if (event.httpMethod !== "POST") return { statusCode: 405, body: "Use POST" };
const labToken = process.env.LAB_TOKEN;
if (!labToken || labToken.length < 24)
return { statusCode: 503, body: "Practice access not configured" };
if (event.headers?.authorization !== "Bearer " + labToken)
return { statusCode: 401, body: "Practice access required" };
if (!event.body || event.body.length > 2000)
return { statusCode: 413, body: "Request too large" };
let value;
try {
value = JSON.parse(event.body);
} catch {
return { statusCode: 400, body: "Invalid JSON" };
}
const name = typeof value?.name === "string" ? value.name.trim() : "";
if (!name || name.length > 80) return { statusCode: 400, body: "Name required" };
const { RESEND_API_KEY, MAIL_FROM, TEST_RECIPIENT } = process.env;
if (!RESEND_API_KEY || !MAIL_FROM || !TEST_RECIPIENT)
return { statusCode: 503, body: "Mail not configured" };
// Practice delivery is restricted to the owner-configured recipient.
try {
const response = await fetch("https://api.resend.com/emails", {
method: "POST",
headers: {
Authorization: "Bearer " + RESEND_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({
from: MAIL_FROM,
to: [TEST_RECIPIENT],
subject: "Workshop confirmation",
text: "Hello " + name + ". Your practice request was received.",
}),
signal: AbortSignal.timeout(10000),
});
if (!response.ok) return { statusCode: 502, body: "Mail provider unavailable" };
const result = await response.json();
return {
statusCode: 202,
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ status: "accepted", id: result.id }),
};
} catch {
return { statusCode: 502, body: "Mail provider unavailable" };
}
};