From b53447e84d5c0dea6118cd81368a6a99eebb6104 Mon Sep 17 00:00:00 2001 From: alma Date: Mon, 21 Apr 2025 12:30:06 +0200 Subject: [PATCH] mail page rest --- app/courrier/page.tsx | 160 ++++++++++++++++++++++++------------------ 1 file changed, 93 insertions(+), 67 deletions(-) diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index b46f7097..280d78a8 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -413,11 +413,76 @@ function decodeMimeContent(content: string): string { // Add this helper function const renderEmailContent = (email: Email) => { - const decodedContent = decodeMimeContent(email.body); - if (email.body.includes('Content-Type: text/html')) { - return
; + try { + // First try to decode the MIME content + const decodedContent = decodeMimeContent(email.body); + + // Check if the content is HTML + const isHtml = decodedContent.includes('<') && + (decodedContent.includes(']*>([\s\S]*?)<\/body>/i); + const content = bodyMatch ? bodyMatch[1] : decodedContent; + + // Sanitize HTML content + const sanitizedHtml = content + .replace(/)<[^<]*)*<\/script>/gi, '') + .replace(/)<[^<]*)*<\/style>/gi, '') + .replace(/on\w+="[^"]*"/g, '') + .replace(/on\w+='[^']*'/g, '') + .replace(/javascript:/gi, '') + .replace(/data:/gi, '') + .replace(/]*>/gi, '') + .replace(/]*>/gi, '') + // Fix common encoding issues + .replace(/=C2=A0/g, ' ') + .replace(/=E2=80=93/g, '\u2013') + .replace(/=E2=80=94/g, '\u2014') + .replace(/=E2=80=98/g, '\u2018') + .replace(/=E2=80=99/g, '\u2019') + .replace(/=E2=80=9C/g, '\u201C') + .replace(/=E2=80=9D/g, '\u201D'); + + return ( +
+ ); + } else { + // Format plain text content + const formattedText = decodedContent + .replace(/\n/g, '
') + .replace(/\t/g, '    ') + .replace(/ /g, '  ') + // Fix common encoding issues + .replace(/=C2=A0/g, ' ') + .replace(/=E2=80=93/g, '\u2013') + .replace(/=E2=80=94/g, '\u2014') + .replace(/=E2=80=98/g, '\u2018') + .replace(/=E2=80=99/g, '\u2019') + .replace(/=E2=80=9C/g, '\u201C') + .replace(/=E2=80=9D/g, '\u201D'); + + return ( +
+ ); + } + } catch (e) { + console.error('Error rendering email content:', e); + return ( +
+ Error rendering email content. Please try refreshing the page. +
+ ); } - return
{decodedContent}
; }; // Add this helper function @@ -1028,39 +1093,7 @@ export default function CourrierPage() {
- {(() => { - try { - const parsed = parseFullEmail(selectedEmail.body); - return ( -
- {/* Display HTML content if available, otherwise fallback to text */} -
- - {/* Display attachments if present */} - {parsed.attachments && parsed.attachments.length > 0 && ( -
-

Attachments

-
- {parsed.attachments.map((attachment, index) => ( -
- - - {attachment.filename} - -
- ))} -
-
- )} -
- ); - } catch (e) { - console.error('Error parsing email:', e); - return selectedEmail.body; - } - })()} + {renderEmailContent(selectedEmail)}
@@ -1141,38 +1174,32 @@ export default function CourrierPage() {
{(() => { - // Get clean preview of the actual message content - let preview = ''; try { - const parsed = parseFullEmail(email.body); + // First decode the MIME content + const decodedContent = decodeMimeContent(email.body); - // Try to get content from parsed email - preview = (parsed.text || parsed.html || '') + // Extract preview text + let preview = decodedContent + // Remove HTML tags .replace(/]*>[\s\S]*?<\/style>/gi, '') .replace(/]*>[\s\S]*?<\/script>/gi, '') - .replace(/<[^>]+>/g, '') - .replace(/ |‌|»|«|>/g, ' ') + .replace(/<[^>]+>/g, ' ') + // Remove email headers + .replace(/^(From|To|Sent|Subject|Date|Cc|Bcc):.*$/gim, '') + // Remove quoted text + .replace(/^>.*$/gm, '') + // Remove multiple spaces .replace(/\s+/g, ' ') - .trim(); - - // If no preview from parsed content, try direct body - if (!preview) { - preview = email.body - .replace(/<[^>]+>/g, '') - .replace(/ |‌|»|«|>/g, ' ') - .replace(/\s+/g, ' ') - .trim(); - } - - // Remove email artifacts and clean up - preview = preview - .replace(/^>+/gm, '') - .replace(/Content-Type:[^\n]+/g, '') - .replace(/Content-Transfer-Encoding:[^\n]+/g, '') - .replace(/--[a-zA-Z0-9]+(-[a-zA-Z0-9]+)?/g, '') - .replace(/boundary=[^\n]+/g, '') - .replace(/charset=[^\n]+/g, '') - .replace(/[\r\n]+/g, ' ') + // Remove special characters + .replace(/ |‌|»|«|>/g, ' ') + // Fix common encoding issues + .replace(/=C2=A0/g, ' ') + .replace(/=E2=80=93/g, '\u2013') + .replace(/=E2=80=94/g, '\u2014') + .replace(/=E2=80=98/g, '\u2018') + .replace(/=E2=80=99/g, '\u2019') + .replace(/=E2=80=9C/g, '\u201C') + .replace(/=E2=80=9D/g, '\u201D') .trim(); // Take first 100 characters @@ -1187,12 +1214,11 @@ export default function CourrierPage() { preview += '...'; } + return preview || 'No preview available'; } catch (e) { console.error('Error generating preview:', e); - preview = ''; + return 'Error loading preview'; } - - return preview || 'No preview available'; })()}