diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index f0445f92..4e73c8ef 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -111,14 +111,23 @@ function parseFullEmail(emailContent: string): ParsedEmailContent { const headerInfo = parseEmailHeaders(headers); const boundary = extractBoundary(headers); + // Initialize result object + const result: ParsedEmailContent = { + headers, + body: '', + html: undefined, + text: undefined, + attachments: [] + }; + // Handle multipart content if (boundary && headerInfo.contentType.startsWith('multipart/')) { const parts = body.split(`--${boundary}`); - const processedParts = parts + parts .filter(part => part.trim() && !part.includes('--')) - .map(part => { + .forEach(part => { const partHeaderEnd = part.indexOf('\r\n\r\n'); - if (partHeaderEnd === -1) return part; + if (partHeaderEnd === -1) return; const partHeaders = part.substring(0, partHeaderEnd); const partBody = part.substring(partHeaderEnd + 4); @@ -131,35 +140,45 @@ function parseFullEmail(emailContent: string): ParsedEmailContent { decodedContent = decodeBase64(partBody, partInfo.charset); } + // Handle different content types if (partInfo.contentType.includes('text/html')) { - decodedContent = cleanHtml(decodedContent); + result.html = cleanHtml(decodedContent); + } else if (partInfo.contentType.includes('text/plain')) { + result.text = decodedContent; + } else if (partInfo.contentType.includes('application/') || partInfo.contentType.includes('image/')) { + // Handle attachments + const filename = extractFilename(partHeaders) || `attachment-${Date.now()}`; + result.attachments?.push({ + filename, + content: decodedContent, + contentType: partInfo.contentType + }); } - - return decodedContent; }); - return { - headers, - body: processedParts.join('\n\n') - }; + // Set the body to the text content if available, otherwise use HTML + result.body = result.text || result.html || ''; + } else { + // Handle single part content + let decodedBody = body; + if (headerInfo.encoding === 'quoted-printable') { + decodedBody = decodeQuotedPrintable(body, headerInfo.charset); + } else if (headerInfo.encoding === 'base64') { + decodedBody = decodeBase64(body, headerInfo.charset); + } + + if (headerInfo.contentType.includes('text/html')) { + result.html = cleanHtml(decodedBody); + result.body = result.html; + } else if (headerInfo.contentType.includes('text/plain')) { + result.text = decodedBody; + result.body = result.text; + } else { + result.body = decodedBody; + } } - // Handle single part content - let decodedBody = body; - if (headerInfo.encoding === 'quoted-printable') { - decodedBody = decodeQuotedPrintable(body, headerInfo.charset); - } else if (headerInfo.encoding === 'base64') { - decodedBody = decodeBase64(body, headerInfo.charset); - } - - if (headerInfo.contentType.includes('text/html')) { - decodedBody = cleanHtml(decodedBody); - } - - return { - headers, - body: decodedBody - }; + return result; } function extractTextFromHtml(html: string): string {