refactor(sendmail): updated the smtp per alpla needs

This commit is contained in:
2026-02-16 18:59:12 -06:00
parent 316af4233f
commit ace73fa919
2 changed files with 116 additions and 114 deletions

View File

@@ -1,123 +1,125 @@
import type { Address } from "nodemailer/lib/mailer/index.js";
import type { Transporter } from "nodemailer"; import type { Transporter } from "nodemailer";
import type SMTPTransport from "nodemailer/lib/smtp-transport/index.js";
import type Mail from "nodemailer/lib/mailer/index.js";
import os from "os";
import nodemailer from "nodemailer"; import nodemailer from "nodemailer";
import type Mail from "nodemailer/lib/mailer/index.js";
import type { Address } from "nodemailer/lib/mailer/index.js";
import type SMTPTransport from "nodemailer/lib/smtp-transport/index.js";
import hbs from "nodemailer-express-handlebars";
import os from "os";
import path from "path"; import path from "path";
import { fileURLToPath } from "url"; import { fileURLToPath } from "url";
import { promisify } from "util"; import { promisify } from "util";
import hbs from "nodemailer-express-handlebars";
import { createLogger } from "../../logger/logger.js"; import { createLogger } from "../../logger/logger.js";
interface HandlebarsMailOptions extends Mail.Options { interface HandlebarsMailOptions extends Mail.Options {
template: string; template: string;
context: Record<string, unknown>; context: Record<string, unknown>;
} }
interface EmailData { interface EmailData {
email: string; email: string;
subject: string; subject: string;
template: string; template: string;
context: Record<string, unknown>; context: Record<string, unknown>;
} }
export const sendEmail = async (data: EmailData): Promise<any> => { export const sendEmail = async (data: EmailData): Promise<any> => {
const log = createLogger({ module: "pkg", subModule: "sendMail" }); const log = createLogger({ module: "pkg", subModule: "sendMail" });
let transporter: Transporter; let transporter: Transporter;
let fromEmail: string | Address; let fromEmail: string | Address;
if ( // if (
os.hostname().includes("OLP") && // os.hostname().includes("OLP") &&
process.env.EMAIL_USER && // process.env.EMAIL_USER &&
process.env.EMAIL_PASSWORD // process.env.EMAIL_PASSWORD
) { // ) {
transporter = nodemailer.createTransport({ // transporter = nodemailer.createTransport({
service: "gmail", // service: "gmail",
auth: { // auth: {
user: process.env.EMAIL_USER, // user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASSWORD, // pass: process.env.EMAIL_PASSWORD,
}, // },
//debug: true, // //debug: true,
}); // });
// update the from email // // update the from email
fromEmail = process.env.EMAIL_USER; // fromEmail = process.env.EMAIL_USER;
} else { // } else {
// convert to the correct plant token. // // convert to the correct plant token.
let host = `${os.hostname().replace("VMS006", "")}-smtp.alpla.net`; //let host = `${os.hostname().replace("VMS006", "")}-smtp.alpla.net`;
//const testServers = ["vms036", "VMS036"]; //const testServers = ["vms036", "VMS036"];
if (os.hostname().includes("VMS036")) { // if (os.hostname().includes("VMS036")) {
host = "USMCD1-smtp.alpla.net"; // host = "USMCD1-smtp.alpla.net";
} // }
// if (plantToken[0].value === "usiow2") { // if (plantToken[0].value === "usiow2") {
// host = "USIOW1-smtp.alpla.net"; // host = "USIOW1-smtp.alpla.net";
// } // }
transporter = nodemailer.createTransport({ transporter = nodemailer.createTransport({
host: host, host: "smtp.azurecomm.net",
port: 25, port: 587,
rejectUnauthorized: false, //rejectUnauthorized: false,
//secure: false, tls: {
// auth: { minVersion: "TLSv1.2",
// user: "alplaprod", },
// pass: "obelix", auth: {
// }, user: "donotreply@mail.alpla.com",
debug: true, pass: process.env.SMTP_PASSWORD,
} as SMTPTransport.Options); },
debug: true,
} as SMTPTransport.Options);
// update the from email // update the from email
fromEmail = `noreply@alpla.com`; fromEmail = `DoNotReply@mail.alpla.com`;
} //}
// creating the handlbar options // creating the handlbar options
const viewPath = path.resolve( const viewPath = path.resolve(
path.dirname(fileURLToPath(import.meta.url)), path.dirname(fileURLToPath(import.meta.url)),
"./views/" "./views/",
); );
const handlebarOptions = { const handlebarOptions = {
viewEngine: { viewEngine: {
extname: ".hbs", extname: ".hbs",
//layoutsDir: path.resolve(viewPath, "layouts"), // Path to layouts directory //layoutsDir: path.resolve(viewPath, "layouts"), // Path to layouts directory
defaultLayout: "", // Specify the default layout defaultLayout: "", // Specify the default layout
partialsDir: viewPath, partialsDir: viewPath,
}, },
viewPath: viewPath, viewPath: viewPath,
extName: ".hbs", // File extension for Handlebars templates extName: ".hbs", // File extension for Handlebars templates
}; };
transporter.use("compile", hbs(handlebarOptions)); transporter.use("compile", hbs(handlebarOptions));
const mailOptions: HandlebarsMailOptions = { const mailOptions: HandlebarsMailOptions = {
from: fromEmail, from: fromEmail,
to: data.email, to: data.email,
subject: data.subject, subject: data.subject,
//text: "You will have a reset token here and only have 30min to click the link before it expires.", //text: "You will have a reset token here and only have 30min to click the link before it expires.",
//html: emailTemplate("BlakesTest", "This is an example with css"), //html: emailTemplate("BlakesTest", "This is an example with css"),
template: data.template, // Name of the Handlebars template (e.g., 'welcome.hbs') template: data.template, // Name of the Handlebars template (e.g., 'welcome.hbs')
context: data.context, context: data.context,
}; };
// now verify and send the email // now verify and send the email
const sendMailPromise = promisify(transporter.sendMail).bind(transporter); const sendMailPromise = promisify(transporter.sendMail).bind(transporter);
try { try {
// Send email and await the result // Send email and await the result
const info = await sendMailPromise(mailOptions); const info = await sendMailPromise(mailOptions);
log.info(null, `Email was sent to: ${data.email}`); log.info(null, `Email was sent to: ${data.email}`);
return { success: true, message: "Email sent.", data: info }; return { success: true, message: "Email sent.", data: info };
} catch (err) { } catch (err) {
console.log(err); console.log(err);
log.error( log.error(
{ error: err }, { error: err },
`Error sending Email to : ${data.email}` `Error sending Email to : ${data.email}`,
); );
return { success: false, message: "Error sending email.", error: err }; return { success: false, message: "Error sending email.", error: err };
} }
}; };

View File

@@ -67,34 +67,34 @@ export const sendEmail = async (data: any): Promise<any> => {
fromEmail = process.env.EMAIL_USER; fromEmail = process.env.EMAIL_USER;
} else { } else {
// convert to the correct plant token. // convert to the correct plant token.
const plantToken = settingData.filter((s) => s.name === "plantToken"); //const plantToken = settingData.filter((s) => s.name === "plantToken");
let host = `${plantToken[0].value}-smtp.alpla.net`; // let host = `${plantToken[0].value}-smtp.alpla.net`;
const testServers = ["test1", "test2", "test3"]; // const testServers = ["test1", "test2", "test3"];
if (testServers.includes(plantToken[0].value)) { // if (testServers.includes(plantToken[0].value)) {
host = "USMCD1-smtp.alpla.net"; // host = "USMCD1-smtp.alpla.net";
} // }
if (plantToken[0].value === "usiow2") { // if (plantToken[0].value === "usiow2") {
host = "USIOW1-smtp.alpla.net"; // host = "USIOW1-smtp.alpla.net";
} // }
transporter = nodemailer.createTransport({ transporter = nodemailer.createTransport({
host: host, host: "smtp.azurecomm.net",
port: 25, port: 587,
rejectUnauthorized: false, //rejectUnauthorized: false,
//secure: false, tls: {
// auth: { minVersion: "TLSv1.2",
// user: "alplaprod", },
// pass: "obelix", auth: {
// }, user: "donotreply@mail.alpla.com",
pass: process.env.SMTP_PASSWORD,
},
debug: true, debug: true,
} as SMTPTransport.Options); });
fromEmail = `DoNotReply@mail.alpla.com`;
// update the from email
fromEmail = `donotreply@alpla.com`;
} }
// creating the handlbar options // creating the handlbar options