From ace73fa919ab5d78f959fec8bc8e5f57c4df7f45 Mon Sep 17 00:00:00 2001 From: Blake Matthes Date: Mon, 16 Feb 2026 18:59:12 -0600 Subject: [PATCH] refactor(sendmail): updated the smtp per alpla needs --- app/src/pkg/utils/mail/sendMail.ts | 188 +++++++++--------- .../notifications/controller/sendMail.ts | 42 ++-- 2 files changed, 116 insertions(+), 114 deletions(-) diff --git a/app/src/pkg/utils/mail/sendMail.ts b/app/src/pkg/utils/mail/sendMail.ts index 971136f..026e91c 100644 --- a/app/src/pkg/utils/mail/sendMail.ts +++ b/app/src/pkg/utils/mail/sendMail.ts @@ -1,123 +1,125 @@ -import type { Address } from "nodemailer/lib/mailer/index.js"; 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 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 { fileURLToPath } from "url"; import { promisify } from "util"; -import hbs from "nodemailer-express-handlebars"; import { createLogger } from "../../logger/logger.js"; interface HandlebarsMailOptions extends Mail.Options { - template: string; - context: Record; + template: string; + context: Record; } interface EmailData { - email: string; - subject: string; - template: string; - context: Record; + email: string; + subject: string; + template: string; + context: Record; } export const sendEmail = async (data: EmailData): Promise => { - const log = createLogger({ module: "pkg", subModule: "sendMail" }); - let transporter: Transporter; - let fromEmail: string | Address; + const log = createLogger({ module: "pkg", subModule: "sendMail" }); + let transporter: Transporter; + let fromEmail: string | Address; - if ( - os.hostname().includes("OLP") && - process.env.EMAIL_USER && - process.env.EMAIL_PASSWORD - ) { - transporter = nodemailer.createTransport({ - service: "gmail", - auth: { - user: process.env.EMAIL_USER, - pass: process.env.EMAIL_PASSWORD, - }, - //debug: true, - }); + // if ( + // os.hostname().includes("OLP") && + // process.env.EMAIL_USER && + // process.env.EMAIL_PASSWORD + // ) { + // transporter = nodemailer.createTransport({ + // service: "gmail", + // auth: { + // user: process.env.EMAIL_USER, + // pass: process.env.EMAIL_PASSWORD, + // }, + // //debug: true, + // }); - // update the from email - fromEmail = process.env.EMAIL_USER; - } else { - // convert to the correct plant token. + // // update the from email + // fromEmail = process.env.EMAIL_USER; + // } else { + // // 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")) { - host = "USMCD1-smtp.alpla.net"; - } + // if (os.hostname().includes("VMS036")) { + // host = "USMCD1-smtp.alpla.net"; + // } - // if (plantToken[0].value === "usiow2") { - // host = "USIOW1-smtp.alpla.net"; - // } + // if (plantToken[0].value === "usiow2") { + // host = "USIOW1-smtp.alpla.net"; + // } - transporter = nodemailer.createTransport({ - host: host, - port: 25, - rejectUnauthorized: false, - //secure: false, - // auth: { - // user: "alplaprod", - // pass: "obelix", - // }, - debug: true, - } as SMTPTransport.Options); + transporter = nodemailer.createTransport({ + host: "smtp.azurecomm.net", + port: 587, + //rejectUnauthorized: false, + tls: { + minVersion: "TLSv1.2", + }, + auth: { + user: "donotreply@mail.alpla.com", + pass: process.env.SMTP_PASSWORD, + }, + debug: true, + } as SMTPTransport.Options); - // update the from email - fromEmail = `noreply@alpla.com`; - } + // update the from email + fromEmail = `DoNotReply@mail.alpla.com`; + //} - // creating the handlbar options - const viewPath = path.resolve( - path.dirname(fileURLToPath(import.meta.url)), - "./views/" - ); + // creating the handlbar options + const viewPath = path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + "./views/", + ); - const handlebarOptions = { - viewEngine: { - extname: ".hbs", - //layoutsDir: path.resolve(viewPath, "layouts"), // Path to layouts directory - defaultLayout: "", // Specify the default layout - partialsDir: viewPath, - }, - viewPath: viewPath, - extName: ".hbs", // File extension for Handlebars templates - }; + const handlebarOptions = { + viewEngine: { + extname: ".hbs", + //layoutsDir: path.resolve(viewPath, "layouts"), // Path to layouts directory + defaultLayout: "", // Specify the default layout + partialsDir: viewPath, + }, + viewPath: viewPath, + extName: ".hbs", // File extension for Handlebars templates + }; - transporter.use("compile", hbs(handlebarOptions)); + transporter.use("compile", hbs(handlebarOptions)); - const mailOptions: HandlebarsMailOptions = { - from: fromEmail, - to: data.email, - subject: data.subject, - //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"), - template: data.template, // Name of the Handlebars template (e.g., 'welcome.hbs') - context: data.context, - }; + const mailOptions: HandlebarsMailOptions = { + from: fromEmail, + to: data.email, + subject: data.subject, + //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"), + template: data.template, // Name of the Handlebars template (e.g., 'welcome.hbs') + context: data.context, + }; - // now verify and send the email - const sendMailPromise = promisify(transporter.sendMail).bind(transporter); + // now verify and send the email + const sendMailPromise = promisify(transporter.sendMail).bind(transporter); - try { - // Send email and await the result - const info = await sendMailPromise(mailOptions); - log.info(null, `Email was sent to: ${data.email}`); - return { success: true, message: "Email sent.", data: info }; - } catch (err) { - console.log(err); - log.error( - { error: err }, + try { + // Send email and await the result + const info = await sendMailPromise(mailOptions); + log.info(null, `Email was sent to: ${data.email}`); + return { success: true, message: "Email sent.", data: info }; + } catch (err) { + console.log(err); + log.error( + { error: err }, - `Error sending Email to : ${data.email}` - ); - return { success: false, message: "Error sending email.", error: err }; - } + `Error sending Email to : ${data.email}`, + ); + return { success: false, message: "Error sending email.", error: err }; + } }; diff --git a/lstV2/server/services/notifications/controller/sendMail.ts b/lstV2/server/services/notifications/controller/sendMail.ts index 898e870..6dde1b6 100644 --- a/lstV2/server/services/notifications/controller/sendMail.ts +++ b/lstV2/server/services/notifications/controller/sendMail.ts @@ -67,34 +67,34 @@ export const sendEmail = async (data: any): Promise => { fromEmail = process.env.EMAIL_USER; } else { // 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)) { - host = "USMCD1-smtp.alpla.net"; - } + // if (testServers.includes(plantToken[0].value)) { + // host = "USMCD1-smtp.alpla.net"; + // } - if (plantToken[0].value === "usiow2") { - host = "USIOW1-smtp.alpla.net"; - } + // if (plantToken[0].value === "usiow2") { + // host = "USIOW1-smtp.alpla.net"; + // } transporter = nodemailer.createTransport({ - host: host, - port: 25, - rejectUnauthorized: false, - //secure: false, - // auth: { - // user: "alplaprod", - // pass: "obelix", - // }, + host: "smtp.azurecomm.net", + port: 587, + //rejectUnauthorized: false, + tls: { + minVersion: "TLSv1.2", + }, + auth: { + user: "donotreply@mail.alpla.com", + pass: process.env.SMTP_PASSWORD, + }, debug: true, - } as SMTPTransport.Options); - - // update the from email - fromEmail = `donotreply@alpla.com`; + }); + fromEmail = `DoNotReply@mail.alpla.com`; } // creating the handlbar options