cdrdpyj/src/pages/api/emailInfo/send.ts

63 lines
1.5 KiB
TypeScript

import type { APIRoute } from "astro";
export const prerender = false;
export const POST: APIRoute = async ({ request }) => {
try {
// 🔹 Leer datos del formulario
const formData = await request.formData();
const nombre = formData.get("nombre")?.toString().trim();
const email = formData.get("email")?.toString().trim();
const mensaje = formData.get("mensaje")?.toString().trim();
// 🔹 Validación básica
if (!nombre || !email || !mensaje) {
return new Response(
JSON.stringify({ error: "Datos incompletos" }),
{ status: 302 }
);
}
const emailApiKey = import.meta.env.EMAIL_API_KEY;
// 🔹 Enviar a tu backend real
const response = await fetch("http://155.138.215.11:3050/email/send", {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": emailApiKey!,
},
body: JSON.stringify({
type: "CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_INFO",
data: {
nombre,
email,
mensaje,
},
}),
});
if (!response.ok) {
return new Response(
JSON.stringify({ error: "Error en servicio de correo" }),
{ status: 302 }
);
}
// 🔹 Redirección elegante después de enviar
return new Response(null, {
status: 302,
headers: {
Location: "/?success=true",
},
});
} catch (error) {
return new Response(null, {
status: 302,
headers: {
Location: "/",
},
});
}
};