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:
@ -2,6 +2,7 @@ import * as plugins from '../plugins.js';
|
||||
import * as paths from '../paths.js';
|
||||
import { Email } from './classes.email.js';
|
||||
import type { MtaService } from './classes.mta.js';
|
||||
import { logger } from '../logger.js';
|
||||
|
||||
export interface ISmtpServerOptions {
|
||||
port: number;
|
||||
@ -113,7 +114,11 @@ export class SMTPServer {
|
||||
|
||||
// If we're in DATA_RECEIVING state, handle differently
|
||||
if (session.state === SmtpState.DATA_RECEIVING) {
|
||||
return this.processEmailData(socket, data.toString());
|
||||
// Call async method but don't return the promise
|
||||
this.processEmailData(socket, data.toString()).catch(err => {
|
||||
console.error(`Error processing email data: ${err.message}`);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Process normal SMTP commands
|
||||
@ -350,14 +355,36 @@ export class SMTPServer {
|
||||
}
|
||||
|
||||
let mightBeSpam = false;
|
||||
// Prepare headers for DKIM verification results
|
||||
const customHeaders: Record<string, string> = {};
|
||||
|
||||
// Verifying the email with DKIM
|
||||
// Verifying the email with enhanced DKIM verification
|
||||
try {
|
||||
const isVerified = await this.mtaRef.dkimVerifier.verify(session.emailData);
|
||||
mightBeSpam = !isVerified;
|
||||
const verificationResult = await this.mtaRef.dkimVerifier.verify(session.emailData, {
|
||||
useCache: true,
|
||||
returnDetails: false
|
||||
});
|
||||
|
||||
mightBeSpam = !verificationResult.isValid;
|
||||
|
||||
if (!verificationResult.isValid) {
|
||||
logger.log('warn', `DKIM verification failed for incoming email: ${verificationResult.errorMessage || 'Unknown error'}`);
|
||||
} else {
|
||||
logger.log('info', `DKIM verification passed for incoming email from domain ${verificationResult.domain}`);
|
||||
}
|
||||
|
||||
// Store verification results in headers
|
||||
if (verificationResult.domain) {
|
||||
customHeaders['X-DKIM-Domain'] = verificationResult.domain;
|
||||
}
|
||||
|
||||
customHeaders['X-DKIM-Status'] = verificationResult.status || 'unknown';
|
||||
customHeaders['X-DKIM-Result'] = verificationResult.isValid ? 'pass' : 'fail';
|
||||
} catch (error) {
|
||||
console.error('Failed to verify DKIM signature:', error);
|
||||
logger.log('error', `Failed to verify DKIM signature: ${error.message}`);
|
||||
mightBeSpam = true;
|
||||
customHeaders['X-DKIM-Status'] = 'error';
|
||||
customHeaders['X-DKIM-Result'] = 'error';
|
||||
}
|
||||
|
||||
try {
|
||||
@ -366,6 +393,7 @@ export class SMTPServer {
|
||||
const email = new Email({
|
||||
from: parsedEmail.from?.value[0].address || session.mailFrom,
|
||||
to: session.rcptTo[0], // Use the first recipient
|
||||
headers: customHeaders, // Add our custom headers with DKIM verification results
|
||||
subject: parsedEmail.subject || '',
|
||||
text: parsedEmail.html || parsedEmail.text || '',
|
||||
attachments: parsedEmail.attachments?.map((attachment) => ({
|
||||
|
Reference in New Issue
Block a user