2025-04-20 11:29:53 -07:00
|
|
|
import { fileURLToPath } from "url";
|
2026-01-20 22:06:55 +00:00
|
|
|
import { EmailTransportConfig } from "@/types/index.js";
|
2026-03-04 18:16:31 +00:00
|
|
|
import { ISettingsService } from "@/service/system/settingsService.js";
|
|
|
|
|
import { ILogger } from "@/utils/logger.js";
|
|
|
|
|
import fs from "node:fs";
|
|
|
|
|
import path from "node:path";
|
|
|
|
|
import nodemailer from "nodemailer";
|
|
|
|
|
import mjml2html from "mjml";
|
2025-04-20 11:29:53 -07:00
|
|
|
|
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
|
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
|
|
|
|
|
|
const SERVICE_NAME = "EmailService";
|
2026-03-04 18:16:31 +00:00
|
|
|
type MjmlFn = typeof mjml2html;
|
|
|
|
|
type FileSystem = typeof fs;
|
|
|
|
|
type PathModule = typeof path;
|
|
|
|
|
type Mailer = typeof nodemailer;
|
|
|
|
|
type TemplateCompiler = (template: string) => (context: Record<string, unknown>) => string;
|
2025-04-20 11:29:53 -07:00
|
|
|
|
2026-03-04 17:32:50 +00:00
|
|
|
export interface IEmailService {
|
|
|
|
|
init(): void;
|
|
|
|
|
buildEmail(template: string, context: Record<string, unknown>): Promise<string | undefined>;
|
|
|
|
|
sendEmail(to: string, subject: string, html: string, transportConfig?: EmailTransportConfig): Promise<string | false | undefined>;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-05 14:46:28 -08:00
|
|
|
export class EmailService implements IEmailService {
|
2025-04-20 11:29:53 -07:00
|
|
|
static SERVICE_NAME = SERVICE_NAME;
|
2025-05-15 15:16:36 +02:00
|
|
|
|
2026-03-04 18:16:31 +00:00
|
|
|
private settingsService: ISettingsService;
|
|
|
|
|
private fs: FileSystem;
|
|
|
|
|
private path: PathModule;
|
|
|
|
|
private compile: TemplateCompiler;
|
|
|
|
|
private mjml2html: MjmlFn;
|
|
|
|
|
private nodemailer: Mailer;
|
|
|
|
|
private logger: ILogger;
|
|
|
|
|
private transporter: ReturnType<typeof import("nodemailer").createTransport> | null = null;
|
|
|
|
|
private templateLookup: Record<string, ((context: Record<string, unknown>) => string) | undefined>;
|
2026-01-20 22:06:55 +00:00
|
|
|
|
2026-03-04 18:16:31 +00:00
|
|
|
constructor(
|
|
|
|
|
settingsService: ISettingsService,
|
|
|
|
|
fs: FileSystem,
|
|
|
|
|
path: PathModule,
|
|
|
|
|
compile: TemplateCompiler,
|
|
|
|
|
mjml2html: MjmlFn,
|
|
|
|
|
nodemailer: Mailer,
|
|
|
|
|
logger: ILogger
|
|
|
|
|
) {
|
2025-04-20 11:29:53 -07:00
|
|
|
this.settingsService = settingsService;
|
|
|
|
|
this.fs = fs;
|
|
|
|
|
this.path = path;
|
|
|
|
|
this.compile = compile;
|
|
|
|
|
this.mjml2html = mjml2html;
|
|
|
|
|
this.nodemailer = nodemailer;
|
|
|
|
|
this.logger = logger;
|
2026-01-20 22:06:55 +00:00
|
|
|
this.templateLookup = {};
|
2026-02-06 22:14:42 +00:00
|
|
|
this.init();
|
2025-05-09 14:52:44 -07:00
|
|
|
}
|
2025-04-20 11:29:53 -07:00
|
|
|
|
2025-07-25 15:15:14 -07:00
|
|
|
get serviceName() {
|
|
|
|
|
return EmailService.SERVICE_NAME;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-06 22:14:42 +00:00
|
|
|
init = () => {
|
2026-04-09 09:49:12 -07:00
|
|
|
const loadTemplate = (templateName: string) => {
|
2025-04-20 11:29:53 -07:00
|
|
|
try {
|
2026-01-16 22:03:35 +00:00
|
|
|
const templatePath = this.path.join(__dirname, `../../templates/${templateName}.mjml`);
|
2025-04-20 11:29:53 -07:00
|
|
|
const templateContent = this.fs.readFileSync(templatePath, "utf8");
|
|
|
|
|
return this.compile(templateContent);
|
2026-03-04 18:16:31 +00:00
|
|
|
} catch (error: unknown) {
|
2025-04-20 11:29:53 -07:00
|
|
|
this.logger.error({
|
2026-03-04 18:16:31 +00:00
|
|
|
message: error instanceof Error ? error.message : "Unknown error",
|
2025-04-20 11:29:53 -07:00
|
|
|
service: SERVICE_NAME,
|
|
|
|
|
method: "loadTemplate",
|
2026-03-04 18:16:31 +00:00
|
|
|
stack: error instanceof Error ? error.stack : undefined,
|
2025-04-20 11:29:53 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.templateLookup = {
|
2026-04-09 09:49:12 -07:00
|
|
|
welcomeEmailTemplate: loadTemplate("welcomeEmail"),
|
|
|
|
|
employeeActivationTemplate: loadTemplate("employeeActivation"),
|
|
|
|
|
noIncidentsThisWeekTemplate: loadTemplate("noIncidentsThisWeek"),
|
|
|
|
|
passwordResetTemplate: loadTemplate("passwordReset"),
|
|
|
|
|
testEmailTemplate: loadTemplate("testEmailTemplate"),
|
|
|
|
|
unifiedNotificationTemplate: loadTemplate("unifiedNotification"),
|
2025-04-20 11:29:53 -07:00
|
|
|
};
|
2025-05-09 14:52:44 -07:00
|
|
|
};
|
2025-04-20 11:29:53 -07:00
|
|
|
|
2026-03-04 18:16:31 +00:00
|
|
|
buildEmail = async (template: string, context: Record<string, unknown>) => {
|
2025-06-18 10:44:59 +08:00
|
|
|
try {
|
2026-01-20 22:06:55 +00:00
|
|
|
const mjml = this.templateLookup[template]?.(context);
|
2026-03-04 18:16:31 +00:00
|
|
|
if (!mjml) {
|
|
|
|
|
throw new Error(`Template ${template} not found`);
|
|
|
|
|
}
|
2026-03-05 13:57:39 -08:00
|
|
|
const html = await this.mjml2html(mjml);
|
2025-06-18 10:44:59 +08:00
|
|
|
return html.html;
|
2026-03-04 18:16:31 +00:00
|
|
|
} catch (error: unknown) {
|
2025-06-18 10:44:59 +08:00
|
|
|
this.logger.error({
|
2026-03-04 18:16:31 +00:00
|
|
|
message: error instanceof Error ? error.message : "Unknown error",
|
2025-06-18 10:44:59 +08:00
|
|
|
service: SERVICE_NAME,
|
|
|
|
|
method: "buildEmail",
|
2026-03-04 18:16:31 +00:00
|
|
|
stack: error instanceof Error ? error.stack : undefined,
|
2025-06-18 10:44:59 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-20 22:06:55 +00:00
|
|
|
sendEmail = async (to: string, subject: string, html: string, transportConfig?: EmailTransportConfig) => {
|
|
|
|
|
let config: EmailTransportConfig;
|
2025-05-16 15:02:07 -07:00
|
|
|
if (typeof transportConfig !== "undefined") {
|
|
|
|
|
config = transportConfig;
|
|
|
|
|
} else {
|
2025-05-23 12:22:05 -07:00
|
|
|
config = await this.settingsService.getDBSettings();
|
2025-05-16 15:02:07 -07:00
|
|
|
}
|
2025-04-20 11:29:53 -07:00
|
|
|
const {
|
|
|
|
|
systemEmailHost,
|
|
|
|
|
systemEmailPort,
|
2025-06-12 00:39:47 +05:30
|
|
|
systemEmailSecure,
|
|
|
|
|
systemEmailPool,
|
2025-04-20 11:29:53 -07:00
|
|
|
systemEmailUser,
|
|
|
|
|
systemEmailAddress,
|
2026-04-29 15:30:16 -04:00
|
|
|
systemEmailDisplayName,
|
2025-04-20 11:29:53 -07:00
|
|
|
systemEmailPassword,
|
2025-05-15 15:16:36 +02:00
|
|
|
systemEmailConnectionHost,
|
2025-06-11 15:11:28 +05:30
|
|
|
systemEmailTLSServername,
|
|
|
|
|
systemEmailIgnoreTLS,
|
|
|
|
|
systemEmailRequireTLS,
|
|
|
|
|
systemEmailRejectUnauthorized,
|
2025-05-16 15:02:07 -07:00
|
|
|
} = config;
|
2025-04-20 11:29:53 -07:00
|
|
|
|
2025-06-12 00:39:47 +05:30
|
|
|
const emailConfig = {
|
2025-04-20 11:29:53 -07:00
|
|
|
host: systemEmailHost,
|
2025-06-11 14:11:49 +05:30
|
|
|
port: Number(systemEmailPort),
|
2025-06-12 00:39:47 +05:30
|
|
|
secure: systemEmailSecure,
|
2025-04-20 11:29:53 -07:00
|
|
|
auth: {
|
|
|
|
|
user: systemEmailUser || systemEmailAddress,
|
|
|
|
|
pass: systemEmailPassword,
|
|
|
|
|
},
|
2025-06-13 12:22:28 +05:30
|
|
|
name: systemEmailConnectionHost || "localhost",
|
2025-05-09 14:52:44 -07:00
|
|
|
connectionTimeout: 5000,
|
2025-06-12 00:39:47 +05:30
|
|
|
pool: systemEmailPool,
|
2025-06-13 12:22:28 +05:30
|
|
|
tls: {
|
|
|
|
|
rejectUnauthorized: systemEmailRejectUnauthorized,
|
|
|
|
|
ignoreTLS: systemEmailIgnoreTLS,
|
|
|
|
|
requireTLS: systemEmailRequireTLS,
|
|
|
|
|
servername: systemEmailTLSServername,
|
|
|
|
|
},
|
2025-04-20 11:29:53 -07:00
|
|
|
};
|
|
|
|
|
this.transporter = this.nodemailer.createTransport(emailConfig);
|
|
|
|
|
|
2025-06-25 09:35:41 +08:00
|
|
|
try {
|
|
|
|
|
await this.transporter.verify();
|
2026-03-04 18:16:31 +00:00
|
|
|
} catch (error: unknown) {
|
2025-06-25 09:35:41 +08:00
|
|
|
this.logger.warn({
|
|
|
|
|
message: "Email transporter verification failed",
|
|
|
|
|
service: SERVICE_NAME,
|
|
|
|
|
method: "verifyTransporter",
|
2026-03-04 18:16:31 +00:00
|
|
|
stack: error instanceof Error ? error.stack : undefined,
|
2025-06-25 09:35:41 +08:00
|
|
|
});
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-29 15:42:02 -04:00
|
|
|
const trimmedDisplayName = systemEmailDisplayName?.trim();
|
|
|
|
|
const from = trimmedDisplayName && systemEmailAddress ? { name: trimmedDisplayName, address: systemEmailAddress } : systemEmailAddress;
|
|
|
|
|
|
2025-06-18 10:44:59 +08:00
|
|
|
try {
|
|
|
|
|
const info = await this.transporter.sendMail({
|
|
|
|
|
to: to,
|
2026-04-29 15:42:02 -04:00
|
|
|
from: from,
|
2025-06-18 10:44:59 +08:00
|
|
|
subject: subject,
|
|
|
|
|
html: html,
|
|
|
|
|
});
|
2026-04-09 09:49:12 -07:00
|
|
|
return info.messageId;
|
2026-03-04 18:16:31 +00:00
|
|
|
} catch (error: unknown) {
|
2025-06-18 10:44:59 +08:00
|
|
|
this.logger.error({
|
2026-03-04 18:16:31 +00:00
|
|
|
message: error instanceof Error ? error.message : "Unknown error",
|
2025-06-18 10:44:59 +08:00
|
|
|
service: SERVICE_NAME,
|
|
|
|
|
method: "sendEmail",
|
2026-03-04 18:16:31 +00:00
|
|
|
stack: error instanceof Error ? error.stack : undefined,
|
2025-06-18 10:44:59 +08:00
|
|
|
});
|
|
|
|
|
}
|
2025-04-20 11:29:53 -07:00
|
|
|
};
|
|
|
|
|
}
|