/*********************************************************************************************************************************************************************** * File Name: email.service.js * Type of Program: Service * Description: Nodemailer-based email service. * Provides: * - sendOTPEmail() → sends a 6-digit OTP verification email * - sendWelcomeEmail() → sent after successful email verification * Author: rgrgogu * Date Created: Oct. 6, 2025 *********************************************************************************************************************************************************************** * HOW TO USE: * const emailService = require('../services/email.service'); * await emailService.sendOTPEmail(user.email, otp); ***********************************************************************************************************************************************************************/ const nodemailer = require('nodemailer'); const { emailTemplates } = require('../data/email_body.data') const transporter = nodemailer.createTransport({ host: process.env.SMTP_HOST, port: Number(process.env.SMTP_PORT), auth: { user: process.env.SMTP_USER, pass: process.env.SMTP_PASS, }, }); const buildEmailTemplate = ({ title, body }) => { return `
Philproperties

${title}

${body}

Regards,

Philproperties IT Team

This is an automated message from STARR System. Please do not reply.
`; }; /** * Sends a 6-digit OTP to the given email address. * @param {string} to - recipient email * @param {string} otp - 6-digit code * @param {number} expiryMinutes */ const sendEmail = async ({ to, type, data = {} }) => { try { const templateFn = emailTemplates[type]; if (!templateFn) { throw new Error(`Email template "${type}" not found`); } const { subject, title, body } = templateFn(data); const html = buildEmailTemplate({ title, body }); return await new Promise((resolve, reject) => { transporter.sendMail( { from: { name: "STARR System", address: "do-not-reply@philproperties.com", }, to, subject, html, }, (err, info) => { if (err) return reject(err); resolve(info); } ); }); } catch (err) { throw new Error(err.message); } }; module.exports = sendEmail;