import { Resend } from "resend" const resend = new Resend(process.env.RESEND_API_KEY) const FROM_ADDRESS = process.env.RESEND_FROM_ADDRESS || "Nexment " const CORS_HEADERS = { "Access-Control-Allow-Origin": process.env.ALLOWED_ORIGIN || "*", "Access-Control-Allow-Methods": "POST, OPTIONS", "Access-Control-Allow-Headers": "Content-Type", } interface MailerRequestBody { fromName: string toEmail: string content: string url: string } function buildEmailHtml(body: MailerRequestBody): string { return `
Nexment

${body.fromName} left a reply:

${body.content}

Reply back

You're receiving this because someone replied to your comment.

` } export async function POST(request: Request) { const body: MailerRequestBody = await request.json() if (!body.toEmail) { return new Response(JSON.stringify({ error: "Target email missing" }), { status: 400, headers: { "Content-Type": "application/json", ...CORS_HEADERS }, }) } const { data, error } = await resend.emails.send({ from: FROM_ADDRESS, to: [body.toEmail], subject: `You received a new reply from ${body.fromName}!`, html: buildEmailHtml(body), }) if (error) { return new Response(JSON.stringify(error), { status: 500, headers: { "Content-Type": "application/json", ...CORS_HEADERS }, }) } return new Response(JSON.stringify(data), { status: 200, headers: { "Content-Type": "application/json", ...CORS_HEADERS }, }) } export async function OPTIONS() { return new Response(null, { status: 204, headers: CORS_HEADERS }) }