"use server"; import { z } from "zod"; import nodemailer from "nodemailer"; const contactSchema = z.object({ name: z.string().min(2, "Name must be at least 2 characters."), email: z.string().email("Invalid email address."), message: z.string().min(10, "Message must be at least 10 characters."), }); export async function submitContactForm(prevState: any, formData: FormData) { const validatedFields = contactSchema.safeParse({ name: formData.get("name"), email: formData.get("email"), message: formData.get("message"), }); if (!validatedFields.success) { return { errors: validatedFields.error.flatten().fieldErrors, message: "Please correct the errors below.", }; } const { name, email, message } = validatedFields.data; // Note: Using an App Password is highly recommended for Gmail. const transporter = nodemailer.createTransport({ host: "smtp.gmail.com", port: 465, secure: true, // true for 465, false for other ports auth: { user: process.env.EMAIL_USER, // Your Gmail address pass: process.env.EMAIL_PASS, // Your Gmail App Password }, }); const mailOptions = { from: process.env.EMAIL_USER, to: "slimshadow.work@gmail.com", subject: `Contact Form Submission from ${name}`, text: `You have a new message from ${name} (${email}):\n\n${message}`, }; try { await transporter.sendMail(mailOptions); return { message: "Thank you for your message! I'll get back to you soon." }; } catch (error) { console.error("Error sending email:", error); return { errors: {}, message: "Sorry, there was an error sending your message. Please try again later.", }; } }