diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx
index 290d5d90..27335ee0 100644
--- a/app/courrier/page.tsx
+++ b/app/courrier/page.tsx
@@ -328,93 +328,98 @@ const initialSidebarItems = [
}
];
-function getReplyBody(email: Email, type: 'reply' | 'reply-all' | 'forward'): string {
+function getReplyBody(email: Email, type: 'reply' | 'reply-all' | 'forward' = 'reply') {
if (!email.body) return '';
-
- 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 body = bodyParts.join('\r\n\r\n');
-
- // Parse headers using Infomaniak MIME decoder
- const headerInfo = parseEmailHeaders(headersPart);
- const boundary = extractBoundary(headersPart);
-
- let content = '';
-
- // If it's a multipart email
+ let content = '';
+ let headers = '';
+ let body = '';
+
+ // Split headers and body
+ const parts = email.body.split('\r\n\r\n');
+ if (parts.length > 1) {
+ headers = parts[0];
+ body = parts.slice(1).join('\r\n\r\n');
+ } else {
+ body = email.body;
+ }
+
+ // Handle multipart emails
+ if (headers.includes('multipart/alternative')) {
+ const boundary = headers.match(/boundary="([^"]+)"/)?.[1];
if (boundary) {
const parts = body.split(`--${boundary}`);
-
- // Find HTML part first, fallback to text part
- const htmlPart = parts.find(part => part.toLowerCase().includes('content-type: text/html'));
- const textPart = parts.find(part => part.toLowerCase().includes('content-type: text/plain'));
-
- const selectedPart = htmlPart || textPart;
- if (selectedPart) {
- const [partHeaders, ...partBodyParts] = selectedPart.split('\r\n\r\n');
- const partBody = partBodyParts.join('\r\n\r\n');
- const partHeaderInfo = parseEmailHeaders(partHeaders);
-
- content = partHeaderInfo.encoding === 'quoted-printable'
- ? decodeQuotedPrintable(partBody, partHeaderInfo.charset)
- : partBody;
+ for (const part of parts) {
+ if (part.includes('text/html')) {
+ content = part.split('\r\n\r\n')[1] || '';
+ break;
+ } else if (part.includes('text/plain')) {
+ content = part.split('\r\n\r\n')[1] || '';
+ }
}
- } else {
- content = headerInfo.encoding === 'quoted-printable'
- ? decodeQuotedPrintable(body, headerInfo.charset)
- : body;
}
-
- // Convert plain text to HTML if needed
- if (!headerInfo.contentType.includes('text/html')) {
- content = content
- .split('\n')
- .map(line => {
- if (!line.trim()) return '
';
- if (line.startsWith('>')) {
- return `
${line}
`; - } - return `${line}
`; - }) - .join(''); - } - - // Clean HTML content - content = cleanHtml(content); - - const date = new Date(email.date).toLocaleString(); - - if (type === 'forward') { - return ` -From: ${email.from}
-Date: ${date}
-Subject: ${email.subject}
-To: ${Array.isArray(email.to) ? email.to.join(', ') : email.to}
-On ${date}, ${email.from} wrote:
-