import nodemailer from 'nodemailer'; /** * Creates and returns a nodemailer SMTP transporter using environment variables. */ export function createSmtpTransporter() { const host = process.env.SMTP_HOST; const port = parseInt(process.env.SMTP_PORT || '465', 10); const secure = process.env.SMTP_SECURE === 'true' || port === 465; const user = process.env.SMTP_USER; const pass = process.env.SMTP_PASS; if (!host || !user || !pass) { return null; } return nodemailer.createTransport({ host, port, secure, auth: { user, pass }, tls: { rejectUnauthorized: false } }); } /** * Sends a contact form email via SMTP using nodemailer. */ export async function sendContactEmail({ name, email, phone, subject, message }) { const transporter = createSmtpTransporter(); const toEmail = process.env.SMTP_TO || process.env.SMTP_USER || 'info@cavinfotech.com'; const fromEmail = process.env.SMTP_FROM || `"Cavin Infotech Contact Form" <${process.env.SMTP_USER || 'info@cavinfotech.com'}>`; const mailSubject = `[Contact Inquiry] ${subject ? subject : `New Inquiry from ${name}`}`; const htmlContent = `
New Website Contact Form Inquiry
| Name: | ${name} |
| Email: | ${email} |
| Phone No: | ${phone || 'Not provided'} |
| Subject: | ${subject || 'General Inquiry'} |
${message}