This commit is contained in:
2025-05-07 20:20:17 +00:00
parent f6377d1973
commit 630e911589
40 changed files with 8745 additions and 333 deletions

View File

@@ -31,15 +31,42 @@ export class MtaConnector {
toAddresses: string | string[],
options: any = {}
): Promise<string> {
// Check if recipients are on the suppression list
const recipients = Array.isArray(toAddresses)
? toAddresses
: toAddresses.split(',').map(addr => addr.trim());
// Filter out suppressed recipients
const validRecipients = [];
const suppressedRecipients = [];
for (const recipient of recipients) {
if (this.emailRef.bounceManager.isEmailSuppressed(recipient)) {
suppressedRecipients.push(recipient);
} else {
validRecipients.push(recipient);
}
}
// Log suppressed recipients
if (suppressedRecipients.length > 0) {
logger.log('warn', `Skipping ${suppressedRecipients.length} suppressed recipients`, {
suppressedRecipients
});
}
// If all recipients are suppressed, throw error
if (validRecipients.length === 0) {
throw new Error('All recipients are on the suppression list');
}
// Continue with valid recipients
try {
// Process recipients
const toArray = Array.isArray(toAddresses)
? toAddresses
: toAddresses.split(',').map(addr => addr.trim());
// Use filtered recipients - already an array, no need for toArray
// Add recipients to smartmail if they're not already added
if (!smartmail.options.to || smartmail.options.to.length === 0) {
for (const recipient of toArray) {
for (const recipient of validRecipients) {
smartmail.addRecipient(recipient);
}
}
@@ -57,15 +84,15 @@ export class MtaConnector {
const mimeEmail = await smartmail.toMimeFormat(smartmail.options.creationObjectRef);
// Parse the MIME email to create an MTA Email
return this.sendMimeEmail(mimeEmail, toArray);
return this.sendMimeEmail(mimeEmail, validRecipients);
} catch (mimeError) {
logger.log('warn', `Failed to use MIME format, falling back to direct conversion: ${mimeError.message}`);
// Fall back to direct conversion
return this.sendDirectEmail(smartmail, toArray);
return this.sendDirectEmail(smartmail, validRecipients);
}
} else {
// Use direct conversion
return this.sendDirectEmail(smartmail, toArray);
return this.sendDirectEmail(smartmail, validRecipients);
}
} catch (error) {
logger.log('error', `Failed to send email via MTA: ${error.message}`, {
@@ -73,6 +100,30 @@ export class MtaConnector {
provider: 'mta',
error: error.message
});
// Check if this is a bounce-related error
if (error.message.includes('550') || // Rejected
error.message.includes('551') || // User not local
error.message.includes('552') || // Mailbox full
error.message.includes('553') || // Bad mailbox name
error.message.includes('554') || // Transaction failed
error.message.includes('does not exist') ||
error.message.includes('unknown user') ||
error.message.includes('invalid recipient')) {
// Process as a bounce
for (const recipient of validRecipients) {
await this.emailRef.bounceManager.processSmtpFailure(
recipient,
error.message,
{
sender: smartmail.options.from,
statusCode: error.message.match(/\b([45]\d{2})\b/) ? error.message.match(/\b([45]\d{2})\b/)[1] : undefined
}
);
}
}
throw error;
}
}