diff --git a/src/pages/api/lib/email.ts b/src/pages/api/lib/email.ts index 055ebf7..3752377 100644 --- a/src/pages/api/lib/email.ts +++ b/src/pages/api/lib/email.ts @@ -80,24 +80,29 @@ export async function sendEmailCf(data: Record, to: string, type const apiToken = import.meta.env.CLOUDFLARE_API_TOKEN; const accountId = import.meta.env.CLOUDFLARE_ACCOUNT_ID; - const client = new Cloudflare({ apiToken }); - const { subject } = TEMPLATE_URLS[type]; - const rawHtml = await fetchTemplate(type); - const html = renderTemplate(rawHtml, data); - const text = stripHtml(html); + try { + const client = new Cloudflare({ apiToken }); + const { subject } = TEMPLATE_URLS[type]; + const rawHtml = await fetchTemplate(type); + const html = renderTemplate(rawHtml, data); + const text = stripHtml(html); - const response = await client.emailSending.send({ - account_id: accountId, - from: "welcome@mail.centrodelreinodepazyjusticia.com", - to, - subject, - html, - text, - }); + const response = await client.emailSending.send({ + account_id: accountId, + from: "welcome@mail.centrodelreinodepazyjusticia.com", + to, + subject, + html, + text, + }); - if (response.permanent_bounces.includes(to)) { - console.error("Cloudflare email not delivered", response); - throw new Error("El correo no fue entregado"); + if (response.permanent_bounces.includes(to)) { + console.error("Cloudflare email not delivered", response); + throw new Error("El correo no fue entregado"); + } + } catch (error) { + console.error("Cloudflare email failed, fallback a n8n:", error); + await sendEmail(data, to, type); } }