|
|
|
|
@ -1,138 +1,110 @@
|
|
|
|
|
// import Cloudflare from "cloudflare";
|
|
|
|
|
import Cloudflare from "cloudflare";
|
|
|
|
|
|
|
|
|
|
type TypeEmailData = "CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_INFO" | "CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_POSTULACION";
|
|
|
|
|
|
|
|
|
|
export async function sendEmail(data: object, to: string, type: TypeEmailData) {
|
|
|
|
|
const emailApiKey = import.meta.env.EMAIL_API_KEY;
|
|
|
|
|
|
|
|
|
|
const dataFinal = {
|
|
|
|
|
...data,
|
|
|
|
|
email: to,
|
|
|
|
|
senderName: "Centro del Reino de Paz y Justicia",
|
|
|
|
|
const TEMPLATE_URLS: Record<TypeEmailData, { subject: string; url: string }> = {
|
|
|
|
|
CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_POSTULACION: {
|
|
|
|
|
subject: "Confirmación de inscripción al voluntariado",
|
|
|
|
|
url: "https://pub-910ed90860804520b9202194cf43c0a6.r2.dev/centro_del_reino_paz_y_justicia_postulacion_user.html",
|
|
|
|
|
},
|
|
|
|
|
CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_INFO: {
|
|
|
|
|
subject: "Notificación de contacto institucional",
|
|
|
|
|
url: "https://pub-910ed90860804520b9202194cf43c0a6.r2.dev/centro_del_reino_paz_y_justicia_info.html",
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const emailResponse = await fetch("https://flows2.carpa.com/webhook/email/send", {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
"x-api-key": emailApiKey!,
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
type: type,
|
|
|
|
|
data: dataFinal,
|
|
|
|
|
}),
|
|
|
|
|
const TEMPLATE_CACHE = new Map<string, string>();
|
|
|
|
|
|
|
|
|
|
async function fetchTemplate(type: TypeEmailData): Promise<string> {
|
|
|
|
|
const cached = TEMPLATE_CACHE.get(type);
|
|
|
|
|
if (cached) return cached;
|
|
|
|
|
|
|
|
|
|
const { url } = TEMPLATE_URLS[type];
|
|
|
|
|
const res = await fetch(url);
|
|
|
|
|
if (!res.ok) {
|
|
|
|
|
throw new Error(`Error descargando template ${type}: HTTP ${res.status}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const html = await res.text();
|
|
|
|
|
TEMPLATE_CACHE.set(type, html);
|
|
|
|
|
return html;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderTemplate(template: string, data: Record<string, string>): string {
|
|
|
|
|
let html = template;
|
|
|
|
|
for (const [key, value] of Object.entries(data)) {
|
|
|
|
|
html = html.replaceAll(new RegExp(`\\{\\{${key}\\}\\}`, "g"), value ?? "");
|
|
|
|
|
}
|
|
|
|
|
return html;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function stripHtml(html: string): string {
|
|
|
|
|
return html.replace(/<[^>]*>/g, "").replace(/\s+/g, " ").trim();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function sendEmail(data: Record<string, string>, to: string, type: TypeEmailData) {
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
const response = await client.emailSending.send({
|
|
|
|
|
account_id: accountId,
|
|
|
|
|
from: "welcome@mail.centrodelreinodepazyjusticia.com",
|
|
|
|
|
to,
|
|
|
|
|
subject,
|
|
|
|
|
html,
|
|
|
|
|
text,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const responseBody = await emailResponse.text();
|
|
|
|
|
|
|
|
|
|
if (!emailResponse.ok) {
|
|
|
|
|
console.error(`Error enviando correo (status ${emailResponse.status}): ${responseBody}`);
|
|
|
|
|
throw new Error(`Error en servicio de correo: ${emailResponse.status}`);
|
|
|
|
|
if (!response.delivered) {
|
|
|
|
|
console.error("Cloudflare email not delivered", response);
|
|
|
|
|
throw new Error("El correo no fue entregado");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ═══════════════════════════════════════════════════════════════
|
|
|
|
|
// CÓDIGO COMENTADO — Cloudflare (Email Workers)
|
|
|
|
|
// Descomentar para volver a Cloudflare en lugar de n8n/Brevo
|
|
|
|
|
// ═══════════════════════════════════════════════════════════════
|
|
|
|
|
/*
|
|
|
|
|
┌────────────────────────────────────────────────────────────┐
|
|
|
|
|
│ ALTERNATIVA: Brevo (API v3) │
|
|
|
|
|
│ Para volver a Brevo: │
|
|
|
|
|
│ 1. Instalar: pnpm add @getbrevo/brevo │
|
|
|
|
|
│ 2. Agregar a .env: BREVO_API_KEY=xxx │
|
|
|
|
|
│ 3. Cambiar "from" a tu dominio verificado en Brevo │
|
|
|
|
|
│ 4. En sendEmail(), comentar el bloque Cloudflare │
|
|
|
|
|
│ y descomentar la llamada a sendEmailViaBrevo() │
|
|
|
|
|
└────────────────────────────────────────────────────────────┘
|
|
|
|
|
|
|
|
|
|
// const TEMPLATE_URLS: Record<TypeEmailData, { subject: string; url: string }> = {
|
|
|
|
|
// CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_POSTULACION: {
|
|
|
|
|
// subject: "Confirmación de inscripción al voluntariado",
|
|
|
|
|
// url: "https://pub-910ed90860804520b9202194cf43c0a6.r2.dev/centro_del_reino_paz_y_justicia_postulacion_user.html",
|
|
|
|
|
// },
|
|
|
|
|
// CENTRO_DEL_REINO_PAZ_Y_JUSTICIA_INFO: {
|
|
|
|
|
// subject: "Notificación de contacto institucional",
|
|
|
|
|
// url: "https://pub-910ed90860804520b9202194cf43c0a6.r2.dev/centro_del_reino_paz_y_justicia_info.html",
|
|
|
|
|
// },
|
|
|
|
|
// };
|
|
|
|
|
import {
|
|
|
|
|
TransactionalEmailsApi,
|
|
|
|
|
SendSmtpEmail,
|
|
|
|
|
} from "@getbrevo/brevo";
|
|
|
|
|
|
|
|
|
|
// const TEMPLATE_CACHE = new Map<string, string>();
|
|
|
|
|
async function sendEmailViaBrevo(
|
|
|
|
|
data: Record<string, string>,
|
|
|
|
|
to: string,
|
|
|
|
|
type: TypeEmailData
|
|
|
|
|
) {
|
|
|
|
|
const apiKey = import.meta.env.BREVO_API_KEY;
|
|
|
|
|
const { subject } = TEMPLATE_URLS[type];
|
|
|
|
|
const rawHtml = await fetchTemplate(type);
|
|
|
|
|
const html = renderTemplate(rawHtml, data);
|
|
|
|
|
const text = stripHtml(html);
|
|
|
|
|
|
|
|
|
|
// async function fetchTemplate(type: TypeEmailData): Promise<string> {
|
|
|
|
|
// const cached = TEMPLATE_CACHE.get(type);
|
|
|
|
|
// if (cached) return cached;
|
|
|
|
|
const api = new TransactionalEmailsApi();
|
|
|
|
|
api.setApiKey(0, apiKey);
|
|
|
|
|
|
|
|
|
|
// const { url } = TEMPLATE_URLS[type];
|
|
|
|
|
// const res = await fetch(url);
|
|
|
|
|
// if (!res.ok) {
|
|
|
|
|
// throw new Error(`Error descargando template ${type}: HTTP ${res.status}`);
|
|
|
|
|
// }
|
|
|
|
|
const email = new SendSmtpEmail();
|
|
|
|
|
email.subject = subject;
|
|
|
|
|
email.sender = { name: "Centro del Reino de Paz y Justicia", email: "welcome@mail.centrodelreinodepazyjusticia.com" };
|
|
|
|
|
email.to = [{ email: to }];
|
|
|
|
|
email.htmlContent = html;
|
|
|
|
|
email.textContent = text;
|
|
|
|
|
|
|
|
|
|
// const html = await res.text();
|
|
|
|
|
// TEMPLATE_CACHE.set(type, html);
|
|
|
|
|
// return html;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// function renderTemplate(template: string, data: Record<string, string>): string {
|
|
|
|
|
// let html = template;
|
|
|
|
|
// for (const [key, value] of Object.entries(data)) {
|
|
|
|
|
// html = html.replaceAll(new RegExp(`\\{\\{${key}\\}\\}`, "g"), value ?? "");
|
|
|
|
|
// }
|
|
|
|
|
// return html;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// function stripHtml(html: string): string {
|
|
|
|
|
// return html.replace(/<[^>]*>/g, "").replace(/\s+/g, " ").trim();
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// export async function sendEmailCf(data: Record<string, string>, to: string, type: TypeEmailData) {
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
|
|
// const response = await client.emailSending.send({
|
|
|
|
|
// account_id: accountId,
|
|
|
|
|
// from: "welcome@mail.centrodelreinodepazyjusticia.com",
|
|
|
|
|
// to,
|
|
|
|
|
// subject,
|
|
|
|
|
// html,
|
|
|
|
|
// text,
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// if (!response.delivered) {
|
|
|
|
|
// console.error("Cloudflare email not delivered", response);
|
|
|
|
|
// throw new Error("El correo no fue entregado");
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// ═══════════════════════════════════════════════════════════════
|
|
|
|
|
// ALTERNATIVA: Brevo (SDK directo)
|
|
|
|
|
// Descomentar y usar sendEmailViaBrevo() en lugar de lo de arriba
|
|
|
|
|
// Requiere: pnpm add @getbrevo/brevo + BREVO_API_KEY en .env
|
|
|
|
|
// ═══════════════════════════════════════════════════════════════
|
|
|
|
|
|
|
|
|
|
// import {
|
|
|
|
|
// TransactionalEmailsApi,
|
|
|
|
|
// SendSmtpEmail,
|
|
|
|
|
// } from "@getbrevo/brevo";
|
|
|
|
|
|
|
|
|
|
// async function sendEmailViaBrevo(
|
|
|
|
|
// data: Record<string, string>,
|
|
|
|
|
// to: string,
|
|
|
|
|
// type: TypeEmailData
|
|
|
|
|
// ) {
|
|
|
|
|
// const apiKey = import.meta.env.BREVO_API_KEY;
|
|
|
|
|
// const { subject } = TEMPLATE_URLS[type];
|
|
|
|
|
// const rawHtml = await fetchTemplate(type);
|
|
|
|
|
// const html = renderTemplate(rawHtml, data);
|
|
|
|
|
// const text = stripHtml(html);
|
|
|
|
|
|
|
|
|
|
// const api = new TransactionalEmailsApi();
|
|
|
|
|
// api.setApiKey(0, apiKey);
|
|
|
|
|
|
|
|
|
|
// const email = new SendSmtpEmail();
|
|
|
|
|
// email.subject = subject;
|
|
|
|
|
// email.sender = { name: "Centro del Reino de Paz y Justicia", email: "welcome@mail.centrodelreinodepazyjusticia.com" };
|
|
|
|
|
// email.to = [{ email: to }];
|
|
|
|
|
// email.htmlContent = html;
|
|
|
|
|
// email.textContent = text;
|
|
|
|
|
|
|
|
|
|
// const response = await api.sendTransacEmail(email);
|
|
|
|
|
// console.log("Brevo email sent:", response.body?.messageId);
|
|
|
|
|
// }
|
|
|
|
|
const response = await api.sendTransacEmail(email);
|
|
|
|
|
console.log("Brevo email sent:", response.body?.messageId);
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|