2024-02-16 13:28:40 +01:00
|
|
|
import * as plugins from '../plugins.js';
|
|
|
|
import * as paths from '../paths.js';
|
2025-03-15 16:04:03 +00:00
|
|
|
import { MtaConnector } from './email.classes.connector.mta.js';
|
2024-02-15 20:30:38 +01:00
|
|
|
import { RuleManager } from './email.classes.rulemanager.js';
|
|
|
|
import { ApiManager } from './email.classes.apimanager.js';
|
2024-02-16 13:28:40 +01:00
|
|
|
import { logger } from '../logger.js';
|
2024-02-15 20:30:38 +01:00
|
|
|
import type { SzPlatformService } from '../classes.platformservice.js';
|
|
|
|
|
2025-03-15 16:04:03 +00:00
|
|
|
// Import MTA service
|
|
|
|
import { MtaService, type IMtaConfig } from '../mta/index.js';
|
2024-02-16 20:42:26 +01:00
|
|
|
|
|
|
|
export interface IEmailConstructorOptions {
|
2025-03-15 16:04:03 +00:00
|
|
|
useMta?: boolean;
|
|
|
|
mtaConfig?: IMtaConfig;
|
2024-02-16 20:42:26 +01:00
|
|
|
}
|
|
|
|
|
2025-03-15 16:04:03 +00:00
|
|
|
/**
|
|
|
|
* Email service with support for both Mailgun and local MTA
|
|
|
|
*/
|
2024-02-16 13:28:40 +01:00
|
|
|
export class EmailService {
|
2024-02-15 20:30:38 +01:00
|
|
|
public platformServiceRef: SzPlatformService;
|
|
|
|
|
|
|
|
// typedrouter
|
2024-02-16 13:28:40 +01:00
|
|
|
public typedrouter = new plugins.typedrequest.TypedRouter();
|
2024-02-15 20:30:38 +01:00
|
|
|
|
|
|
|
// connectors
|
2025-03-15 16:04:03 +00:00
|
|
|
public mtaConnector: MtaConnector;
|
2024-02-15 20:30:38 +01:00
|
|
|
public qenv = new plugins.qenv.Qenv('./', '.nogit/');
|
|
|
|
|
2025-03-15 16:04:03 +00:00
|
|
|
// MTA service
|
|
|
|
public mtaService: MtaService;
|
|
|
|
|
|
|
|
// services
|
|
|
|
public apiManager: ApiManager;
|
2024-02-15 20:30:38 +01:00
|
|
|
public ruleManager: RuleManager;
|
|
|
|
|
2025-03-15 16:04:03 +00:00
|
|
|
// configuration
|
|
|
|
private config: IEmailConstructorOptions;
|
|
|
|
|
|
|
|
constructor(platformServiceRefArg: SzPlatformService, options: IEmailConstructorOptions = {}) {
|
2024-02-15 20:30:38 +01:00
|
|
|
this.platformServiceRef = platformServiceRefArg;
|
2024-02-16 13:28:40 +01:00
|
|
|
this.platformServiceRef.typedrouter.addTypedRouter(this.typedrouter);
|
2025-03-15 16:04:03 +00:00
|
|
|
|
|
|
|
// Set default options
|
|
|
|
this.config = {
|
|
|
|
useMta: options.useMta ?? true,
|
|
|
|
mtaConfig: options.mtaConfig || {}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.config.useMta) {
|
|
|
|
// Initialize MTA service
|
|
|
|
this.mtaService = new MtaService(platformServiceRefArg, this.config.mtaConfig);
|
|
|
|
// Initialize MTA connector
|
|
|
|
this.mtaConnector = new MtaConnector(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize API manager and rule manager
|
|
|
|
this.apiManager = new ApiManager(this);
|
2024-02-15 20:30:38 +01:00
|
|
|
this.ruleManager = new RuleManager(this);
|
2025-03-15 16:04:03 +00:00
|
|
|
|
|
|
|
// Set up MTA SMTP server webhook if using MTA
|
|
|
|
if (this.config.useMta) {
|
|
|
|
// The MTA SMTP server will handle incoming emails directly
|
|
|
|
// through its SMTP protocol. No additional webhook needed.
|
|
|
|
}
|
2024-02-15 20:30:38 +01:00
|
|
|
}
|
|
|
|
|
2025-03-15 16:04:03 +00:00
|
|
|
/**
|
|
|
|
* Start the email service
|
|
|
|
*/
|
2024-02-15 20:30:38 +01:00
|
|
|
public async start() {
|
2025-03-15 16:04:03 +00:00
|
|
|
// Initialize rule manager
|
2024-02-15 20:30:38 +01:00
|
|
|
await this.ruleManager.init();
|
2025-03-15 16:04:03 +00:00
|
|
|
|
|
|
|
// Start MTA service if enabled
|
|
|
|
if (this.config.useMta && this.mtaService) {
|
|
|
|
await this.mtaService.start();
|
|
|
|
logger.log('success', 'Started MTA service');
|
|
|
|
}
|
|
|
|
|
2024-02-15 20:30:38 +01:00
|
|
|
logger.log('success', `Started email service`);
|
|
|
|
}
|
|
|
|
|
2025-03-15 16:04:03 +00:00
|
|
|
/**
|
|
|
|
* Stop the email service
|
|
|
|
*/
|
2024-02-15 20:30:38 +01:00
|
|
|
public async stop() {
|
2025-03-15 16:04:03 +00:00
|
|
|
// Stop MTA service if it's running
|
|
|
|
if (this.config.useMta && this.mtaService) {
|
|
|
|
await this.mtaService.stop();
|
|
|
|
logger.log('info', 'Stopped MTA service');
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.log('info', 'Stopped email service');
|
2024-02-15 20:30:38 +01:00
|
|
|
}
|
|
|
|
|
2025-03-15 16:04:03 +00:00
|
|
|
/**
|
|
|
|
* Send an email using the configured provider (Mailgun or MTA)
|
|
|
|
* @param email The email to send
|
|
|
|
* @param to Recipient(s)
|
|
|
|
* @param options Additional options
|
|
|
|
*/
|
|
|
|
public async sendEmail(
|
|
|
|
email: plugins.smartmail.Smartmail<>,
|
|
|
|
to: string | string[],
|
|
|
|
options: any = {}
|
|
|
|
): Promise<string> {
|
|
|
|
// Determine which connector to use
|
|
|
|
if (this.config.useMta && this.mtaConnector) {
|
|
|
|
return this.mtaConnector.sendEmail(email, to, options);
|
|
|
|
} else {
|
|
|
|
throw new Error('No email provider configured');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get email service statistics
|
|
|
|
*/
|
|
|
|
public getStats() {
|
|
|
|
const stats: any = {
|
|
|
|
activeProviders: []
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.config.useMta) {
|
|
|
|
stats.activeProviders.push('mta');
|
|
|
|
stats.mta = this.mtaService.getStats();
|
|
|
|
}
|
|
|
|
|
|
|
|
return stats;
|
|
|
|
}
|
|
|
|
}
|