From 8f6e530dc48be3d731d5e03dc59ece1408c45181 Mon Sep 17 00:00:00 2001 From: Ajay Ghanwat Date: Mon, 13 Apr 2026 12:51:29 +0530 Subject: [PATCH] fix: correct SMTP transporter config with env vars and STARTTLS settings --- src/pages/api/contact.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/pages/api/contact.ts b/src/pages/api/contact.ts index 8fdf1bf..e9928ca 100644 --- a/src/pages/api/contact.ts +++ b/src/pages/api/contact.ts @@ -94,11 +94,12 @@ function validateContactForm(data: ContactFormData): string | null { // Email sending via SMTP (nodemailer) or no-op in development // ============================================================ async function sendContactEmail(data: ContactFormData): Promise { - const smtpHost = import.meta.env.SMTP_HOST; - const smtpUser = import.meta.env.SMTP_USER; - const smtpPass = import.meta.env.SMTP_PASS; - const smtpPort = parseInt(import.meta.env.SMTP_PORT ?? '587', 10); - const recipientEmail = import.meta.env.CONTACT_EMAIL ?? import.meta.env.SMTP_USER; + const smtpHost = process.env.SMTP_HOST; + const smtpUser = process.env.SMTP_USER; + const smtpPass = process.env.SMTP_PASS; + const smtpPort = parseInt(process.env.SMTP_PORT ?? '587', 10); + const smtpFrom = process.env.SMTP_FROM; + const recipientEmail = process.env.CONTACT_EMAIL_TO; if (!smtpHost || !smtpUser || !smtpPass || !recipientEmail) { // SMTP not configured - log and continue (graceful degradation) @@ -116,8 +117,12 @@ async function sendContactEmail(data: ContactFormData): Promise { const transporter = nodemailer.default.createTransport({ host: smtpHost, port: smtpPort, - secure: smtpPort === 465, + secure: false, // Port 587 uses STARTTLS, not SSL auth: { user: smtpUser, pass: smtpPass }, + requireTLS: true, // Force STARTTLS upgrade + tls: { + rejectUnauthorized: false, // Handle Hostinger certificate issues + }, }); const subjectLabels: Record = { @@ -130,8 +135,10 @@ async function sendContactEmail(data: ContactFormData): Promise { 'other': 'Other', }; + const fromAddress = smtpFrom || smtpUser; + await transporter.sendMail({ - from: `"WorkRoot Contact Form" <${smtpUser}>`, + from: `"CompanySite Contact Form" <${fromAddress}>`, to: recipientEmail, replyTo: data.email, subject: `[Contact Form] ${subjectLabels[data.subject] ?? data.subject} - ${data.name}`,