This commit is contained in:
2025-05-20 11:04:09 +00:00
parent 07f03eb834
commit f3f06ed06d
4 changed files with 418 additions and 54 deletions

View File

@ -0,0 +1,72 @@
import * as plugins from '../../plugins.js';
import * as paths from '../../paths.js';
import type { SzPlatformService } from '../../platformservice.js';
/**
* Extension to the MTA service to handle custom email storage
*/
export function configureEmailStorage(mtaService: any, options: any = {}) {
const originalSaveToLocalMailbox = mtaService.saveToLocalMailbox;
// Override the saveToLocalMailbox method to use custom path
mtaService.saveToLocalMailbox = async function(email: any): Promise<void> {
try {
// Use the custom received emails path if configured
const customPath = options?.receivedEmailsPath;
if (customPath) {
// Ensure the directory exists
plugins.smartfile.fs.ensureDirSync(customPath);
// Check if this is a bounce notification
const isBounceNotification = this.isBounceNotification(email);
if (isBounceNotification) {
await this.processBounceNotification(email);
return;
}
// Store the email in the custom path
const emailContent = email.toRFC822String();
const filename = `${Date.now()}_${email.to[0].replace(/[^a-zA-Z0-9]/g, '_')}.eml`;
plugins.smartfile.memory.toFsSync(
emailContent,
plugins.path.join(customPath, filename)
);
console.log(`Email saved to custom mailbox location: ${customPath}/${filename}`);
} else {
// Use the original implementation
await originalSaveToLocalMailbox.call(this, email);
}
} catch (error) {
console.error('Error saving email to local mailbox:', error);
throw error;
}
};
return mtaService;
}
/**
* Apply the email storage configuration to the platform service
*/
export function applyCustomEmailStorage(platformService: SzPlatformService, options: any): void {
// Extract the receivedEmailsPath if available
if (options?.emailPortConfig?.receivedEmailsPath) {
const receivedEmailsPath = options.emailPortConfig.receivedEmailsPath;
// Ensure the directory exists
plugins.smartfile.fs.ensureDirSync(receivedEmailsPath);
// Apply configuration to MTA service if available
if (platformService.mtaService) {
configureEmailStorage(platformService.mtaService, {
receivedEmailsPath
});
console.log(`Configured MTA to store received emails to: ${receivedEmailsPath}`);
}
}
}