"use server"; import nodemailer from "nodemailer"; interface ContactFormData { fullName: string; email: string; subject: string; message: string; } export async function sendEmailAction(data: ContactFormData) { const { fullName, email, subject, message } = data; if (!fullName || !email || !message) { return { success: false, error: "Name, email, and message are required." }; } let password = process.env.SMTP_PASSWORD || ""; if (password.startsWith('"') && password.endsWith('"')) { password = password.slice(1, -1); } // Remove spaces just in case password = password.replace(/\s+/g, ""); const transporter = nodemailer.createTransport({ host: process.env.SMTP_HOST, port: Number(process.env.SMTP_PORT) || 465, secure: true, auth: { user: process.env.SMTP_USER, pass: password, }, }); try { await transporter.sendMail({ from: `"${fullName}" <${process.env.SMTP_USER}>`, to: process.env.CONTACT_RECEIVER, replyTo: email, subject: `New Contact Form Submission: ${subject || "No Subject"}`, html: `

New Contact Form Submission

Name: ${fullName}

Email: ${email}

Subject: ${subject}

Message:

${message.replace(/\n/g, "
")}
`, }); return { success: true }; } catch (error: any) { console.error("Email send error:", error); const errorMessage = error instanceof Error ? error.message : String(error); return { success: false, error: errorMessage || "Failed to send the email. Please try again later." }; } }