diff --git a/components/email/ComposeEmail.tsx b/components/email/ComposeEmail.tsx index 85556051..aee4913a 100644 --- a/components/email/ComposeEmail.tsx +++ b/components/email/ComposeEmail.tsx @@ -350,56 +350,12 @@ export default function ComposeEmail(props: ComposeEmailAllProps) { setSubject(subject); - // Format the forwarded message as a simple text representation with clean formatting - let forwardedText = ''; + // For forwarded emails, we'll use a completely different approach + // Just save the original HTML content and use it directly + // This preserves all formatting without trying to parse it - // Format the "From" field - const fromString = Array.isArray(initialEmail.from) && initialEmail.from.length > 0 - ? initialEmail.from.map(addr => addr.name - ? `${addr.name} <${addr.address}>` - : addr.address).join(', ') - : 'Unknown'; - - // Format the "To" field - const toString = Array.isArray(initialEmail.to) && initialEmail.to.length > 0 - ? initialEmail.to.map(addr => addr.name - ? `${addr.name} <${addr.address}>` - : addr.address).join(', ') - : ''; - - // Format the date - const dateString = initialEmail.date - ? typeof initialEmail.date === 'string' - ? new Date(initialEmail.date).toLocaleString() - : initialEmail.date.toLocaleString() - : new Date().toLocaleString(); - - // Create a simple text representation of the forwarded header - forwardedText += '---------- Forwarded message ---------\n'; - forwardedText += `From: ${fromString}\n`; - forwardedText += `Date: ${dateString}\n`; - forwardedText += `Subject: ${subjectBase}\n`; - forwardedText += `To: ${toString}\n\n`; - - // Add the content - clean and sanitize the HTML to prevent formatting issues - if (initialEmail.content || initialEmail.html || initialEmail.text) { - // Try to extract text from HTML content - const tempDiv = document.createElement('div'); - tempDiv.innerHTML = initialEmail.content || initialEmail.html || initialEmail.text || ''; - - // Get the text content, preserving line breaks - let cleanContent = tempDiv.textContent || tempDiv.innerText || ''; - - // Add the clean content - forwardedText += cleanContent; - } else { - forwardedText += 'No content available in original email'; - } - - // Set the forwarded text as the original content (plain text preserves formatting better) - setOriginalContent(forwardedText); - - // Keep user message separate + // Set message parts for the editor + setOriginalContent(initialEmail.content || initialEmail.html || initialEmail.text || ''); setUserMessage(''); setBody(''); } catch (error) { @@ -455,7 +411,7 @@ export default function ComposeEmail(props: ComposeEmailAllProps) { setAttachments(current => current.filter((_, i) => i !== index)); }; - // Send the email + // Modified send handler to combine user message with forwarded content const handleSend = async () => { if (!to) { alert('Please specify at least one recipient'); @@ -465,15 +421,42 @@ export default function ComposeEmail(props: ComposeEmailAllProps) { try { setSending(true); - // Combine user message with original content for forwarded emails - let finalBody = ''; + // Prepare the complete email body + let finalBody = body; if (type === 'forward' && originalContent) { - // Format the user message + forwarded content properly - finalBody = `${userMessage}\n\n${originalContent}`; - } else { - // For other cases, use the current body - finalBody = editorRef.current?.innerHTML || body; + // For forwarded emails, construct the email in a standard format + const fromString = initialEmail?.from?.map(addr => + addr.name ? `${addr.name} <${addr.address}>` : addr.address + ).join(', ') || ''; + + const toString = initialEmail?.to?.map(addr => + addr.name ? `${addr.name} <${addr.address}>` : addr.address + ).join(', ') || ''; + + const dateString = initialEmail?.date + ? typeof initialEmail.date === 'string' + ? new Date(initialEmail.date).toLocaleString() + : initialEmail.date.toLocaleString() + : ''; + + // Combine user message with forwarded header and content + finalBody = ` + ${userMessage} +

+
+
+
+
---------- Forwarded message ---------
+
From: ${fromString}
+
Date: ${dateString}
+
Subject: ${initialEmail?.subject || ''}
+
To: ${toString}
+
+
${originalContent}
+
+
+ `; } await onSend({ @@ -499,8 +482,6 @@ export default function ComposeEmail(props: ComposeEmailAllProps) { if (editorRef.current) { const content = editorRef.current.innerHTML; setUserMessage(content); - - // Simply update body with user message - we'll combine with original content when sending setBody(content); } }; @@ -641,17 +622,14 @@ export default function ComposeEmail(props: ComposeEmailAllProps) { {type !== 'new' && originalContent && (
- {type === 'forward' ? 'Forwarded content' : 'Original message'} + {type === 'forward' ? 'Forwarded content (original)' : 'Original message'} +
+
+
-
-                  {originalContent}
-                
)}
diff --git a/lib/services/email-service.ts b/lib/services/email-service.ts index c02a9dbe..a8b096b5 100644 --- a/lib/services/email-service.ts +++ b/lib/services/email-service.ts @@ -720,7 +720,7 @@ export async function formatEmailForReplyOrForward( /** * Format subject with appropriate prefix (Re:, Fwd:) */ -async function formatSubject(subject: string, type: 'reply' | 'reply-all' | 'forward'): Promise { +export async function formatSubject(subject: string, type: 'reply' | 'reply-all' | 'forward'): Promise { // Clean up any existing prefixes let cleanSubject = subject .replace(/^(Re|Fwd|FW|Forward):\s*/i, '') @@ -743,7 +743,7 @@ async function formatSubject(subject: string, type: 'reply' | 'reply-all' | 'for /** * Create a quote header for reply/forward */ -async function createQuoteHeader(email: EmailMessage): Promise { +export async function createQuoteHeader(email: EmailMessage): Promise { // Format the date const date = new Date(email.date); const formattedDate = date.toLocaleString('en-US', {