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 general 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 = `
Cavin Infotech
New Website Contact Form Inquiry
| Name: |
${name} |
| Email: |
${email} |
| Phone No: |
${phone || 'Not provided'} |
| Subject: |
${subject || 'General Inquiry'} |
Submitted on ${new Date().toLocaleString()} from Cavin Infotech Website
`;
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) {
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
});
return { success: true, messageId: info.messageId };
} catch (err) {
return { success: false, error: err.message || 'SMTP sending failed' };
}
}
/**
* Sends a Careers application form email via SMTP using nodemailer.
*/
export async function sendCareersEmail({ name, email, phone, resumeName, message, file }) {
const transporter = createSmtpTransporter();
const toEmail = process.env.SMTP_TO || process.env.SMTP_USER || 'info@cavinfotech.com';
const noReplyEmail = process.env.SMTP_FROM || `"Cavin Infotech Careers" <${process.env.SMTP_USER || 'info@cavinfotech.com'}>`;
const mailSubject = `[Careers Application] New Application from ${name}`;
// Internal notification HTML (sent to the team)
const internalHtml = `
Cavin Infotech
New Careers / Job Application
| Applicant Name: |
${name} |
| Email: |
${email} |
| Phone No: |
${phone || 'Not provided'} |
| Resume File: |
${resumeName || 'Attached'} |
${message ? `
Notes / Message:
${message}
` : ''}
Submitted on ${new Date().toLocaleString()} from Cavin Infotech Careers Form
`;
// Auto-reply HTML (sent to the applicant)
const autoReplyHtml = `
Cavin Infotech
Application Received — Thank You!
Dear ${name},
Thank you for applying to Cavin Infotech. We have successfully received your application and resume.
Our team will review your profile and reach out to you if your qualifications match our current openings. We appreciate your interest in joining our team!
Application Summary
Name: ${name}
Email: ${email}
Phone: ${phone || 'Not provided'}
Resume: ${resumeName || 'Submitted'}
This is an automated confirmation email. Please do not reply to this message. For enquiries, contact us at
info@cavinfotech.com.
© ${new Date().getFullYear()} Cavin Infotech. All rights reserved.
`;
if (!transporter) {
return {
success: false,
error: 'SMTP credentials not configured on the server. Please set SMTP_HOST, SMTP_USER, and SMTP_PASS in .env file.'
};
}
try {
// 1. Send internal team notification (with resume attachment)
const mailOptions = {
from: noReplyEmail,
to: toEmail,
replyTo: `"${name}" <${email}>`,
subject: mailSubject,
text: `New Careers Application:\nName: ${name}\nEmail: ${email}\nPhone: ${phone}\nResume: ${resumeName}`,
html: internalHtml
};
if (file) {
mailOptions.attachments = [
{
filename: file.originalname,
content: file.buffer
}
];
}
const info = await transporter.sendMail(mailOptions);
// 2. Send auto-reply confirmation to the applicant
await transporter.sendMail({
from: noReplyEmail,
to: email,
subject: `We've received your application — Cavin Infotech`,
text: `Dear ${name},\n\nThank you for applying to Cavin Infotech. We have received your application and will review it shortly.\n\nThis is an automated confirmation. Please do not reply to this email.\n\n© ${new Date().getFullYear()} Cavin Infotech`,
html: autoReplyHtml
});
return { success: true, messageId: info.messageId };
} catch (err) {
return { success: false, error: err.message || 'SMTP sending failed' };
}
}
/**
* Sends a Strategy Call booking email via SMTP using nodemailer.
*/
export async function sendStrategyEmail({ name, email, phone, designation, dateTime, interests, 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 Strategy Call" <${process.env.SMTP_USER || 'info@cavinfotech.com'}>`;
const mailSubject = `[Strategy Call Booking] New Session Request from ${name}`;
const htmlContent = `
Cavin Infotech
New Strategy Call Session Request
| Client Name: |
${name} |
| Email: |
${email} |
| Mobile: |
${phone || 'Not provided'} |
| Designation: |
${designation || 'Not provided'} |
| Preferred Slot: |
${dateTime || 'Flexible'} |
| Areas of Interest: |
${interests || 'General Consultation'} |
Submitted on ${new Date().toLocaleString()} from Cavin Infotech Book a Strategy Call Modal
`;
if (!transporter) {
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: `New Strategy Call Request:\nName: ${name}\nEmail: ${email}\nMobile: ${phone}\nDesignation: ${designation}\nPreferred Slot: ${dateTime}\nInterests: ${interests}`,
html: htmlContent
});
return { success: true, messageId: info.messageId };
} catch (err) {
return { success: false, error: err.message || 'SMTP sending failed' };
}
}