mail page fix

This commit is contained in:
alma 2025-04-21 19:12:01 +02:00
parent 1f9040bd98
commit a7cfeb759a

View File

@ -331,15 +331,24 @@ const initialSidebarItems = [
function getReplyBody(email: Email, type: 'reply' | 'reply-all' | 'forward'): string { function getReplyBody(email: Email, type: 'reply' | 'reply-all' | 'forward'): string {
if (!email.body) return ''; if (!email.body) return '';
const { headers, body } = splitEmailHeadersAndBody(email.body); try {
const { contentType, encoding, charset } = parseEmailHeaders(headers); // Split email into headers and body
const [headersPart, ...bodyParts] = email.body.split('\r\n\r\n');
if (!headersPart || bodyParts.length === 0) {
throw new Error('Invalid email format: missing headers or body');
}
const body = bodyParts.join('\r\n\r\n');
// Parse headers using Infomaniak MIME decoder
const headerInfo = parseEmailHeaders(headersPart);
const boundary = extractBoundary(headersPart);
let content = ''; let content = '';
if (contentType.includes('multipart/')) { // If it's a multipart email
const boundary = contentType.match(/boundary="([^"]+)"/)?.[1];
if (boundary) { if (boundary) {
const parts = body.split('--' + boundary).filter(part => part.trim()); const parts = body.split(`--${boundary}`);
// Find HTML part first, fallback to text part // Find HTML part first, fallback to text part
const htmlPart = parts.find(part => part.toLowerCase().includes('content-type: text/html')); const htmlPart = parts.find(part => part.toLowerCase().includes('content-type: text/html'));
@ -347,29 +356,26 @@ function getReplyBody(email: Email, type: 'reply' | 'reply-all' | 'forward'): st
const selectedPart = htmlPart || textPart; const selectedPart = htmlPart || textPart;
if (selectedPart) { if (selectedPart) {
const partHeaders = selectedPart.split('\r\n\r\n')[0]; const [partHeaders, ...partBodyParts] = selectedPart.split('\r\n\r\n');
const partBody = selectedPart.split('\r\n\r\n').slice(1).join('\r\n\r\n'); const partBody = partBodyParts.join('\r\n\r\n');
const { encoding: partEncoding } = parseEmailHeaders(partHeaders); const partHeaderInfo = parseEmailHeaders(partHeaders);
content = partEncoding === 'quoted-printable' content = partHeaderInfo.encoding === 'quoted-printable'
? decodeQuotedPrintable(partBody, charset) ? decodeQuotedPrintable(partBody, partHeaderInfo.charset)
: partBody; : partBody;
} }
}
} else { } else {
content = encoding === 'quoted-printable' content = headerInfo.encoding === 'quoted-printable'
? decodeQuotedPrintable(body, charset) ? decodeQuotedPrintable(body, headerInfo.charset)
: body; : body;
} }
// Convert plain text to HTML if needed, preserving line breaks // Convert plain text to HTML if needed
if (!contentType.includes('text/html')) { if (!headerInfo.contentType.includes('text/html')) {
content = content content = content
.split('\n') .split('\n')
.map(line => { .map(line => {
// Preserve empty lines
if (!line.trim()) return '<br>'; if (!line.trim()) return '<br>';
// Handle quoted text
if (line.startsWith('>')) { if (line.startsWith('>')) {
return `<p class="text-gray-600">${line}</p>`; return `<p class="text-gray-600">${line}</p>`;
} }
@ -378,29 +384,8 @@ function getReplyBody(email: Email, type: 'reply' | 'reply-all' | 'forward'): st
.join(''); .join('');
} }
// Clean HTML content while preserving formatting // Clean HTML content
content = content content = cleanHtml(content);
.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, '');
// Sanitize HTML content while preserving formatting
content = DOMPurify.sanitize(content, {
ALLOWED_TAGS: [
'p', 'br', 'div', 'span', 'b', 'i', 'u', 'strong', 'em',
'blockquote', 'ul', 'ol', 'li', 'a', 'h1', 'h2', 'h3', 'h4',
'table', 'thead', 'tbody', 'tr', 'td', 'th', 'pre', 'code'
],
ALLOWED_ATTR: ['href', 'style', 'class', 'target'],
});
const date = new Date(email.date).toLocaleString(); const date = new Date(email.date).toLocaleString();
@ -408,10 +393,10 @@ function getReplyBody(email: Email, type: 'reply' | 'reply-all' | 'forward'): st
return ` return `
<div class="prose max-w-none"> <div class="prose max-w-none">
<div class="border-l-4 border-gray-300 pl-4 my-4"> <div class="border-l-4 border-gray-300 pl-4 my-4">
<p class="text-sm text-gray-600"><strong>From:</strong> ${email.from}</p> <p class="text-sm text-gray-600 mb-2"><strong>From:</strong> ${email.from}</p>
<p class="text-sm text-gray-600"><strong>Date:</strong> ${date}</p> <p class="text-sm text-gray-600 mb-2"><strong>Date:</strong> ${date}</p>
<p class="text-sm text-gray-600"><strong>Subject:</strong> ${email.subject}</p> <p class="text-sm text-gray-600 mb-2"><strong>Subject:</strong> ${email.subject}</p>
<p class="text-sm text-gray-600"><strong>To:</strong> ${Array.isArray(email.to) ? email.to.join(', ') : email.to}</p> <p class="text-sm text-gray-600 mb-2"><strong>To:</strong> ${Array.isArray(email.to) ? email.to.join(', ') : email.to}</p>
<div class="mt-4 prose-sm">${content}</div> <div class="mt-4 prose-sm">${content}</div>
</div> </div>
</div> </div>
@ -420,12 +405,16 @@ function getReplyBody(email: Email, type: 'reply' | 'reply-all' | 'forward'): st
return ` return `
<div class="prose max-w-none"> <div class="prose max-w-none">
<div class="border-l-4 border-gray-300 pl-4 my-4"> <div class="border-l-4 border-gray-300 pl-4 my-4">
<p class="text-sm text-gray-600">On ${date}, ${email.from} wrote:</p> <p class="text-sm text-gray-600 mb-2">On ${date}, ${email.from} wrote:</p>
<div class="mt-4 prose-sm">${content}</div> <div class="mt-4 prose-sm">${content}</div>
</div> </div>
</div> </div>
`; `;
} }
} catch (error) {
console.error('Error processing email body:', error);
return '';
}
} }
export default function CourrierPage() { export default function CourrierPage() {