feat(structure): Use unified Email class

This commit is contained in:
2025-05-27 15:38:34 +00:00
parent cfea44742a
commit 243a45d24c
11 changed files with 546 additions and 143 deletions

View File

@@ -269,44 +269,31 @@ export class EmailService {
/**
* Send an email using the UnifiedEmailServer
* @param email The email to send
* @param to Recipient(s)
* @param to Recipient(s) - if provided, overrides the email's 'to' field
* @param options Additional options
*/
public async sendEmail(
email: plugins.smartmail.Smartmail<any>,
to: string | string[],
email: Email,
to?: string | string[],
options: ISendEmailOptions = {}
): Promise<string> {
if (this.config.useEmail && this.unifiedEmailServer) {
// Convert Smartmail to Email format
const recipients = Array.isArray(to) ? to : [to];
// Access Smartmail properties using any type to bypass TypeScript checking
const emailAny = email as any;
const emailObj = new Email({
from: emailAny.from,
to: recipients,
subject: emailAny.subject,
text: emailAny.body || emailAny.text,
html: emailAny.htmlBody || emailAny.html,
attachments: emailAny.attachments ? emailAny.attachments.map((att: any) => ({
filename: att.filename,
content: att.contents || att.content,
contentType: att.contentType
})) : []
});
// If 'to' is provided, update the email's recipients
if (to) {
const recipients = Array.isArray(to) ? to : [to];
email.to = recipients;
}
// Determine the domain for routing
let matchedRule;
const recipientDomain = recipients[0].split('@')[1];
const recipientDomain = email.to[0].split('@')[1];
if (recipientDomain && this.domainRouter) {
matchedRule = this.domainRouter.matchRule(recipients[0]);
matchedRule = this.domainRouter.matchRule(email.to[0]);
}
// Send through UnifiedEmailServer
return this.unifiedEmailServer.sendEmail(
emailObj,
email,
matchedRule?.mode || 'mta',
matchedRule
);
@@ -330,10 +317,10 @@ export class EmailService {
): Promise<string> {
try {
// Get email from template
const smartmail = await this.templateManager.prepareEmail(templateId, context);
const email = await this.templateManager.prepareEmail(templateId, context);
// Send the email through UnifiedEmailServer
return this.sendEmail(smartmail, to, options);
return this.sendEmail(email, to, options);
} catch (error) {
logger.log('error', `Failed to send template email: ${error.message}`, {
templateId,