feat(email): Enhance email integration by updating @push.rocks/smartmail to ^2.1.0 and improving the entire email stack including validation, DKIM verification, templating, MIME conversion, and attachment handling.

This commit is contained in:
2025-05-07 17:41:04 +00:00
parent 2ee66ef967
commit c852e954c9
17 changed files with 2566 additions and 625 deletions

View File

@ -372,8 +372,8 @@ export class MtaService {
// Generate a unique ID for this email
const id = plugins.uuid.v4();
// Validate email
this.validateEmail(email);
// Validate email (now async)
await this.validateEmail(email);
// Create DKIM keys if needed
if (this.config.security.useDkim) {
@ -905,10 +905,11 @@ export class MtaService {
/**
* Validate an email before sending
* Performs both basic validation and enhanced validation using smartmail
*/
private validateEmail(email: Email): void {
private async validateEmail(email: Email): Promise<void> {
// The Email class constructor already performs basic validation
// Here we can add additional MTA-specific validation
// Here we add additional MTA-specific validation
if (!email.from) {
throw new Error('Email must have a sender address');
@ -928,6 +929,49 @@ export class MtaService {
if (this.isLocalDomain(senderDomain) && this.config.security.useDkim) {
// DKIM keys will be created if needed in the send method
}
// Enhanced validation using smartmail capabilities
// Only perform MX validation for non-local domains
const isLocalSender = this.isLocalDomain(senderDomain);
// Validate sender and recipient email addresses
try {
// For performance reasons, we only do sender validation for outbound emails
// and first recipient validation for external domains
const validationResult = await email.validateAddresses({
checkMx: true,
checkDisposable: true,
checkSenderOnly: false,
checkFirstRecipientOnly: true
});
// Handle validation failures for non-local domains
if (!validationResult.isValid) {
// For local domains, we're more permissive as we trust our own services
if (!isLocalSender) {
// For external domains, enforce stricter validation
if (!validationResult.sender.result.isValid) {
throw new Error(`Invalid sender email: ${validationResult.sender.email} - ${validationResult.sender.result.details?.errorMessage || 'Validation failed'}`);
}
}
// Always check recipients regardless of domain
const invalidRecipients = validationResult.recipients
.filter(r => !r.result.isValid)
.map(r => `${r.email} (${r.result.details?.errorMessage || 'Validation failed'})`);
if (invalidRecipients.length > 0) {
throw new Error(`Invalid recipient emails: ${invalidRecipients.join(', ')}`);
}
}
} catch (error) {
// Log validation error but don't throw to avoid breaking existing emails
// This allows for graceful degradation if validation fails
console.warn(`Email validation warning: ${error.message}`);
// Mark the email as potentially spam
email.mightBeSpam = true;
}
}
/**