Neah/lib/server/email-parser.ts
2025-04-26 09:56:48 +02:00

48 lines
1.7 KiB
TypeScript

import { simpleParser } from 'mailparser';
export function cleanHtml(html: string): string {
try {
// More permissive cleaning that preserves styling but removes potentially harmful elements
return html
.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '')
.replace(/<iframe\b[^<]*(?:(?!<\/iframe>)<[^<]*)*<\/iframe>/gi, '')
.replace(/<object\b[^<]*(?:(?!<\/object>)<[^<]*)*<\/object>/gi, '')
.replace(/<embed\b[^<]*(?:(?!<\/embed>)<[^<]*)*<\/embed>/gi, '')
.replace(/<form\b[^<]*(?:(?!<\/form>)<[^<]*)*<\/form>/gi, '')
.replace(/on\w+="[^"]*"/gi, '') // Remove inline event handlers (onclick, onload, etc.)
.replace(/on\w+='[^']*'/gi, '');
} catch (error) {
console.error('Error cleaning HTML:', error);
return html;
}
}
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 ? cleanHtml(parsed.html) : null,
text: parsed.text || null,
attachments: parsed.attachments || [],
headers: Object.fromEntries(parsed.headers)
};
} catch (error) {
console.error('Error parsing email:', error);
throw error;
}
}