From c039291aeace9b523ba77918de3eaf9034bcda2d Mon Sep 17 00:00:00 2001 From: alma Date: Mon, 21 Apr 2025 15:16:24 +0200 Subject: [PATCH] mail page rest --- app/courrier/page.tsx | 145 +++++++++++++++++++++--------------------- 1 file changed, 71 insertions(+), 74 deletions(-) diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index 146dca20..d9dfc7fb 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -74,6 +74,13 @@ interface Attachment { interface ParsedEmailContent { headers: string; body: string; + html?: string; + text?: string; + attachments?: Array<{ + filename: string; + content: string; + contentType: string; + }>; } interface ParsedEmailMetadata { @@ -245,89 +252,79 @@ function decodeMimeContent(content: string): string { } function renderEmailContent(email: Email) { - console.log('=== renderEmailContent Debug ==='); - console.log('Email ID:', email.id); - console.log('Subject:', email.subject); - console.log('Body length:', email.body.length); - console.log('First 100 chars:', email.body.substring(0, 100)); - try { // First try to parse the full email const parsed = parseFullEmail(email.body); - console.log('Parsed content:', { - hasText: !!parsed.body, - hasHtml: !!parsed.headers, - hasAttachments: parsed.headers.length > 0 - }); - - // Determine content and type - let content = ''; - let isHtml = false; - - if (parsed.headers) { - // Use our existing MIME decoding for HTML content - content = decodeMIME(parsed.headers, 'quoted-printable', 'utf-8'); - isHtml = true; - } else if (parsed.body) { - // Use our existing MIME decoding for plain text content - content = decodeMIME(parsed.body, 'quoted-printable', 'utf-8'); - isHtml = false; - } else { - // Try to extract content directly from body using our existing functions - const htmlMatch = email.body.match(/]*>[\s\S]*?<\/html>/i); - if (htmlMatch) { - content = decodeMIME(htmlMatch[0], 'quoted-printable', 'utf-8'); - isHtml = true; - } else { - // Use our existing text extraction function - content = extractTextFromHtml(email.body); - isHtml = false; - } + + // If we have HTML content, display it + if (parsed.html) { + return ( +
]*>[\s\S]*?<\/style>/gi, '') + .replace(/]*>[\s\S]*?<\/script>/gi, '') + .replace(/]*>/gi, '') + .replace(/]*>/gi, '') + .replace(/]*>/gi, '') + .replace(/]*>[\s\S]*?<\/title>/gi, '') + .replace(/]*>[\s\S]*?<\/head>/gi, '') + .replace(/]*>/gi, '') + .replace(/<\/body>/gi, '') + .replace(/]*>/gi, '') + .replace(/<\/html>/gi, '') + }} + /> + ); } - if (!content) { - console.log('No content available after all attempts'); - return
No content available
; + // If we have text content, display it + if (parsed.text) { + return ( +
+ {parsed.text.split('\n').map((line: string, i: number) => ( +

{line}

+ ))} +
+ ); } - // Handle attachments - const attachmentElements = parsed.headers.split('\n').filter(header => header.startsWith('Content-Type:')).map((header, index) => ( -
-
- - {header.split(': ')[1]} -
-
- )); - - return ( -
- {isHtml ? ( -
]*>[\s\S]*?<\/style>/gi, '') - .replace(/]*>[\s\S]*?<\/script>/gi, '') - .replace(/]*>/gi, '') - .replace(/]*>/gi, '') - .replace(/]*>/gi, '') - .replace(/]*>[\s\S]*?<\/title>/gi, '') - .replace(/]*>[\s\S]*?<\/head>/gi, '') - .replace(/]*>/gi, '') - .replace(/<\/body>/gi, '') - .replace(/]*>/gi, '') - .replace(/<\/html>/gi, '') - }} - /> - ) : ( -
- {content.split('\n').map((line, i) => ( -

{line}

+ // If we have attachments, display them + if (parsed.attachments && parsed.attachments.length > 0) { + return ( +
+

Attachments

+
+ {parsed.attachments.map((attachment: { filename: string }, index: number) => ( +
+ + + {attachment.filename} + +
))}
- )} - {attachmentElements} +
+ ); + } + + // If we couldn't parse the content, try to clean and display the raw body + const cleanedContent = email.body + .replace(/Content-Type:[^\n]+/g, '') + .replace(/Content-Transfer-Encoding:[^\n]+/g, '') + .replace(/MIME-Version:[^\n]+/g, '') + .replace(/--[a-zA-Z0-9]+(-[a-zA-Z0-9]+)?/g, '') + .replace(/boundary=[^\n]+/g, '') + .replace(/charset=[^\n]+/g, '') + .replace(/[\r\n]+/g, '\n') + .trim(); + + return ( +
+ {cleanedContent.split('\n').map((line: string, i: number) => ( +

{line}

+ ))}
); } catch (e) {