120 lines
4.4 KiB
JavaScript
120 lines
4.4 KiB
JavaScript
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 = `
|
|
<div style="font-family: 'Segoe UI', Helvetica, Arial, sans-serif; background-color: #020710; color: #f8fafc; padding: 40px 20px; max-width: 650px; margin: 0 auto; border-radius: 16px; border: 1px solid #1e293b;">
|
|
<div style="text-align: center; padding-bottom: 24px; border-bottom: 1px solid #334155;">
|
|
<h2 style="color: #ffaa00; font-size: 24px; margin: 0; font-weight: 700;">Cavin Infotech</h2>
|
|
<p style="color: #94a3b8; font-size: 14px; margin-top: 6px;">New Website Contact Form Inquiry</p>
|
|
</div>
|
|
|
|
<div style="padding: 24px 0;">
|
|
<table style="width: 100%; border-collapse: collapse; color: #e2e8f0; font-size: 15px;">
|
|
<tr>
|
|
<td style="padding: 10px 0; font-weight: 600; color: #ffaa00; width: 130px;">Name:</td>
|
|
<td style="padding: 10px 0; color: #ffffff;">${name}</td>
|
|
</tr>
|
|
<tr>
|
|
<td style="padding: 10px 0; font-weight: 600; color: #ffaa00;">Email:</td>
|
|
<td style="padding: 10px 0;"><a href="mailto:${email}" style="color: #38bdf8; text-decoration: none;">${email}</a></td>
|
|
</tr>
|
|
<tr>
|
|
<td style="padding: 10px 0; font-weight: 600; color: #ffaa00;">Phone No:</td>
|
|
<td style="padding: 10px 0; color: #ffffff;">${phone || 'Not provided'}</td>
|
|
</tr>
|
|
<tr>
|
|
<td style="padding: 10px 0; font-weight: 600; color: #ffaa00;">Subject:</td>
|
|
<td style="padding: 10px 0; color: #ffffff;">${subject || 'General Inquiry'}</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<div style="margin-top: 24px; padding: 20px; background-color: #0b1528; border-left: 4px solid #ffaa00; border-radius: 8px;">
|
|
<h4 style="margin: 0 0 10px 0; color: #94a3b8; font-size: 13px; text-transform: uppercase; letter-spacing: 0.05em;">Message:</h4>
|
|
<p style="margin: 0; color: #f1f5f9; font-size: 15px; line-height: 1.6; white-space: pre-line;">${message}</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div style="text-align: center; padding-top: 20px; border-top: 1px solid #334155; color: #64748b; font-size: 12px;">
|
|
Submitted on ${new Date().toLocaleString()} from Cavin Infotech Contact Us Page
|
|
</div>
|
|
</div>
|
|
`;
|
|
|
|
const textContent = `
|
|
New Contact Inquiry - Cavin Infotech
|
|
|
|
Name: ${name}
|
|
Email: ${email}
|
|
Phone: ${phone || 'Not provided'}
|
|
Subject: ${subject || 'General Inquiry'}
|
|
|
|
Message:
|
|
${message}
|
|
|
|
Submitted on: ${new Date().toLocaleString()}
|
|
`;
|
|
|
|
if (!transporter) {
|
|
console.warn('[SMTP Service] SMTP credentials not set in environment (SMTP_HOST, SMTP_USER, SMTP_PASS).');
|
|
return {
|
|
success: false,
|
|
error: 'SMTP credentials not configured on the server. Please set SMTP_HOST, SMTP_USER, and SMTP_PASS in .env file.'
|
|
};
|
|
}
|
|
|
|
try {
|
|
const info = await transporter.sendMail({
|
|
from: fromEmail,
|
|
to: toEmail,
|
|
replyTo: `"${name}" <${email}>`,
|
|
subject: mailSubject,
|
|
text: textContent,
|
|
html: htmlContent
|
|
});
|
|
|
|
console.log('[SMTP Service] Contact email sent via SMTP:', info.messageId);
|
|
return { success: true, messageId: info.messageId };
|
|
} catch (err) {
|
|
console.error('[SMTP Service] Failed to send email via SMTP:', err);
|
|
return { success: false, error: err.message || 'SMTP sending failed' };
|
|
}
|
|
}
|