webdev-service-backend / src /services /email.service.ts
underrate's picture
Initial commit
d53e5b4 verified
Raw
History Blame Contribute Delete
4.82 kB
import nodemailer from 'nodemailer';
// Email configuration
const smtpHost = process.env.SMTP_HOST || '';
const smtpPort = process.env.SMTP_PORT ? parseInt(process.env.SMTP_PORT, 10) : 587;
const smtpUser = process.env.SMTP_USER || '';
const smtpPass = process.env.SMTP_PASS || '';
const fromEmail = process.env.FROM_EMAIL || 'noreply@nexaflow.com';
const adminEmail = process.env.ADMIN_EMAIL || 'admin@nexaflow.com';
const isConfigured = Boolean(smtpHost && smtpUser && smtpPass);
// Create reusable transporter object using the default SMTP transport
const transporter = nodemailer.createTransport({
host: smtpHost,
port: smtpPort,
secure: smtpPort === 465, // true for 465, false for other ports
auth: {
user: smtpUser, // generated ethereal user
pass: smtpPass, // generated ethereal password
},
});
interface InquiryData {
referenceNumber: string;
name: string;
email: string;
projectType: string;
budget?: string;
timeline?: string;
description: string;
company?: string;
phone?: string;
}
export const sendQuoteConfirmation = async (inquiry: InquiryData) => {
const targetEmail = inquiry.email;
const subject = `Quote Request Received - Ref: ${inquiry.referenceNumber}`;
const htmlBody = `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
<h2>Hello ${inquiry.name},</h2>
<p>Thank you for requesting a quote from NexaFlow! We have received your inquiry.</p>
<p><strong>Your Reference Number:</strong> ${inquiry.referenceNumber}</p>
<hr />
<p><strong>Project Details Summarized:</strong></p>
<ul>
<li><strong>Type:</strong> ${inquiry.projectType}</li>
<li><strong>Budget:</strong> ${inquiry.budget || 'Not specified'}</li>
<li><strong>Timeline:</strong> ${inquiry.timeline || 'Not specified'}</li>
</ul>
<p>Our team will review your requirements and get back to you within 24 hours with a detailed proposal or further questions.</p>
<p>Best regards,<br/>The NexaFlow Team</p>
</div>
`;
return sendEmail(targetEmail, subject, htmlBody);
};
export const sendAdminNotification = async (inquiry: InquiryData) => {
const subject = `New Quote Request - ${inquiry.projectType.toUpperCase()} - Ref: ${inquiry.referenceNumber}`;
const htmlBody = `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
<h2>New Quote Request Received</h2>
<p>A new inquiry has been submitted via the website form.</p>
<hr />
<p><strong>Reference Number:</strong> ${inquiry.referenceNumber}</p>
<p><strong>Client Details:</strong></p>
<ul>
<li><strong>Name:</strong> ${inquiry.name}</li>
<li><strong>Email:</strong> ${inquiry.email}</li>
<li><strong>Phone:</strong> ${inquiry.phone || 'Not provided'}</li>
<li><strong>Company:</strong> ${inquiry.company || 'Not provided'}</li>
</ul>
<p><strong>Project Details:</strong></p>
<ul>
<li><strong>Type:</strong> ${inquiry.projectType}</li>
<li><strong>Budget:</strong> ${inquiry.budget || 'Not specified'}</li>
<li><strong>Timeline:</strong> ${inquiry.timeline || 'Not specified'}</li>
</ul>
<p><strong>Description:</strong></p>
<p>${inquiry.description}</p>
</div>
`;
return sendEmail(adminEmail, subject, htmlBody);
};
const sendEmail = async (to: string, subject: string, html: string) => {
if (!isConfigured) {
// Fallback for local development or when SMTP is not configured
console.log('-------------------------------------------------------');
console.log('📧 EMAIL MOCK (SMTP not configured)');
console.log(`To: ${to}`);
console.log(`From: ${fromEmail}`);
console.log(`Subject: ${subject}`);
console.log('Body HTML preview:');
console.log(html);
console.log('-------------------------------------------------------');
return true;
}
try {
const info = await transporter.sendMail({
from: `"NexaFlow" <${fromEmail}>`, // sender address
to, // list of receivers
subject, // Subject line
html, // html body
});
console.log("Message sent: %s", info.messageId);
return true;
} catch (error) {
console.error("Error sending email:", error);
return false;
}
};