mail page rest

This commit is contained in:
alma 2025-04-21 15:27:12 +02:00
parent f4502f15ff
commit b5ba7ef508

View File

@ -111,14 +111,23 @@ function parseFullEmail(emailContent: string): ParsedEmailContent {
const headerInfo = parseEmailHeaders(headers);
const boundary = extractBoundary(headers);
// Initialize result object
const result: ParsedEmailContent = {
headers,
body: '',
html: undefined,
text: undefined,
attachments: []
};
// Handle multipart content
if (boundary && headerInfo.contentType.startsWith('multipart/')) {
const parts = body.split(`--${boundary}`);
const processedParts = parts
parts
.filter(part => part.trim() && !part.includes('--'))
.map(part => {
.forEach(part => {
const partHeaderEnd = part.indexOf('\r\n\r\n');
if (partHeaderEnd === -1) return part;
if (partHeaderEnd === -1) return;
const partHeaders = part.substring(0, partHeaderEnd);
const partBody = part.substring(partHeaderEnd + 4);
@ -131,19 +140,25 @@ function parseFullEmail(emailContent: string): ParsedEmailContent {
decodedContent = decodeBase64(partBody, partInfo.charset);
}
// Handle different content types
if (partInfo.contentType.includes('text/html')) {
decodedContent = cleanHtml(decodedContent);
result.html = cleanHtml(decodedContent);
} else if (partInfo.contentType.includes('text/plain')) {
result.text = decodedContent;
} else if (partInfo.contentType.includes('application/') || partInfo.contentType.includes('image/')) {
// Handle attachments
const filename = extractFilename(partHeaders) || `attachment-${Date.now()}`;
result.attachments?.push({
filename,
content: decodedContent,
contentType: partInfo.contentType
});
}
return decodedContent;
});
return {
headers,
body: processedParts.join('\n\n')
};
}
// Set the body to the text content if available, otherwise use HTML
result.body = result.text || result.html || '';
} else {
// Handle single part content
let decodedBody = body;
if (headerInfo.encoding === 'quoted-printable') {
@ -153,13 +168,17 @@ function parseFullEmail(emailContent: string): ParsedEmailContent {
}
if (headerInfo.contentType.includes('text/html')) {
decodedBody = cleanHtml(decodedBody);
result.html = cleanHtml(decodedBody);
result.body = result.html;
} else if (headerInfo.contentType.includes('text/plain')) {
result.text = decodedBody;
result.body = result.text;
} else {
result.body = decodedBody;
}
}
return {
headers,
body: decodedBody
};
return result;
}
function extractTextFromHtml(html: string): string {