import { NextResponse } from "next/server"; import { Resend } from "resend"; // Helper function to escape HTML entities function escapeHtml(text: string): string { const map: Record = { "&": "&", "<": "<", ">": ">", '"': """, "'": "'", }; return text.replace(/[&<>"']/g, (m) => map[m]); } export async function POST(request: Request) { try { const body = await request.json(); const { name, email, company, website, userCount, interest, features, additional, migrating, currentPlatform, } = body ?? {}; if (!name || !email) { return NextResponse.json( { error: "Missing required fields" }, { status: 400 }, ); } const toEmail = process.env.SUPPORT_EMAIL; if (!toEmail) { return NextResponse.json( { error: "Missing required fields" }, { status: 400 }, ); } const resendApiKey = process.env.RESEND_API_KEY; if (resendApiKey) { try { const resend = new Resend(resendApiKey); await resend.emails.send({ from: "Enterprise Support ", to: toEmail || "", subject: `${interest === "enterprise" ? "Enterprise" : "Support"} Inquiry from ${name}`, html: `

${interest === "enterprise" ? "Enterprise" : "Support"} Inquiry

Name: ${escapeHtml(name)}

Email: ${escapeHtml(email)}

${company ? `

Company: ${escapeHtml(company)}

` : ""} ${website ? `

Website: ${escapeHtml(website)}

` : ""} ${userCount ? `

User Count: ${escapeHtml(userCount)}

` : ""} ${migrating ? `

Migrating: ${migrating === "yes" ? "Yes" : "No"}

` : ""} ${currentPlatform ? `

Current Platform: ${escapeHtml(currentPlatform)}

` : ""} ${interest ? `

Interest: ${escapeHtml(interest)}

` : ""} ${features ? `

Features: ${escapeHtml(features)}

` : ""} ${additional ? `

Message:
${escapeHtml(additional).replace(/\n/g, "
")}

` : ""}

Submitted: ${new Date().toLocaleString()}
User Agent: ${escapeHtml(request.headers.get("user-agent") || "N/A")}
Referer: ${escapeHtml(request.headers.get("referer") || "N/A")}

`, }); } catch (e) { console.error("Resend email failed", e); } } return NextResponse.json({ ok: true }); } catch (e) { console.error(e); return NextResponse.json({ error: "Invalid request" }, { status: 400 }); } }