From 28612fb39e0b5a0358ebecdb3f64579aeed6bd3e Mon Sep 17 00:00:00 2001 From: alma Date: Mon, 21 Apr 2025 13:27:20 +0200 Subject: [PATCH] mail page rest --- app/courrier/page.tsx | 115 ++++++++++++------------------------------ 1 file changed, 32 insertions(+), 83 deletions(-) diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index 1283b24e..ac228e5c 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -436,7 +436,7 @@ function cleanHtml(html: string): string { function decodeMimeContent(content: string): string { if (!content) return ''; - // Check if this is an Infomaniak multipart message + // Check if this is a multipart message if (content.includes('Content-Type: multipart/')) { const boundary = content.match(/boundary="([^"]+)"/)?.[1]; if (boundary) { @@ -474,93 +474,42 @@ function renderEmailContent(email: Email) { console.log('Body length:', email.body.length); console.log('First 100 chars:', email.body.substring(0, 100)); - const parsedContent = parseFullEmail(email.body); - console.log('Parsed content:', { - hasText: !!parsedContent.text, - hasHtml: !!parsedContent.html, - hasAttachments: parsedContent.attachments.length > 0 - }); + try { + const parsed = parseFullEmail(email.body); + console.log('Parsed content:', { + hasText: !!parsed.text, + hasHtml: !!parsed.html, + hasAttachments: parsed.attachments.length > 0 + }); - // Determine content type and get content - let content = null; - let isHtml = false; - - if (parsedContent.html) { - content = parsedContent.html; - isHtml = true; - } else if (parsedContent.text) { - content = parsedContent.text; - isHtml = false; - } - - if (!content) { - console.log('No content available from parsing, trying direct body'); - // Try to extract content directly from body - const htmlMatch = email.body.match(/]*>[\s\S]*?<\/html>/i); - if (htmlMatch) { - content = htmlMatch[0]; - isHtml = true; - } else { - content = email.body - .replace(/<[^>]+>/g, '') - .replace(/ /g, ' ') - .replace(/&/g, '&') - .replace(/</g, '<') - .replace(/>/g, '>') - .replace(/"/g, '"') - .replace(/\r\n/g, '\n') - .replace(/=\n/g, '') - .replace(/=3D/g, '=') - .replace(/=09/g, '\t') - .trim(); - isHtml = false; + // Display HTML content if available, otherwise fallback to text + const content = parsed.html || parsed.text || decodeMimeContent(email.body); + + if (!content) { + console.log('No content available after all attempts'); + return
No content available
; } - } - if (!content) { - console.log('No content available after all attempts'); - return
No content available
; - } - - // Clean and sanitize content - const sanitizedContent = isHtml ? - cleanEmailContent(content) : - content.replace(/&/g, '&') - .replace(//g, '>') - .replace(/\n/g, '
'); - - // Handle attachments - const attachmentElements = parsedContent.attachments.map((attachment, index) => ( -
-
- - {attachment.filename} + // Handle attachments + const attachmentElements = parsed.attachments.map((attachment, index) => ( +
+
+ + {attachment.filename} +
-
- )); + )); - return ( -
- {isHtml ? ( -
- ) : ( -
{sanitizedContent}
- )} - {attachmentElements} -
- ); -} - -function cleanEmailContent(content: string): string { - // Remove or fix malformed URLs - return content.replace(/=3D"(http[^"]+)"/g, (match, url) => { - try { - return `"${decodeURIComponent(url)}"`; - } catch { - return ''; - } - }); + return ( +
+
+ {attachmentElements} +
+ ); + } catch (e) { + console.error('Error parsing email:', e); + return
Error displaying email content
; + } } // Define the exact folder names from IMAP