From 6af27f906580ee3fbab151b3fbfaf422f64225ec Mon Sep 17 00:00:00 2001 From: alma Date: Tue, 15 Apr 2025 22:01:57 +0200 Subject: [PATCH] mail page imap connection 15 --- app/mail/page.tsx | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/app/mail/page.tsx b/app/mail/page.tsx index b911bb8..027d9c3 100644 --- a/app/mail/page.tsx +++ b/app/mail/page.tsx @@ -42,30 +42,56 @@ interface Email { } // Improved MIME decoding function for all emails -const decodeMimeContent = (content: string) => { +const decodeMimeContent = (content: string): string => { try { - // Remove MIME headers and metadata - let cleanContent = content - // Remove MIME headers + // Handle forwarded message headers specially + if (content.includes('-------- Forwarded message ----------')) { + const parts: string[] = content.split('-------- Forwarded message ----------'); + if (parts.length > 1) { + // Clean the forwarded headers section + const headers = parts[1].split('\n') + .filter(line => line.trim()) + .map(line => line.replace(/=\n/g, '')) + .map(line => line.replace(/=([0-9A-F]{2})/g, (_, hex) => String.fromCharCode(parseInt(hex, 16)))) + .join('\n'); + + // Clean the message body + const messageBody = parts[1].split('\n\n').slice(1).join('\n\n') + .replace(/^This is a multi-part message.*?charset="utf-8"/s, '') + .replace(/---InfomaniakPhpMail.*?Content-Transfer-Encoding:.*?\n/g, '') + .replace(/Content-Type:.*?\n/g, '') + .replace(/Content-Transfer-Encoding:.*?\n/g, '') + .replace(/=C2=A0/g, ' ') // non-breaking space + .replace(/=E2=80=(93|94)/g, '-') // dashes + .replace(/=\n/g, '') // soft line breaks + .replace(/=([0-9A-F]{2})/g, (_, hex) => String.fromCharCode(parseInt(hex, 16))) + .replace(/\[IMG:(.*?)\]/g, '') // remove image placeholders + .replace(/\[ ?LINK: ([^\]]+?) ?\]/g, '$1') // clean up links + .replace(/\*(.*?)\*/g, '$1') // remove asterisk formatting + .replace(/\n{3,}/g, '\n\n') // reduce multiple line breaks + .replace(/^\s+|\s+$/gm, '') // trim each line + .trim(); + + return `-------- Forwarded message ----------\n${headers}\n\n${messageBody}`; + } + } + + // Regular email content cleaning + return content .replace(/^This is a multi-part message.*?charset="utf-8"/s, '') .replace(/---InfomaniakPhpMail.*?Content-Transfer-Encoding:.*?\n/g, '') .replace(/Content-Type:.*?\n/g, '') .replace(/Content-Transfer-Encoding:.*?\n/g, '') - // Clean up special characters and encoding .replace(/=C2=A0/g, ' ') // non-breaking space .replace(/=E2=80=(93|94)/g, '-') // dashes .replace(/=\n/g, '') // soft line breaks .replace(/=([0-9A-F]{2})/g, (_, hex) => String.fromCharCode(parseInt(hex, 16))) - // Format links and content markers .replace(/\[IMG:(.*?)\]/g, '') // remove image placeholders - .replace(/\[ ?LINK: ([^\]]+?) ?\]/g, (_, url) => url) // clean up links + .replace(/\[ ?LINK: ([^\]]+?) ?\]/g, '$1') // clean up links .replace(/\*(.*?)\*/g, '$1') // remove asterisk formatting - // Clean up whitespace .replace(/\n{3,}/g, '\n\n') // reduce multiple line breaks .replace(/^\s+|\s+$/gm, '') // trim each line .trim(); - - return cleanContent; } catch (error) { console.error('Error decoding MIME content:', error); return content;