update
This commit is contained in:
@ -11,26 +11,21 @@ export class RuleManager {
|
||||
constructor(emailRefArg: EmailService) {
|
||||
this.emailRef = emailRefArg;
|
||||
|
||||
// Register MTA handler for incoming emails if MTA is enabled
|
||||
if (this.emailRef.mtaService) {
|
||||
this.setupMtaIncomingHandler();
|
||||
// Register handler for incoming emails if email server is enabled
|
||||
if (this.emailRef.unifiedEmailServer) {
|
||||
this.setupIncomingHandler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up handler for incoming emails via MTA's SMTP server
|
||||
* Set up handler for incoming emails via the UnifiedEmailServer
|
||||
*/
|
||||
private setupMtaIncomingHandler() {
|
||||
// The original MtaService doesn't have a direct callback for incoming emails,
|
||||
// but we can modify this approach based on how you prefer to integrate.
|
||||
// One option would be to extend the MtaService to add an event emitter.
|
||||
private setupIncomingHandler() {
|
||||
// Use UnifiedEmailServer events for incoming emails
|
||||
const incomingDir = './received';
|
||||
|
||||
// For now, we'll use a directory watcher as an example
|
||||
// This would watch the directory where MTA saves incoming emails
|
||||
const incomingDir = this.emailRef.mtaService['receivedEmailsDir'] || './received';
|
||||
|
||||
// Simple file watcher (in real implementation, use proper file watching)
|
||||
// This is just conceptual - would need modification to work with your specific setup
|
||||
// The UnifiedEmailServer raises events for incoming emails
|
||||
// For backward compatibility, also watch the directory
|
||||
this.watchIncomingEmails(incomingDir);
|
||||
}
|
||||
|
||||
@ -41,44 +36,72 @@ export class RuleManager {
|
||||
console.log(`Watching for incoming emails in: ${directory}`);
|
||||
|
||||
// Conceptual - in a real implementation, set up proper file watching
|
||||
// or modify the MTA to emit events when emails are received
|
||||
// or use UnifiedEmailServer events for incoming emails
|
||||
|
||||
/*
|
||||
// Example using a file watcher:
|
||||
const watcher = plugins.fs.watch(directory, async (eventType, filename) => {
|
||||
if (eventType === 'rename' && filename.endsWith('.eml')) {
|
||||
const filePath = plugins.path.join(directory, filename);
|
||||
await this.handleMtaIncomingEmail(filePath);
|
||||
await this.handleIncomingEmail(filePath);
|
||||
}
|
||||
});
|
||||
*/
|
||||
|
||||
// Set up event listener on UnifiedEmailServer if available
|
||||
if (this.emailRef.unifiedEmailServer) {
|
||||
this.emailRef.unifiedEmailServer.on('emailProcessed', (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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle incoming email received via MTA
|
||||
* Handle incoming email received via email server
|
||||
*/
|
||||
public async handleMtaIncomingEmail(emailPath: string) {
|
||||
public async handleIncomingEmail(emailPath: string) {
|
||||
try {
|
||||
// Process the email file
|
||||
const fetchedSmartmail = await this.emailRef.mtaConnector.receiveEmail(emailPath);
|
||||
// Process the email file using direct file access or access through UnifiedEmailServer
|
||||
// For compatibility with existing code, we'll make a basic assumption about structure
|
||||
const emailContent = await plugins.fs.promises.readFile(emailPath, 'utf8');
|
||||
// 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 || '',
|
||||
subject: parsedContent.subject || '',
|
||||
// Use a default from address if not present
|
||||
from: parsedContent.from?.text || 'unknown@example.com'
|
||||
});
|
||||
|
||||
console.log('=======================');
|
||||
console.log('Received a mail via MTA:');
|
||||
console.log(`From: ${fetchedSmartmail.options.creationObjectRef.From}`);
|
||||
console.log(`To: ${fetchedSmartmail.options.creationObjectRef.To}`);
|
||||
console.log(`Subject: ${fetchedSmartmail.options.creationObjectRef.Subject}`);
|
||||
console.log('Received a mail:');
|
||||
console.log(`From: ${fetchedSmartmail.options?.from || 'unknown'}`);
|
||||
console.log(`Subject: ${fetchedSmartmail.options?.subject || 'no subject'}`);
|
||||
console.log('^^^^^^^^^^^^^^^^^^^^^^^');
|
||||
|
||||
logger.log(
|
||||
'info',
|
||||
`email from ${fetchedSmartmail.options.creationObjectRef.From} to ${fetchedSmartmail.options.creationObjectRef.To} with subject '${fetchedSmartmail.options.creationObjectRef.Subject}'`,
|
||||
`email from ${fetchedSmartmail.options?.from || 'unknown'} with subject '${fetchedSmartmail.options?.subject || 'no subject'}'`,
|
||||
{
|
||||
eventType: 'receivedEmail',
|
||||
provider: 'mta',
|
||||
provider: 'unified',
|
||||
email: {
|
||||
from: fetchedSmartmail.options.creationObjectRef.From,
|
||||
to: fetchedSmartmail.options.creationObjectRef.To,
|
||||
subject: fetchedSmartmail.options.creationObjectRef.Subject,
|
||||
from: fetchedSmartmail.options?.from || 'unknown',
|
||||
subject: fetchedSmartmail.options?.subject || 'no subject',
|
||||
},
|
||||
}
|
||||
);
|
||||
@ -86,9 +109,9 @@ export class RuleManager {
|
||||
// Process with rules
|
||||
this.smartruleInstance.makeDecision(fetchedSmartmail);
|
||||
} catch (error) {
|
||||
logger.log('error', `Failed to process incoming MTA email: ${error.message}`, {
|
||||
logger.log('error', `Failed to process incoming email: ${error.message}`, {
|
||||
eventType: 'emailError',
|
||||
provider: 'mta',
|
||||
provider: 'unified',
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user