-
-
- {email.fromName ? `${email.fromName} <${email.from}>` : email.from}
-
- {email.to && (
-
- To: {email.to}
-
- )}
- {email.cc && (
-
- CC: {email.cc}
-
- )}
-
-
- {new Date(email.date).toLocaleString('fr-FR', {
- day: 'numeric',
- month: 'short',
- hour: '2-digit',
- minute: '2-digit'
- })}
+ // 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(/