diff --git a/lib/utils/email-utils.ts b/lib/utils/email-utils.ts index 51e8953e..353062b9 100644 --- a/lib/utils/email-utils.ts +++ b/lib/utils/email-utils.ts @@ -274,47 +274,6 @@ export function formatReplyEmail(originalEmail: EmailMessage | LegacyEmailMessag // Get recipient addresses const { to, cc } = getRecipientAddresses(email, type); - // Get email content and sanitize it - const originalContent = email.content; - - // Extract text and html content - let htmlContent = ''; - let textContent = ''; - let direction: 'ltr' | 'rtl' = 'ltr'; - - // Handle different content formats - if (typeof originalContent === 'string') { - console.log('formatReplyEmail: content is string, length:', originalContent.length); - // Simple string content - textContent = originalContent; - const isHtml = isHtmlContent(originalContent); - if (isHtml) { - htmlContent = originalContent; - } else { - // If it's plain text, convert to HTML - htmlContent = formatPlainTextToHtml(originalContent); - } - } - else if (originalContent) { - console.log('formatReplyEmail: content is object:', { - hasHtml: !!originalContent.html, - htmlLength: originalContent.html?.length || 0, - hasText: !!originalContent.text, - textLength: originalContent.text?.length || 0, - direction: originalContent.direction - }); - - // Standard EmailContent object - htmlContent = originalContent.html || ''; - textContent = originalContent.text || ''; - direction = originalContent.direction || 'ltr' as const; - - // If no HTML but has text, convert text to HTML - if (!htmlContent && textContent) { - htmlContent = formatPlainTextToHtml(textContent); - } - } - // Get quote header const { fromStr, dateStr } = getFormattedHeaderInfo(email); @@ -322,31 +281,27 @@ export function formatReplyEmail(originalEmail: EmailMessage | LegacyEmailMessag const sender = fromStr; const date = dateStr; - // Create the quoted reply content - if (htmlContent) { - // Format HTML reply - console.log('Formatting HTML reply, quoted content length:', htmlContent.length); - const sanitizedReplyContent = sanitizeHtml(htmlContent); - console.log('Sanitized reply content length:', sanitizedReplyContent.length); - console.log('Sanitized reply content truncated sample:', - sanitizedReplyContent.length > 100 - ? sanitizedReplyContent.substring(0, 100) + '...' - : sanitizedReplyContent || 'EMPTY'); - - htmlContent = ` -
- On ${date}, ${sender} wrote: -
-
- ${sanitizedReplyContent} -
- `; - } + // Process the original content using formatEmailContent for consistent rendering + const { text: textContent, html: originalHtmlContent } = extractEmailContent(email.content); + // Get the properly formatted content using the same utility as the preview panel + const processedContent = formatEmailContent(email.content); + + // Create the quoted reply HTML + const htmlContent = ` +
+ On ${date}, ${sender} wrote: +
+
+ ${processedContent} +
+ `; + + // Format plain text reply if available + let formattedTextContent = ''; if (textContent) { - // Format plain text reply const lines = textContent.split(/\r\n|\r|\n/); - textContent = `On ${date}, ${sender} wrote:\n\n${lines.map(line => `> ${line}`).join('\n')}`; + formattedTextContent = `On ${date}, ${sender} wrote:\n\n${lines.map(line => `> ${line}`).join('\n')}`; } const result = { @@ -355,9 +310,9 @@ export function formatReplyEmail(originalEmail: EmailMessage | LegacyEmailMessag subject, content: { html: htmlContent, - text: textContent, + text: formattedTextContent || textContent, isHtml: true, - direction, + direction: typeof email.content === 'object' && email.content ? email.content.direction || 'ltr' : 'ltr', }, attachments: email.attachments?.map(att => { // Create properly typed attachment @@ -419,106 +374,52 @@ export function formatForwardedEmail(originalEmail: EmailMessage | LegacyEmailMe // Original sent date const date = dateStr; - // Get email content - const originalContent = email.content; + // Process the original content using formatEmailContent for consistent rendering + const { text: textContent } = extractEmailContent(email.content); - // Extract text and html content - let htmlContent = ''; - let textContent = ''; - let direction: 'ltr' | 'rtl' = 'ltr'; + // Get the properly formatted content using the same utility as the preview panel + const processedContent = formatEmailContent(email.content); - // Handle different content formats - if (typeof originalContent === 'string') { - console.log('formatForwardedEmail: content is string, length:', originalContent.length); - // Simple string content - textContent = originalContent; - const isHtml = isHtmlContent(originalContent); - if (isHtml) { - htmlContent = originalContent; - } else { - // If it's plain text, convert to HTML - htmlContent = formatPlainTextToHtml(originalContent); - } - } - else if (originalContent) { - console.log('formatForwardedEmail: content is object:', { - hasHtml: !!originalContent.html, - htmlLength: originalContent.html?.length || 0, - hasText: !!originalContent.text, - textLength: originalContent.text?.length || 0, - direction: originalContent.direction - }); - - // Standard EmailContent object - htmlContent = originalContent.html || ''; - textContent = originalContent.text || ''; - direction = originalContent.direction || 'ltr' as const; - - // If no HTML but has text, convert text to HTML - if (!htmlContent && textContent) { - htmlContent = formatPlainTextToHtml(textContent); - } - } - - // Create the forwarded email HTML content - if (htmlContent) { - console.log('Formatting HTML forward, original content length:', htmlContent.length); - - // Important: First sanitize the content portion only - const sanitizedReplyContent = sanitizeHtml(htmlContent); - console.log('Sanitized forward content length:', sanitizedReplyContent.length); - console.log('Sanitized forward content truncated sample:', - sanitizedReplyContent.length > 100 - ? sanitizedReplyContent.substring(0, 100) + '...' - : sanitizedReplyContent || 'EMPTY'); - - // Create the complete forwarded email with header info - const fullForwardedEmail = ` -
- ---------- Forwarded message ----------
- - - - - - - - - - - - - - - - - - - ${ccStr ? ` - - - - - ` : ''} - -
From:${fromStr}
Date:${date}
Subject:${email.subject || ''}
To:${toStr}
Cc:${ccStr}
-
-
- ${sanitizedReplyContent} -
- `; - - // Now we have the full forwarded email structure without sanitizing it again - htmlContent = fullForwardedEmail; - - console.log('Final forward HTML content length:', htmlContent.length, - 'contains table:', htmlContent.includes(' + ---------- Forwarded message ----------
+ + + + + + + + + + + + + + + + + + + ${ccStr ? ` + + + + + ` : ''} + +
From:${fromStr}
Date:${date}
Subject:${email.subject || ''}
To:${toStr}
Cc:${ccStr}
+ +
+ ${processedContent} +
+ `; - // Format the plain text version + // Format plain text version + let formattedTextContent = ''; if (textContent) { - textContent = ` + formattedTextContent = ` ---------- Forwarded message ---------- From: ${fromStr} Date: ${date} @@ -535,9 +436,9 @@ ${textContent} subject, content: { html: htmlContent, - text: textContent, + text: formattedTextContent || textContent, isHtml: true, - direction, + direction: typeof email.content === 'object' && email.content ? email.content.direction || 'ltr' : 'ltr', }, attachments: email.attachments?.map(att => { // Create properly typed attachment