diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index fe1b15f6..12bac525 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -231,9 +231,9 @@ function decodeMIME(text: string, encoding?: string, charset: string = 'utf-8'): } } -function extractHtmlBody(htmlContent: string): string { - const bodyMatch = htmlContent.match(/]*>([\s\S]*?)<\/body>/i); - return bodyMatch ? bodyMatch[1] : htmlContent; +function extractHtmlBody(html: string): string { + const bodyMatch = html.match(/]*>([\s\S]*?)<\/body>/i); + return bodyMatch ? bodyMatch[1] : html; } function decodeMimeContent(content: string): string { @@ -271,91 +271,78 @@ function decodeMimeContent(content: string): string { } function renderEmailContent(email: Email) { + if (!email.body) return null; + try { - // Parse the full email content + // Parse the email content using our MIME decoder const parsed = parseFullEmail(email.body); - // If we have HTML content, display it + // If we have HTML content, render 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, '') - }} - /> +
+
+ {parsed.attachments && parsed.attachments.length > 0 && ( +
+

Attachments:

+
    + {parsed.attachments.map((attachment, index) => ( +
  • + + {attachment.filename} +
  • + ))} +
+
+ )}
); } - // If we have text content, display it with proper formatting + // If we have text content, render it if (parsed.text) { return ( -
- {parsed.text} -
- ); - } - - // If we have attachments, display them - if (parsed.attachments && parsed.attachments.length > 0) { - return ( -
-
- {parsed.attachments.map((attachment, index) => ( -
- {attachment.filename} - - ({attachment.contentType}) - -
+
+
+ {parsed.text.split('\n').map((line, i) => ( +

{line}

))}
- {parsed.body && ( -
- {parsed.body} + {parsed.attachments && parsed.attachments.length > 0 && ( +
+

Attachments:

+
    + {parsed.attachments.map((attachment, index) => ( +
  • + + {attachment.filename} +
  • + ))} +
)}
); } - // If parsing failed, try to clean and display the raw body - const cleanedBody = email.body - .replace(/Content-Type:.*?\r\n/g, '') - .replace(/Content-Transfer-Encoding:.*?\r\n/g, '') - .replace(/MIME-Version:.*?\r\n/g, '') - .replace(/--.*?--\r\n/g, '') - .replace(/--.*?\r\n/g, '') - .replace(/=\r\n/g, '') - .replace(/=3D/g, '=') - .replace(/=20/g, ' ') - .replace(/=09/g, '\t') - .replace(/=0A/g, '\n') - .replace(/=0D/g, '\r') - .replace(/=([0-9A-F]{2})/g, (_, hex) => String.fromCharCode(parseInt(hex, 16))); + // If we couldn't parse the content, try to decode and clean the raw body + const decodedBody = decodeMIME(email.body, 'quoted-printable', 'utf-8'); + const cleanedContent = cleanHtml(decodedBody); return ( -
- {cleanedBody} +
+
+ {cleanedContent.split('\n').map((line, i) => ( +

{line}

+ ))} +
); } catch (error) { console.error('Error rendering email content:', error); return ( -
- Unable to display email content +
+
{email.body}
); }