fix: correct SMTP transporter config with env vars and STARTTLS settings
This commit is contained in:
@@ -94,11 +94,12 @@ function validateContactForm(data: ContactFormData): string | null {
|
|||||||
// Email sending via SMTP (nodemailer) or no-op in development
|
// Email sending via SMTP (nodemailer) or no-op in development
|
||||||
// ============================================================
|
// ============================================================
|
||||||
async function sendContactEmail(data: ContactFormData): Promise<void> {
|
async function sendContactEmail(data: ContactFormData): Promise<void> {
|
||||||
const smtpHost = import.meta.env.SMTP_HOST;
|
const smtpHost = process.env.SMTP_HOST;
|
||||||
const smtpUser = import.meta.env.SMTP_USER;
|
const smtpUser = process.env.SMTP_USER;
|
||||||
const smtpPass = import.meta.env.SMTP_PASS;
|
const smtpPass = process.env.SMTP_PASS;
|
||||||
const smtpPort = parseInt(import.meta.env.SMTP_PORT ?? '587', 10);
|
const smtpPort = parseInt(process.env.SMTP_PORT ?? '587', 10);
|
||||||
const recipientEmail = import.meta.env.CONTACT_EMAIL ?? import.meta.env.SMTP_USER;
|
const smtpFrom = process.env.SMTP_FROM;
|
||||||
|
const recipientEmail = process.env.CONTACT_EMAIL_TO;
|
||||||
|
|
||||||
if (!smtpHost || !smtpUser || !smtpPass || !recipientEmail) {
|
if (!smtpHost || !smtpUser || !smtpPass || !recipientEmail) {
|
||||||
// SMTP not configured - log and continue (graceful degradation)
|
// SMTP not configured - log and continue (graceful degradation)
|
||||||
@@ -116,8 +117,12 @@ async function sendContactEmail(data: ContactFormData): Promise<void> {
|
|||||||
const transporter = nodemailer.default.createTransport({
|
const transporter = nodemailer.default.createTransport({
|
||||||
host: smtpHost,
|
host: smtpHost,
|
||||||
port: smtpPort,
|
port: smtpPort,
|
||||||
secure: smtpPort === 465,
|
secure: false, // Port 587 uses STARTTLS, not SSL
|
||||||
auth: { user: smtpUser, pass: smtpPass },
|
auth: { user: smtpUser, pass: smtpPass },
|
||||||
|
requireTLS: true, // Force STARTTLS upgrade
|
||||||
|
tls: {
|
||||||
|
rejectUnauthorized: false, // Handle Hostinger certificate issues
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const subjectLabels: Record<string, string> = {
|
const subjectLabels: Record<string, string> = {
|
||||||
@@ -130,8 +135,10 @@ async function sendContactEmail(data: ContactFormData): Promise<void> {
|
|||||||
'other': 'Other',
|
'other': 'Other',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const fromAddress = smtpFrom || smtpUser;
|
||||||
|
|
||||||
await transporter.sendMail({
|
await transporter.sendMail({
|
||||||
from: `"WorkRoot Contact Form" <${smtpUser}>`,
|
from: `"CompanySite Contact Form" <${fromAddress}>`,
|
||||||
to: recipientEmail,
|
to: recipientEmail,
|
||||||
replyTo: data.email,
|
replyTo: data.email,
|
||||||
subject: `[Contact Form] ${subjectLabels[data.subject] ?? data.subject} - ${data.name}`,
|
subject: `[Contact Form] ${subjectLabels[data.subject] ?? data.subject} - ${data.name}`,
|
||||||
|
|||||||
Reference in New Issue
Block a user