mail page rest dang

This commit is contained in:
alma 2025-04-21 16:21:51 +02:00
parent 784d673063
commit ca090f1ebb

View File

@ -131,28 +131,41 @@ function renderEmailContent(email: Email) {
if (!partHeaders || partBodyParts.length === 0) continue; if (!partHeaders || partBodyParts.length === 0) continue;
const partBody = partBodyParts.join('\r\n\r\n'); const partBody = partBodyParts.join('\r\n\r\n');
const partHeaderInfo = parseEmailHeaders(partHeaders);
const contentType = extractHeader(partHeaders, 'Content-Type').toLowerCase(); const contentType = extractHeader(partHeaders, 'Content-Type').toLowerCase();
const encoding = extractHeader(partHeaders, 'Content-Transfer-Encoding').toLowerCase(); const encoding = extractHeader(partHeaders, 'Content-Transfer-Encoding').toLowerCase();
const charset = extractHeader(partHeaders, 'charset') || 'utf-8'; const charset = extractHeader(partHeaders, 'charset') || 'utf-8';
try { try {
let decodedContent = '';
if (encoding === 'base64') {
decodedContent = decodeBase64(partBody, charset);
} else if (encoding === 'quoted-printable') {
decodedContent = decodeQuotedPrintable(partBody, charset);
} else {
decodedContent = convertCharset(partBody, charset);
}
if (contentType.includes('text/html')) { if (contentType.includes('text/html')) {
const decodedContent = encoding === 'base64' // For HTML content, we want to preserve the HTML structure
? decodeBase64(partBody, charset) // Only clean up problematic elements while keeping the formatting
: decodeQuotedPrintable(partBody, charset); htmlContent = decodedContent
htmlContent = cleanHtml(decodedContent); .replace(/<style[^>]*>[\s\S]*?<\/style>/gi, '')
.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '')
.replace(/<meta[^>]*>/gi, '')
.replace(/<link[^>]*>/gi, '')
.replace(/<base[^>]*>/gi, '')
.replace(/<title[^>]*>[\s\S]*?<\/title>/gi, '')
.replace(/<head[^>]*>[\s\S]*?<\/head>/gi, '')
.replace(/<body[^>]*>/gi, '')
.replace(/<\/body>/gi, '')
.replace(/<html[^>]*>/gi, '')
.replace(/<\/html>/gi, '');
} else if (contentType.includes('text/plain')) { } else if (contentType.includes('text/plain')) {
const decodedContent = encoding === 'base64'
? decodeBase64(partBody, charset)
: decodeQuotedPrintable(partBody, charset);
textContent = decodedContent; textContent = decodedContent;
} else if (contentType.includes('attachment') || extractHeader(partHeaders, 'Content-Disposition').includes('attachment')) { } else if (contentType.includes('attachment') || extractHeader(partHeaders, 'Content-Disposition').includes('attachment')) {
attachments.push({ attachments.push({
filename: extractFilename(partHeaders) || 'unnamed_attachment', filename: extractFilename(partHeaders) || 'unnamed_attachment',
content: encoding === 'base64' content: decodedContent
? decodeBase64(partBody, charset)
: decodeQuotedPrintable(partBody, charset)
}); });
} }
} catch (partError) { } catch (partError) {
@ -196,15 +209,33 @@ function renderEmailContent(email: Email) {
const charset = extractHeader(headersPart, 'charset') || 'utf-8'; const charset = extractHeader(headersPart, 'charset') || 'utf-8';
try { try {
const decodedBody = encoding === 'base64' let decodedBody = '';
? decodeBase64(body, charset) if (encoding === 'base64') {
: decodeQuotedPrintable(body, charset); decodedBody = decodeBase64(body, charset);
} else if (encoding === 'quoted-printable') {
decodedBody = decodeQuotedPrintable(body, charset);
} else {
decodedBody = convertCharset(body, charset);
}
if (contentType.includes('text/html')) { if (contentType.includes('text/html')) {
const cleanedContent = cleanHtml(decodedBody); // For HTML content, preserve the HTML structure
const cleanedHtml = decodedBody
.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, '')
.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '')
.replace(/<meta[^>]*>/gi, '')
.replace(/<link[^>]*>/gi, '')
.replace(/<base[^>]*>/gi, '')
.replace(/<title[^>]*>[\s\S]*?<\/title>/gi, '')
.replace(/<head[^>]*>[\s\S]*?<\/head>/gi, '')
.replace(/<body[^>]*>/gi, '')
.replace(/<\/body>/gi, '')
.replace(/<html[^>]*>/gi, '')
.replace(/<\/html>/gi, '');
return ( return (
<div className="email-content"> <div className="email-content">
<div className="prose max-w-none" dangerouslySetInnerHTML={{ __html: cleanedContent }} /> <div className="prose max-w-none" dangerouslySetInnerHTML={{ __html: cleanedHtml }} />
</div> </div>
); );
} else { } else {