diff --git a/components/email/EmailPreview.tsx b/components/email/EmailPreview.tsx index fc4bbc9b..2ce03bcf 100644 --- a/components/email/EmailPreview.tsx +++ b/components/email/EmailPreview.tsx @@ -222,27 +222,46 @@ export default function EmailPreview({ email, loading = false, onReply }: EmailP
-
+
+ {formattedContent ? ( +
+ ) : ( +
+

This email does not contain any content.

+
+ )} +
+ + {process.env.NODE_ENV === 'development' && ( +
+ Email Debug Info +
+

Email ID: {email.id}

+

Content Type: { + typeof email.content === 'object' && email.content?.html + ? 'HTML' + : 'Plain Text' + }

+

Content Size: { + typeof email.content === 'object' + ? `HTML: ${email.content?.html?.length || 0} chars, Text: ${email.content?.text?.length || 0} chars` + : `${typeof email.content === 'string' ? email.content.length : 0} chars` + }

+
+
+ )}
diff --git a/lib/utils/email-content.ts b/lib/utils/email-content.ts index 0c30aa81..b6ceb09a 100644 --- a/lib/utils/email-content.ts +++ b/lib/utils/email-content.ts @@ -46,22 +46,50 @@ export function formatEmailContent(email: any): string { textContent = ''; } + // Log what we found for debugging + console.log(`Email content detected: isHtml=${isHtml}, contentLength=${content.length}, textLength=${textContent.length}`); + // If we have HTML content, sanitize and standardize it if (isHtml && content) { + // Make sure we have a complete HTML structure + const hasHtmlTag = content.includes(' - ${sanitizedContent} + `; } // If we only have text content, format it properly else if (textContent) { + // Check for RTL content and set appropriate direction + const rtlLangPattern = /[\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC]/; + const containsRtlText = rtlLangPattern.test(textContent); + const dirAttribute = containsRtlText ? 'dir="rtl"' : 'dir="ltr"'; + + // Escape HTML characters to prevent XSS + const escapedText = textContent + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"') + .replace(/'/g, '''); + // Format plain text with proper line breaks and paragraphs - const formattedText = textContent + const formattedText = escapedText .replace(/\r\n|\r|\n/g, '
') // Convert all newlines to
.replace(/((?:
){2,})/g, '

') // Convert multiple newlines to paragraphs .replace(/
<\/p>/g, '

') // Fix any

combinations .replace(/


/g, '

'); // Fix any


combinations return ` -