This commit is contained in:
2025-05-23 01:00:37 +00:00
parent 4905595cbb
commit 7344bf0f70
5 changed files with 119 additions and 33 deletions

View File

@ -221,8 +221,12 @@ export class DataHandler implements IDataHandler {
});
// Create a minimal email object on error
const fallbackEmail = new Email();
fallbackEmail.setFromRawData(cleanedData);
const fallbackEmail = new Email({
from: 'unknown@localhost',
to: 'unknown@localhost',
subject: 'Parse Error',
text: cleanedData
});
return fallbackEmail;
}
}
@ -234,22 +238,51 @@ export class DataHandler implements IDataHandler {
* @returns Email object
*/
private async parseEmailFromData(rawData: string, session: ISmtpSession): Promise<Email> {
const email = new Email();
// Parse the raw email data to extract headers and body
const lines = rawData.split('\r\n');
let headerEnd = -1;
// Set raw data
email.setFromRawData(rawData);
// Set envelope information from session
if (session.mailFrom) {
email.setFrom(session.mailFrom);
}
if (session.rcptTo && session.rcptTo.length > 0) {
for (const recipient of session.rcptTo) {
email.addTo(recipient);
// Find where headers end
for (let i = 0; i < lines.length; i++) {
if (lines[i].trim() === '') {
headerEnd = i;
break;
}
}
// Extract headers
let subject = 'No Subject';
const headers: Record<string, string> = {};
if (headerEnd > -1) {
for (let i = 0; i < headerEnd; i++) {
const line = lines[i];
const colonIndex = line.indexOf(':');
if (colonIndex > 0) {
const headerName = line.substring(0, colonIndex).trim().toLowerCase();
const headerValue = line.substring(colonIndex + 1).trim();
if (headerName === 'subject') {
subject = headerValue;
} else {
headers[headerName] = headerValue;
}
}
}
}
// Extract body
const body = headerEnd > -1 ? lines.slice(headerEnd + 1).join('\r\n') : rawData;
// Create email with session information
const email = new Email({
from: session.mailFrom || 'unknown@localhost',
to: session.rcptTo || ['unknown@localhost'],
subject,
text: body,
headers
});
return email;
}
@ -290,7 +323,7 @@ export class DataHandler implements IDataHandler {
// Process the email via the UnifiedEmailServer
// Pass the email object, session data, and specify the mode (mta, forward, or process)
// This connects SMTP reception to the overall email system
const processResult = await this.smtpServer.getEmailServer().processEmailByMode(email, session, 'mta');
const processResult = await this.smtpServer.getEmailServer().processEmailByMode(email, session as any, 'mta');
SmtpLogger.info(`Email processed through UnifiedEmailServer: ${email.getMessageId()}`, {
sessionId: session.id,
@ -340,7 +373,7 @@ export class DataHandler implements IDataHandler {
// Process the email via the UnifiedEmailServer in forward mode
try {
const processResult = await this.smtpServer.getEmailServer().processEmailByMode(email, session, 'forward');
const processResult = await this.smtpServer.getEmailServer().processEmailByMode(email, session as any, 'forward');
SmtpLogger.info(`Email forwarded through UnifiedEmailServer: ${email.getMessageId()}`, {
sessionId: session.id,
@ -379,7 +412,7 @@ export class DataHandler implements IDataHandler {
// Process the email via the UnifiedEmailServer in process mode
try {
const processResult = await this.smtpServer.getEmailServer().processEmailByMode(email, session, 'process');
const processResult = await this.smtpServer.getEmailServer().processEmailByMode(email, session as any, 'process');
SmtpLogger.info(`Email processed directly through UnifiedEmailServer: ${email.getMessageId()}`, {
sessionId: session.id,
@ -1057,8 +1090,8 @@ SmtpLogger.debug(`Parsed email subject: ${subject}`, { subject });
// Optionally save email to disk
this.saveEmail(session);
// Process the email
const result = await this.processEmail(session);
// Process the email using legacy method
const result = await this.processEmailLegacy(session);
if (result.success) {
// Send success response