diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx
index 5f25efce..f5d020e0 100644
--- a/app/courrier/page.tsx
+++ b/app/courrier/page.tsx
@@ -40,6 +40,8 @@ import {
} from '@/lib/infomaniak-mime-decoder';
import DOMPurify from 'isomorphic-dompurify';
import ComposeEmail from '@/components/ComposeEmail';
+import { decodeEmail } from '@/lib/mail-parser-wrapper';
+import { Attachment as MailParserAttachment } from 'mailparser';
export interface Account {
id: number;
@@ -108,188 +110,67 @@ function splitEmailHeadersAndBody(emailBody: string): { headers: string; body: s
};
}
-function renderEmailContent(email: Email) {
+async function renderEmailContent(email: Email) {
if (!email.body) {
console.warn('No email body provided');
return null;
}
try {
- // Split email into headers and body
- const [headersPart, ...bodyParts] = email.body.split('\r\n\r\n');
- if (!headersPart || bodyParts.length === 0) {
- throw new Error('Invalid email format: missing headers or body');
+ const decoded = await decodeEmail(email.body);
+
+ // Prefer HTML content if available
+ if (decoded.html) {
+ return (
+
+
+ {decoded.attachments.length > 0 && renderAttachments(decoded.attachments)}
+
+ );
}
- const body = bodyParts.join('\r\n\r\n');
-
- // Parse headers using Infomaniak MIME decoder
- const headerInfo = parseEmailHeaders(headersPart);
- const boundary = extractBoundary(headersPart);
-
- // If it's a multipart email
- if (boundary) {
- try {
- const parts = body.split(`--${boundary}`);
- let htmlContent = '';
- let textContent = '';
- let attachments: { filename: string; content: string }[] = [];
-
- for (const part of parts) {
- if (!part.trim()) continue;
-
- const [partHeaders, ...partBodyParts] = part.split('\r\n\r\n');
- if (!partHeaders || partBodyParts.length === 0) continue;
-
- const partBody = partBodyParts.join('\r\n\r\n');
- const contentType = extractHeader(partHeaders, 'Content-Type').toLowerCase();
- const encoding = extractHeader(partHeaders, 'Content-Transfer-Encoding').toLowerCase();
- const charset = extractHeader(partHeaders, 'charset') || 'utf-8';
-
- try {
- let decodedContent = '';
- if (encoding === 'base64') {
- decodedContent = decodeBase64(partBody, charset);
- } else if (encoding === 'quoted-printable') {
- decodedContent = decodeQuotedPrintable(partBody, charset);
- } else {
- decodedContent = convertCharset(partBody, charset);
- }
-
- if (contentType.includes('text/html')) {
- // For HTML content, we want to preserve the HTML structure
- // Only clean up problematic elements while keeping the formatting
- htmlContent = decodedContent
- .replace(/