From d8cab53806653d0c91102a901c124c94fd477d64 Mon Sep 17 00:00:00 2001 From: alma Date: Mon, 21 Apr 2025 12:43:18 +0200 Subject: [PATCH] mail page rest --- app/api/mail/route.ts | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/app/api/mail/route.ts b/app/api/mail/route.ts index dc898b82..138aff4b 100644 --- a/app/api/mail/route.ts +++ b/app/api/mail/route.ts @@ -53,7 +53,7 @@ export async function GET() { envelope: true, flags: true, bodyStructure: true, - bodyParts: ['TEXT'] + bodyParts: ['HEADER', 'TEXT'] }); const result = []; @@ -63,12 +63,40 @@ export async function GET() { if (message.bodyParts && Array.isArray(message.bodyParts)) { for (const [partType, content] of message.bodyParts) { if (partType === 'text') { - body = content.toString('utf-8'); + // Convert buffer to string and decode if needed + const contentStr = content.toString('utf-8'); + // Check if content is base64 encoded + if (contentStr.includes('Content-Transfer-Encoding: base64')) { + const base64Content = contentStr.split('\r\n\r\n')[1]; + body = Buffer.from(base64Content, 'base64').toString('utf-8'); + } else { + body = contentStr; + } break; } } } + // If no body found, try to get it from the message structure + if (!body && message.bodyStructure) { + try { + const fetch = await client.fetchOne(message.uid.toString(), { + bodyStructure: true, + bodyParts: ['TEXT'] + }); + if (fetch?.bodyParts) { + for (const [partType, content] of fetch.bodyParts) { + if (partType === 'text') { + body = content.toString('utf-8'); + break; + } + } + } + } catch (error) { + console.error('Error fetching message content:', error); + } + } + result.push({ id: message.uid.toString(), accountId: 1, // Default account ID