courrier clean 2

This commit is contained in:
alma 2025-04-26 14:54:26 +02:00
parent 051bcb08a4
commit 24bafdcce4
2 changed files with 49 additions and 71 deletions

View File

@ -350,56 +350,12 @@ export default function ComposeEmail(props: ComposeEmailAllProps) {
setSubject(subject); setSubject(subject);
// Format the forwarded message as a simple text representation with clean formatting // For forwarded emails, we'll use a completely different approach
let forwardedText = ''; // Just save the original HTML content and use it directly
// This preserves all formatting without trying to parse it
// Format the "From" field // Set message parts for the editor
const fromString = Array.isArray(initialEmail.from) && initialEmail.from.length > 0 setOriginalContent(initialEmail.content || initialEmail.html || initialEmail.text || '');
? 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
setUserMessage(''); setUserMessage('');
setBody(''); setBody('');
} catch (error) { } catch (error) {
@ -455,7 +411,7 @@ export default function ComposeEmail(props: ComposeEmailAllProps) {
setAttachments(current => current.filter((_, i) => i !== index)); setAttachments(current => current.filter((_, i) => i !== index));
}; };
// Send the email // Modified send handler to combine user message with forwarded content
const handleSend = async () => { const handleSend = async () => {
if (!to) { if (!to) {
alert('Please specify at least one recipient'); alert('Please specify at least one recipient');
@ -465,15 +421,42 @@ export default function ComposeEmail(props: ComposeEmailAllProps) {
try { try {
setSending(true); setSending(true);
// Combine user message with original content for forwarded emails // Prepare the complete email body
let finalBody = ''; let finalBody = body;
if (type === 'forward' && originalContent) { if (type === 'forward' && originalContent) {
// Format the user message + forwarded content properly // For forwarded emails, construct the email in a standard format
finalBody = `${userMessage}\n\n${originalContent}`; const fromString = initialEmail?.from?.map(addr =>
} else { addr.name ? `${addr.name} <${addr.address}>` : addr.address
// For other cases, use the current body ).join(', ') || '';
finalBody = editorRef.current?.innerHTML || body;
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}
<br><br>
<div style="border-top: 1px solid #ccc; margin-top: 20px; padding-top: 10px;">
<div style="font-family: Arial, sans-serif; color: #333;">
<div style="margin-bottom: 15px;">
<div>---------- Forwarded message ---------</div>
<div><b>From:</b> ${fromString}</div>
<div><b>Date:</b> ${dateString}</div>
<div><b>Subject:</b> ${initialEmail?.subject || ''}</div>
<div><b>To:</b> ${toString}</div>
</div>
<div>${originalContent}</div>
</div>
</div>
`;
} }
await onSend({ await onSend({
@ -499,8 +482,6 @@ export default function ComposeEmail(props: ComposeEmailAllProps) {
if (editorRef.current) { if (editorRef.current) {
const content = editorRef.current.innerHTML; const content = editorRef.current.innerHTML;
setUserMessage(content); setUserMessage(content);
// Simply update body with user message - we'll combine with original content when sending
setBody(content); setBody(content);
} }
}; };
@ -641,17 +622,14 @@ export default function ComposeEmail(props: ComposeEmailAllProps) {
{type !== 'new' && originalContent && ( {type !== 'new' && originalContent && (
<div className="border-t"> <div className="border-t">
<div className="px-4 py-2 bg-gray-50 text-xs font-medium text-gray-500"> <div className="px-4 py-2 bg-gray-50 text-xs font-medium text-gray-500">
{type === 'forward' ? 'Forwarded content' : 'Original message'} {type === 'forward' ? 'Forwarded content (original)' : 'Original message'}
</div>
<div className="p-4 bg-gray-50 border-t overflow-auto max-h-[400px]">
<div
className="forwarded-content text-sm"
dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(originalContent) }}
/>
</div> </div>
<pre
className="p-4 bg-gray-50 text-sm original-content font-mono whitespace-pre-wrap overflow-auto"
style={{
lineHeight: '1.5',
maxHeight: '400px'
}}
>
{originalContent}
</pre>
</div> </div>
)} )}
</div> </div>

View File

@ -720,7 +720,7 @@ export async function formatEmailForReplyOrForward(
/** /**
* Format subject with appropriate prefix (Re:, Fwd:) * Format subject with appropriate prefix (Re:, Fwd:)
*/ */
async function formatSubject(subject: string, type: 'reply' | 'reply-all' | 'forward'): Promise<string> { export async function formatSubject(subject: string, type: 'reply' | 'reply-all' | 'forward'): Promise<string> {
// Clean up any existing prefixes // Clean up any existing prefixes
let cleanSubject = subject let cleanSubject = subject
.replace(/^(Re|Fwd|FW|Forward):\s*/i, '') .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 * Create a quote header for reply/forward
*/ */
async function createQuoteHeader(email: EmailMessage): Promise<string> { export async function createQuoteHeader(email: EmailMessage): Promise<string> {
// Format the date // Format the date
const date = new Date(email.date); const date = new Date(email.date);
const formattedDate = date.toLocaleString('en-US', { const formattedDate = date.toLocaleString('en-US', {