import { NextResponse } from 'next/server'; import { simpleParser, AddressObject } from 'mailparser'; function getEmailAddress(address: AddressObject | AddressObject[] | undefined): string | null { if (!address) return null; if (Array.isArray(address)) { return address.map(a => a.text).join(', '); } return address.text; } // Clean up the HTML to make it safe but preserve styles function processHtml(html: string | null): string | null { if (!html) return null; try { // Make the content display well in the email context return html // Fix self-closing tags that might break React .replace(/<(br|hr|img|input|link|meta|area|base|col|embed|keygen|param|source|track|wbr)([^>]*)>/gi, '<$1$2 />') // Keep style tags but ensure they're closed properly .replace(/]*)>([\s\S]*?)<\/style>/gi, (match) => { // Just return the matched style tag as-is return match; }); } catch (error) { console.error('Error processing HTML:', error); return html; } } export async function POST(request: Request) { try { const body = await request.json(); const { email } = body; if (!email || typeof email !== 'string') { return NextResponse.json( { error: 'Invalid email content' }, { status: 400 } ); } const parsed = await simpleParser(email); // Process the HTML to preserve styling but make it safe // Handle the case where parsed.html could be a boolean const processedHtml = typeof parsed.html === 'string' ? processHtml(parsed.html) : null; return NextResponse.json({ subject: parsed.subject || null, from: getEmailAddress(parsed.from), to: getEmailAddress(parsed.to), cc: getEmailAddress(parsed.cc), bcc: getEmailAddress(parsed.bcc), date: parsed.date || null, html: processedHtml, text: parsed.textAsHtml || parsed.text || null, attachments: parsed.attachments?.map(att => ({ filename: att.filename, contentType: att.contentType, size: att.size })) || [], headers: parsed.headers || {} }); } catch (error) { console.error('Error parsing email:', error); return NextResponse.json( { error: 'Failed to parse email' }, { status: 500 } ); } }