diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index b1c1c994..0127be3b 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -144,7 +144,9 @@ function parseFullEmail(emailRaw: string): ParsedEmailContent { // Handle multipart content if (contentType.includes('multipart')) { - const boundaryMatch = emailRaw.match(/boundary="?([^"\r\n;]+)"?/i); + const boundaryMatch = emailRaw.match(/boundary="?([^"\r\n;]+)"?/i) || + emailRaw.match(/boundary=([^\r\n;]+)/i); + if (boundaryMatch) { const boundary = boundaryMatch[1].trim(); const parts = emailRaw.split(new RegExp(`--${boundary}(?:--)?(\\r?\\n|$)`)); @@ -438,88 +440,58 @@ function decodeMimeContent(content: string): string { return cleanHtml(content); } -// Add this helper function -const renderEmailContent = (email: Email) => { +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 200 chars of body:', email.body.substring(0, 200)); + console.log('First 100 chars:', email.body.substring(0, 100)); - try { - const parsed = parseFullEmail(email.body); - console.log('Parsed content:', { - hasText: !!parsed.text, - hasHtml: !!parsed.html, - textPreview: parsed.text?.substring(0, 100) || 'No text', - htmlPreview: parsed.html?.substring(0, 100) || 'No HTML' - }); + const parsedContent = parseFullEmail(email.body); + console.log('Parsed content:', { + hasText: !!parsedContent.text, + hasHtml: !!parsedContent.html, + hasAttachments: parsedContent.attachments.length > 0 + }); - const content = parsed.html || parsed.text || email.body; - const isHtml = !!parsed.html; + // Determine content type + const isHtml = parsedContent.html !== null; + const content = isHtml ? parsedContent.html : parsedContent.text; - console.log('Selected content type:', isHtml ? 'HTML' : 'Plain text'); - console.log('Content preview:', content.substring(0, 100) + '...'); - - if (isHtml) { - return ( -
- {parsed.attachments.length > 0 && ( -
-

Attachments:

-
- {parsed.attachments.map((attachment, index) => ( -
- - {attachment.filename} - - ({attachment.contentType}) - -
- ))} -
-
- )} -
-
- ); - } else { - return ( -
- {parsed.attachments.length > 0 && ( -
-

Attachments:

-
- {parsed.attachments.map((attachment, index) => ( -
- - {attachment.filename} - - ({attachment.contentType}) - -
- ))} -
-
- )} -
{content}
-
- ); - } - } catch (e) { - console.error('Error in renderEmailContent:', e); - return ( -
- Error rendering email content. Please try refreshing the page. -
- ); + if (!content) { + console.log('No content available'); + return
No content available
; } -}; -// Add this helper function -const decodeEmailContent = (content: string, charset: string = 'utf-8') => { - return convertCharset(content, charset); -}; + // 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} +
+
+ )); + + return ( +
+ {isHtml ? ( +
+ ) : ( +
{sanitizedContent}
+ )} + {attachmentElements} +
+ ); +} function cleanEmailContent(content: string): string { // Remove or fix malformed URLs