first commit

This commit is contained in:
Isuru Bimsara
2026-08-14 22:46:02 +05:30
commit 81d0e65686
132 changed files with 24398 additions and 0 deletions
+89
View File
@@ -0,0 +1,89 @@
/**
* Copyright (c) 2026 Niolla
* All rights reserved.
*
* This source code is proprietary and confidential.
* Unauthorized copying, modification, distribution, or use
* of this file, via any medium, is strictly prohibited.
*/
// app/utils/mailer.util.js
const nodemailer = require("nodemailer");
const fs = require("fs");
const path = require("path");
const mailConfig = require("../config/mail.config");
const transporter = nodemailer.createTransport({
host: mailConfig.host,
port: mailConfig.port,
secure: mailConfig.secure,
auth: mailConfig.auth,
});
transporter.verify((err) => {
if (err) console.error("Mail server connection failed", err);
else console.log("Mail server ready");
});
// Utility to load template and replace placeholders
const loadTemplate = (templateName, variables = {}) => {
const templatePath = path.join(__dirname, "../templates/emails", `${templateName}.html`);
let template = fs.readFileSync(templatePath, "utf-8");
Object.keys(variables).forEach((key) => {
const regex = new RegExp(`{{${key}}}`, "g");
template = template.replace(regex, variables[key]);
});
return template;
};
const send = async ({ to, subject, templateName, templateVars = {}, text }) => {
const html = templateName ? loadTemplate(templateName, templateVars) : undefined;
const mailOptions = {
from: mailConfig.from,
to,
subject,
text,
html,
};
try {
const info = await transporter.sendMail(mailOptions);
console.log("Email sent:", info.messageId);
return info;
} catch (err) {
console.error("Error sending email:", err);
throw err;
}
};
const sendMail = async (options) => {
const today = new Date();
// Full formatted date: "January 1, 2026"
const formattedDate = today.toLocaleDateString("en-US", {
year: "numeric",
month: "long",
day: "numeric",
});
// Only year: "2026"
const currentYear = today.getFullYear();
const templateVars = {
...options.templateVars, // keep existing vars
currentDate: formattedDate,
currentYear: currentYear,
};
return send({ ...options, templateVars });
};
module.exports = { sendMail };