diff --git a/components/ComposeEmail.tsx b/components/ComposeEmail.tsx index 252ceb88..d207721d 100644 --- a/components/ComposeEmail.tsx +++ b/components/ComposeEmail.tsx @@ -174,7 +174,7 @@ export default function ComposeEmail({ setUserMessage(content); // Combine user message with quoted content for the full email body - const combined = `${content}${quotedContent ? quotedContent : ''}`; + const combined = `${content}${quotedContent ? `
]*>([\s\S]*?)<\/blockquote>/i); + if (quoteMatch && quoteMatch[0]) { + setOriginalContent(quoteMatch[0]); + } } // Focus editor after initializing @@ -198,92 +212,23 @@ export default function ComposeEmail({ // Check if we have content to forward if (initialEmail.content || initialEmail.html || initialEmail.text) { - try { - // Use the parse-email API endpoint which centralizes our email parsing logic - const response = await fetch('/api/parse-email', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ - email: initialEmail.content || initialEmail.html || initialEmail.text || '' - }), - }); - - if (response.ok) { - const parsedEmail = await response.json(); - - // Use the parsed HTML content if available - if (parsedEmail.html) { - contentHtml = parsedEmail.html; - console.log('Successfully parsed HTML content'); - } else if (parsedEmail.text) { - // Text-only content is wrapped in pre-formatted styling - contentHtml = `${parsedEmail.text}`; - console.log('Using text content'); - } else { - console.warn('API returned success but no content'); - } - } else { - console.error('API returned error:', await response.text()); - throw new Error('API call failed'); - } - } catch (error) { - console.error('Error parsing email:', error); - - // Fallback processing - using our cleanHtml utility directly - if (initialEmail.html) { - // Import the cleanHtml function dynamically if needed - const { cleanHtml } = await import('@/lib/mail-parser-wrapper'); - contentHtml = cleanHtml(initialEmail.html, { - preserveStyles: true, - scopeStyles: true, - addWrapper: true - }); - console.log('Using direct HTML cleaning fallback'); - } else if (initialEmail.content) { - contentHtml = DOMPurify.sanitize(initialEmail.content, { - ADD_TAGS: ['style'], - FORBID_TAGS: ['script', 'iframe'] - }); - console.log('Using DOMPurify sanitized content'); - } else if (initialEmail.text) { - contentHtml = `${initialEmail.text}`; - console.log('Using plain text fallback'); - } - } - } else { - console.warn('No email content available for forwarding'); + // Use the most complete version of content available + contentHtml = DOMPurify.sanitize( + initialEmail.content || initialEmail.html || + `${initialEmail.text || ''}` + ); } - // Combine the header and content - using containment - const forwardedContent = ` - ${headerHtml} --- `; + // Store the complete forwarded content in state + const forwardedContent = `${headerHtml}- ${contentHtml} --${contentHtml}`; + setOriginalContent(forwardedContent); + + // Set the complete body + setBody(`${forwardedContent}`); - console.log('Setting body with forwarded content'); - setBody(forwardedContent); } catch (error) { - console.error('Error initializing forwarded email:', error); - - // Provide a minimal template even in error case - setBody(` --------------- Forwarded message ----------From: ${initialEmail.from ? formatSender(initialEmail.from) : 'Unknown'}-Date: ${new Date().toLocaleString()}-Subject: ${initialEmail.subject || '(No subject)'}-To: ${initialEmail.to ? formatRecipients(initialEmail.to) : ''}-- Error loading original message content. The original message may still be viewable in your inbox. -- `); + console.error('Error formatting forwarded email:', error); + setBody('Error formatting forwarded email content'); } }; @@ -364,8 +309,14 @@ export default function ComposeEmail({ // Handle editor input const handleEditorInput = (e: React.FormEvent) => { - // Store the HTML content for use in the send function - setBody(e.currentTarget.innerHTML); + if (editorRef.current) { + const content = editorRef.current.innerHTML; + setUserMessage(content); + + // Combine user message with original content for the full email + const combined = `${originalContent}`; + setBody(combined); + } }; // Toggle text direction @@ -377,23 +328,17 @@ export default function ComposeEmail({ }; return ( - - - - --+- {type === 'new' ? 'New Message' : - type === 'reply' ? 'Reply' : - type === 'reply-all' ? 'Reply All' : - 'Forward'} - ++ + + {type === 'new' ? 'New Message' : type === 'forward' ? 'Forward Email' : 'Reply to Email'} - - {/* Email header fields */} + + {/* Recipients, Subject fields remain the same */} - {/* Editor toolbar */} -To: @@ -477,31 +422,52 @@ export default function ComposeEmail({- + {/* Updated Email Body Editor */} ++- {/* Email body editor */} - - - {/* Attachments list */} + {/* Attachments section remains the same */} {attachments.length > 0 && (+ + ++ + {/* Email editor with clear separation between user message and original content */} ++ {/* User's message input area */} + + + {/* Original content display with visual separation - only shown for replies/forwards */} + {type !== 'new' && originalContent && ( +++ )} ++ {type === 'forward' ? 'Forwarded content' : 'Original message'} ++ +Attachments:diff --git a/lib/services/email-service.ts b/lib/services/email-service.ts index a1b05022..ccbb6f0a 100644 --- a/lib/services/email-service.ts +++ b/lib/services/email-service.ts @@ -673,32 +673,36 @@ export function formatEmailForReplyOrForward( const fromText = email.from.map(f => f.name ? `${f.name} <${f.address}>` : f.address).join(', '); const toText = email.to.map(t => t.name ? `${t.name} <${t.address}>` : t.address).join(', '); - // Return specialized body for forward + // Return specialized body for forward with improved styling return { to: '', subject, body: ` -