Neah/lib/server/email-parser.ts
2025-04-26 11:36:35 +02:00

45 lines
1.6 KiB
TypeScript

import { simpleParser } from 'mailparser';
import { cleanHtml as cleanHtmlCentralized } from '@/lib/mail-parser-wrapper';
/**
* @deprecated This function is deprecated and will be removed in future versions.
* Use the centralized cleanHtml function from '@/lib/mail-parser-wrapper.ts instead.
* This is maintained only for backward compatibility.
*/
export function cleanHtml(html: string): string {
console.warn('The cleanHtml function in lib/server/email-parser.ts is deprecated. Use the one in lib/mail-parser-wrapper.ts');
return cleanHtmlCentralized(html, { preserveStyles: true, scopeStyles: false });
}
function getAddressText(address: any): string | null {
if (!address) return null;
if (Array.isArray(address)) {
return address.map(addr => addr.value?.[0]?.address || '').filter(Boolean).join(', ');
}
return address.value?.[0]?.address || null;
}
export async function parseEmail(emailContent: string) {
try {
const parsed = await simpleParser(emailContent);
return {
subject: parsed.subject || null,
from: getAddressText(parsed.from),
to: getAddressText(parsed.to),
cc: getAddressText(parsed.cc),
bcc: getAddressText(parsed.bcc),
date: parsed.date || null,
html: parsed.html ? cleanHtmlCentralized(parsed.html as string, {
preserveStyles: true,
scopeStyles: false
}) : null,
text: parsed.text || null,
attachments: parsed.attachments || [],
headers: Object.fromEntries(parsed.headers)
};
} catch (error) {
console.error('Error parsing email:', error);
throw error;
}
}