dsql-admin/dsql-app/package-shared/functions/backend/handleNodemailer.js

130 lines
4.1 KiB
JavaScript
Raw Normal View History

2024-12-06 13:24:26 +00:00
// @ts-check
/**
* Imports
* ==============================================================================
*/
const fs = require("fs");
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
const nodemailer = require("nodemailer");
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
let transporter = nodemailer.createTransport({
host: process.env.DSQL_MAIL_HOST,
port: 465,
secure: true,
auth: {
user: process.env.DSQL_MAIL_EMAIL,
pass: process.env.DSQL_MAIL_PASSWORD,
},
});
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/**
* # Handle mails
* @param {object} mailObject - Mail Object with params
* @param {string} [mailObject.to] - who is recieving this email? Comma separated for multiple recipients
* @param {string} [mailObject.subject] - Mail Subject
* @param {string} [mailObject.text] - Mail text
* @param {string} [mailObject.html] - Mail HTML
* @param {string | null} [mailObject.alias] - Sender alias: "support" or null
*
* @returns {Promise<any>} mail object
*/
module.exports = async function handleNodemailer({
to,
subject,
text,
html,
alias,
}) {
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
if (
!process.env.DSQL_MAIL_HOST ||
!process.env.DSQL_MAIL_EMAIL ||
!process.env.DSQL_MAIL_PASSWORD
) {
return null;
}
const sender = (() => {
if (alias?.match(/support/i)) return process.env.DSQL_MAIL_EMAIL;
return process.env.DSQL_MAIL_EMAIL;
})();
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
let sentMessage;
if (!fs.existsSync("./email/index.html")) {
return;
}
let mailRoot = fs.readFileSync("./email/index.html", "utf8");
let finalHtml = mailRoot
.replace(/{{email_body}}/, html ? html : "")
.replace(/{{issue_date}}/, Date().substring(0, 24));
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
try {
let mailObject = {};
mailObject["from"] = `"Datasquirel" <${sender}>`;
mailObject["sender"] = sender;
if (alias) mailObject["replyTo "] = sender;
// mailObject["priority"] = "high";
mailObject["to"] = to;
mailObject["subject"] = subject;
mailObject["text"] = text;
mailObject["html"] = finalHtml;
// send mail with defined transport object
let info = await transporter.sendMail(mailObject);
sentMessage = info;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
} catch (/** @type {any} */ error) {
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
console.log("ERROR in handleNodemailer Function =>", error.message);
// serverError({
// component: "handleNodemailer",
// message: error.message,
// user: { email: to },
// });
}
return sentMessage;
};
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////