courrier preview

This commit is contained in:
alma 2025-05-01 20:23:52 +02:00
parent 2900e0d188
commit c1aa1e8f10

View File

@ -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 = `
<div style="margin: 20px 0 10px 0; color: #666; border-bottom: 1px solid #ddd; padding-bottom: 5px;">
On ${date}, ${sender} wrote:
</div>
<blockquote style="margin: 0; padding-left: 10px; border-left: 3px solid #ddd; color: #505050; background-color: #f9f9f9; padding: 10px;">
${sanitizedReplyContent}
</blockquote>
`;
}
// 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 = `
<div style="margin: 20px 0 10px 0; color: #666; border-bottom: 1px solid #ddd; padding-bottom: 5px;">
On ${date}, ${sender} wrote:
</div>
<blockquote style="margin: 0; padding-left: 10px; border-left: 3px solid #ddd; color: #505050; background-color: #f9f9f9; padding: 10px;">
${processedContent}
</blockquote>
`;
// 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 = `
<div style="margin: 20px 0 10px 0; color: #666; font-family: Arial, sans-serif;">
---------- Forwarded message ----------<br>
<table style="margin: 10px 0 15px 0; border-collapse: collapse; font-size: 13px; color: #333;">
<tbody>
<tr>
<td style="padding: 3px 10px 3px 0; font-weight: bold; text-align: right; vertical-align: top;">From:</td>
<td style="padding: 3px 0;">${fromStr}</td>
</tr>
<tr>
<td style="padding: 3px 10px 3px 0; font-weight: bold; text-align: right; vertical-align: top;">Date:</td>
<td style="padding: 3px 0;">${date}</td>
</tr>
<tr>
<td style="padding: 3px 10px 3px 0; font-weight: bold; text-align: right; vertical-align: top;">Subject:</td>
<td style="padding: 3px 0;">${email.subject || ''}</td>
</tr>
<tr>
<td style="padding: 3px 10px 3px 0; font-weight: bold; text-align: right; vertical-align: top;">To:</td>
<td style="padding: 3px 0;">${toStr}</td>
</tr>
${ccStr ? `
<tr>
<td style="padding: 3px 10px 3px 0; font-weight: bold; text-align: right; vertical-align: top;">Cc:</td>
<td style="padding: 3px 0;">${ccStr}</td>
</tr>
` : ''}
</tbody>
</table>
</div>
<blockquote style="margin: 0; padding-left: 10px; border-left: 3px solid #ddd; color: #505050; background-color: #f9f9f9; padding: 10px;">
${sanitizedReplyContent}
</blockquote>
`;
// 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('<table'),
'contains forwarded message:', htmlContent.includes('---------- Forwarded message ----------'));
}
// Create the forwarded email HTML content with the properly processed content
const htmlContent = `
<div style="margin: 20px 0 10px 0; color: #666; font-family: Arial, sans-serif;">
---------- Forwarded message ----------<br>
<table style="margin: 10px 0 15px 0; border-collapse: collapse; font-size: 13px; color: #333;">
<tbody>
<tr>
<td style="padding: 3px 10px 3px 0; font-weight: bold; text-align: right; vertical-align: top;">From:</td>
<td style="padding: 3px 0;">${fromStr}</td>
</tr>
<tr>
<td style="padding: 3px 10px 3px 0; font-weight: bold; text-align: right; vertical-align: top;">Date:</td>
<td style="padding: 3px 0;">${date}</td>
</tr>
<tr>
<td style="padding: 3px 10px 3px 0; font-weight: bold; text-align: right; vertical-align: top;">Subject:</td>
<td style="padding: 3px 0;">${email.subject || ''}</td>
</tr>
<tr>
<td style="padding: 3px 10px 3px 0; font-weight: bold; text-align: right; vertical-align: top;">To:</td>
<td style="padding: 3px 0;">${toStr}</td>
</tr>
${ccStr ? `
<tr>
<td style="padding: 3px 10px 3px 0; font-weight: bold; text-align: right; vertical-align: top;">Cc:</td>
<td style="padding: 3px 0;">${ccStr}</td>
</tr>
` : ''}
</tbody>
</table>
</div>
<blockquote style="margin: 0; padding-left: 10px; border-left: 3px solid #ddd; color: #505050; background-color: #f9f9f9; padding: 10px;">
${processedContent}
</blockquote>
`;
// 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