emails cloudflare

This commit is contained in:
David Ascanio 2026-08-02 13:57:48 -03:00
parent 34230624ce
commit 9aa0105fca
2 changed files with 8 additions and 38 deletions

View File

@ -1,5 +1,6 @@
import type { APIRoute } from "astro"; import type { APIRoute } from "astro";
import { appendToSheet, emailExists } from "../lib/googleSheets"; import { appendToSheet, emailExists } from "../lib/googleSheets";
import { sendEmailCf } from "../lib/email";
export const prerender = false; export const prerender = false;
export const POST: APIRoute = async ({ request }) => { export const POST: APIRoute = async ({ request }) => {
@ -29,17 +30,6 @@ export const POST: APIRoute = async ({ request }) => {
}) })
} }
const dataSend = {
nombres,
apellidos,
pais,
lugar,
direccion,
telefono,
email
};
const emailApiKey = import.meta.env.EMAIL_API_KEY;
const valuesForSheets = [ const valuesForSheets = [
nombres, nombres,
apellidos, apellidos,
@ -51,31 +41,12 @@ export const POST: APIRoute = async ({ request }) => {
new Date().toLocaleString() new Date().toLocaleString()
]; ];
// 🔹 Enviar a tu backend real // 🔹 Enviar correo (Cloudflare) y guardar en Google Sheets
const [emailResponse] = await Promise.all([ await Promise.all([
fetch("http://155.138.215.11:3050/email/send", { sendEmailCf({ nombres, apellidos }, email, "CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_POSTULACION"),
method: "POST", appendToSheet(valuesForSheets)
headers: {
"Content-Type": "application/json",
"x-api-key": emailApiKey!,
},
body: JSON.stringify({
type: "CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_POSTULACION",
data: dataSend,
}),
}),
appendToSheet(valuesForSheets) // Guardar en Google Sheets
]); ]);
if (!emailResponse.ok) {
return new Response(null, {
status: 302,
headers: {
Location: "/",
},
});
}
// 🔹 Redirección elegante después de enviar // 🔹 Redirección elegante después de enviar
return new Response(null, { return new Response(null, {
status: 302, status: 302,

View File

@ -39,11 +39,11 @@ export async function sendEmail(data: object, to: string, type: TypeEmailData) {
const TEMPLATE_URLS: Record<TypeEmailData, { subject: string; url: string }> = { const TEMPLATE_URLS: Record<TypeEmailData, { subject: string; url: string }> = {
CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_POSTULACION: { CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_POSTULACION: {
subject: "Confirmación de inscripción al voluntariado", subject: "Confirmación de inscripción al voluntariado",
url: "https://pub-910ed90860804520b9202194cf43c0a6.r2.dev/centro_del_reino_paz_y_justicia_postulacion_user.html", url: "https://assets.centrodelreinodepazyjusticia.com/centro_del_reino_paz_y_justicia_postulacion_user.html",
}, },
CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_INFO: { CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_INFO: {
subject: "Notificación de contacto institucional", subject: "Notificación de contacto institucional",
url: "https://pub-910ed90860804520b9202194cf43c0a6.r2.dev/centro_del_reino_paz_y_justicia_info.html", url: "https://assets.centrodelreinodepazyjusticia.com/centro_del_reino_paz_y_justicia_info.html",
}, },
}; };
@ -95,8 +95,7 @@ export async function sendEmailCf(data: Record<string, string>, to: string, type
text, text,
}); });
const deliveredOrQueued = [...response.delivered, ...response.queued]; if (response.permanent_bounces.includes(to)) {
if (!deliveredOrQueued.includes(to) || response.permanent_bounces.includes(to)) {
console.error("Cloudflare email not delivered", response); console.error("Cloudflare email not delivered", response);
throw new Error("El correo no fue entregado"); throw new Error("El correo no fue entregado");
} }