import { simpleParser, ParsedMail, Attachment, HeaderValue, AddressObject } from 'mailparser'; export interface DecodedEmail { html: string | false; text: string | false; attachments: Attachment[]; headers: Map; subject: string; from: string; to: string; date: Date; } function getAddressText(address: AddressObject | AddressObject[] | undefined): string { if (!address) return ''; if (Array.isArray(address)) { return address.map(addr => addr.value?.[0]?.address || '').filter(Boolean).join(', '); } return address.value?.[0]?.address || ''; } export async function decodeEmail(rawEmail: string): Promise { try { const parsed = await simpleParser(rawEmail); return { html: parsed.html || false, text: parsed.text || false, attachments: parsed.attachments || [], headers: parsed.headers, subject: parsed.subject || '', from: getAddressText(parsed.from), to: getAddressText(parsed.to), date: parsed.date || new Date() }; } catch (error) { console.error('Error decoding email:', error); throw new Error('Failed to decode email'); } } export function cleanHtml(html: string): string { if (!html) return ''; // Detect text direction from the content const hasRtlChars = /[\u0591-\u07FF\u200F\u202B\u202E\uFB1D-\uFDFD\uFE70-\uFEFC]/.test(html); const defaultDir = hasRtlChars ? 'rtl' : 'ltr'; // Basic HTML cleaning while preserving structure const cleaned = html // Remove script and style tags .replace(/]*>[\s\S]*?<\/script>/gi, '') .replace(/]*>[\s\S]*?<\/style>/gi, '') // Remove meta tags .replace(/]*>/gi, '') // Remove head and title .replace(/]*>[\s\S]*?<\/head>/gi, '') .replace(/]*>[\s\S]*?<\/title>/gi, '') // Remove body tags .replace(/]*>/gi, '') .replace(/<\/body>/gi, '') // Remove html tags .replace(/]*>/gi, '') .replace(/<\/html>/gi, '') // Clean up whitespace .replace(/\s+/g, ' ') .trim(); // Wrap in a div with the detected direction return `
${cleaned}
`; }