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

@@ -1,12 +1,11 @@
import * as plugins from '../../plugins.js';
import { EmailService } from '../services/classes.emailservice.js';
import { logger } from '../../logger.js';
import { Email, type IEmailOptions } from './classes.email.js';
export class RuleManager {
public emailRef: EmailService;
public smartruleInstance = new plugins.smartrule.SmartRule<
plugins.smartmail.Smartmail<any>
>();
public smartruleInstance = new plugins.smartrule.SmartRule<Email>();
constructor(emailRefArg: EmailService) {
this.emailRef = emailRefArg;
@@ -50,19 +49,9 @@ export class RuleManager {
// Set up event listener on UnifiedEmailServer if available
if (this.emailRef.unifiedEmailServer) {
this.emailRef.unifiedEmailServer.on('emailProcessed', (email, mode, rule) => {
this.emailRef.unifiedEmailServer.on('emailProcessed', (email: Email, mode, rule) => {
// Process email through rule system
// Convert Email to Smartmail format
// Convert Email object to Smartmail format
const smartmail = new plugins.smartmail.Smartmail({
// Use standard fields
from: email.from,
subject: email.subject || '',
body: email.text || email.html || ''
});
// Process with rules
this.smartruleInstance.makeDecision(smartmail);
this.smartruleInstance.makeDecision(email);
});
}
}
@@ -78,36 +67,44 @@ export class RuleManager {
// Parse the email content into proper format
const parsedContent = await plugins.mailparser.simpleParser(emailContent);
// Create a Smartmail object with the parsed content
const fetchedSmartmail = new plugins.smartmail.Smartmail({
// Use standardized fields that are always available
body: parsedContent.text || parsedContent.html || '',
// Create an Email object with the parsed content
const fromAddress = Array.isArray(parsedContent.from)
? parsedContent.from[0]?.text || 'unknown@example.com'
: parsedContent.from?.text || 'unknown@example.com';
const toAddress = Array.isArray(parsedContent.to)
? parsedContent.to[0]?.text || 'unknown@example.com'
: parsedContent.to?.text || 'unknown@example.com';
const fetchedEmail = new Email({
from: fromAddress,
to: toAddress,
subject: parsedContent.subject || '',
// Use a default from address if not present
from: parsedContent.from?.text || 'unknown@example.com'
text: parsedContent.text || '',
html: parsedContent.html || undefined
});
console.log('=======================');
console.log('Received a mail:');
console.log(`From: ${fetchedSmartmail.options?.from || 'unknown'}`);
console.log(`Subject: ${fetchedSmartmail.options?.subject || 'no subject'}`);
console.log(`From: ${fetchedEmail.from}`);
console.log(`Subject: ${fetchedEmail.subject}`);
console.log('^^^^^^^^^^^^^^^^^^^^^^^');
logger.log(
'info',
`email from ${fetchedSmartmail.options?.from || 'unknown'} with subject '${fetchedSmartmail.options?.subject || 'no subject'}'`,
`email from ${fetchedEmail.from} with subject '${fetchedEmail.subject}'`,
{
eventType: 'receivedEmail',
provider: 'unified',
email: {
from: fetchedSmartmail.options?.from || 'unknown',
subject: fetchedSmartmail.options?.subject || 'no subject',
from: fetchedEmail.from,
subject: fetchedEmail.subject,
},
}
);
// Process with rules
this.smartruleInstance.makeDecision(fetchedSmartmail);
this.smartruleInstance.makeDecision(fetchedEmail);
} catch (error) {
logger.log('error', `Failed to process incoming email: ${error.message}`, {
eventType: 'emailError',
@@ -135,9 +132,9 @@ export class RuleManager {
for (const forward of forwards) {
this.smartruleInstance.createRule(
10,
async (smartmailArg) => {
async (emailArg: Email) => {
const matched = forward.originalToAddress.reduce<boolean>((prevValue, currentValue) => {
return smartmailArg.options.creationObjectRef.To.includes(currentValue) || prevValue;
return emailArg.to.some(to => to.includes(currentValue)) || prevValue;
}, false);
if (matched) {
console.log('Forward rule matched');
@@ -147,48 +144,46 @@ export class RuleManager {
return 'continue';
}
},
async (smartmailArg: plugins.smartmail.Smartmail<any>) => {
async (emailArg: Email) => {
forward.forwardedToAddress.map(async (toArg) => {
const forwardedSmartMail = new plugins.smartmail.Smartmail({
body:
const forwardedEmail = new Email({
from: 'forwarder@mail.lossless.one',
to: toArg,
subject: `Forwarded mail for '${emailArg.to.join(', ')}'`,
html:
`
<div style="background: #CCC; padding: 10px; border-radius: 3px;">
<div><b>Original Sender:</b></div>
<div>${smartmailArg.options.creationObjectRef.From}</div>
<div>${emailArg.from}</div>
<div><b>Original Recipient:</b></div>
<div>${smartmailArg.options.creationObjectRef.To}</div>
<div>${emailArg.to.join(', ')}</div>
<div><b>Forwarded to:</b></div>
<div>${forward.forwardedToAddress.reduce<string>((pVal, cVal) => {
return `${pVal ? pVal + ', ' : ''}${cVal}`;
}, null)}</div>
<div><b>Subject:</b></div>
<div>${smartmailArg.getSubject()}</div>
<div>${emailArg.getSubject()}</div>
<div><b>The original body can be found below.</b></div>
</div>
` + smartmailArg.getBody(),
from: 'forwarder@mail.lossless.one',
subject: `Forwarded mail for '${smartmailArg.options.creationObjectRef.To}'`,
` + emailArg.getBody(true),
text: `Forwarded mail from ${emailArg.from} to ${emailArg.to.join(', ')}\n\n${emailArg.getBody()}`,
attachments: emailArg.attachments
});
for (const attachment of smartmailArg.attachments) {
forwardedSmartMail.addAttachment(attachment);
}
// Use the EmailService's sendEmail method to send with the appropriate provider
await this.emailRef.sendEmail(forwardedSmartMail, toArg);
await this.emailRef.sendEmail(forwardedEmail);
console.log(`forwarded mail to ${toArg}`);
logger.log(
'info',
`email from ${
smartmailArg.options.creationObjectRef.From
} to ${toArg} with subject '${smartmailArg.getSubject()}'`,
`email from ${emailArg.from} to ${toArg} with subject '${emailArg.getSubject()}'`,
{
eventType: 'forwardedEmail',
email: {
from: smartmailArg.options.creationObjectRef.From,
to: smartmailArg.options.creationObjectRef.To,
from: emailArg.from,
to: emailArg.to.join(', '),
forwardedTo: toArg,
subject: smartmailArg.options.creationObjectRef.Subject,
subject: emailArg.subject,
},
}
);