import nodemailer from "nodemailer"; import { NextResponse } from "next/server"; import { isAdminAuthenticated } from "@/lib/admin-auth"; import { getMessageById, markMessageReply } from "@/lib/db"; async function sendReplyEmail(input: { to: string; reply: string; senderName: string; originalMessage: string }) { const smtpHost = process.env.SMTP_HOST; const smtpPort = Number(process.env.SMTP_PORT || "587"); const smtpUser = process.env.SMTP_USER; const smtpPass = process.env.SMTP_PASS; if (!smtpHost || !smtpUser || !smtpPass) { throw new Error("SMTP non configure pour les reponses admin."); } const transporter = nodemailer.createTransport({ host: smtpHost, port: Number.isNaN(smtpPort) ? 587 : smtpPort, secure: smtpPort === 465, auth: { user: smtpUser, pass: smtpPass }, }); const fromEmail = process.env.CONTACT_FROM_EMAIL || smtpUser; const escapedOriginal = input.originalMessage .replace(/&/g, "&") .replace(//g, ">"); const escapedReply = input.reply .replace(/&/g, "&") .replace(//g, ">"); await transporter.sendMail({ from: fromEmail, to: input.to, subject: "Reponse a votre message", text: [ `Bonjour ${input.senderName},`, "", "Merci pour votre message. Voici ma reponse:", "", input.reply, "", "---------------------------", "Votre message initial:", input.originalMessage, "---------------------------", "", "ArthurP", ].join("\n"), html: `
Bonjour ${input.senderName},
Merci pour votre message. Voici ma reponse:
Votre message initial:
ArthurP