mail page rest

This commit is contained in:
alma 2025-04-21 15:32:35 +02:00
parent faa43d8ff4
commit dc081b1b6d

View File

@ -231,9 +231,9 @@ function decodeMIME(text: string, encoding?: string, charset: string = 'utf-8'):
}
}
function extractHtmlBody(htmlContent: string): string {
const bodyMatch = htmlContent.match(/<body[^>]*>([\s\S]*?)<\/body>/i);
return bodyMatch ? bodyMatch[1] : htmlContent;
function extractHtmlBody(html: string): string {
const bodyMatch = html.match(/<body[^>]*>([\s\S]*?)<\/body>/i);
return bodyMatch ? bodyMatch[1] : html;
}
function decodeMimeContent(content: string): string {
@ -271,91 +271,78 @@ function decodeMimeContent(content: string): string {
}
function renderEmailContent(email: Email) {
if (!email.body) return null;
try {
// Parse the full email content
// Parse the email content using our MIME decoder
const parsed = parseFullEmail(email.body);
// If we have HTML content, display it
// If we have HTML content, render it
if (parsed.html) {
return (
<div className="prose max-w-none">
<div
dangerouslySetInnerHTML={{
__html: parsed.html
.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, '')
.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '')
.replace(/<base[^>]*>/gi, '')
.replace(/<meta[^>]*>/gi, '')
.replace(/<link[^>]*>/gi, '')
.replace(/<title[^>]*>[\s\S]*?<\/title>/gi, '')
.replace(/<head[^>]*>[\s\S]*?<\/head>/gi, '')
.replace(/<body[^>]*>/gi, '')
.replace(/<\/body>/gi, '')
.replace(/<html[^>]*>/gi, '')
.replace(/<\/html>/gi, '')
}}
/>
<div className="email-content">
<div className="prose max-w-none" dangerouslySetInnerHTML={{ __html: parsed.html }} />
{parsed.attachments && parsed.attachments.length > 0 && (
<div className="mt-4">
<h3 className="text-sm font-medium mb-2">Attachments:</h3>
<ul className="space-y-2">
{parsed.attachments.map((attachment, index) => (
<li key={index} className="flex items-center gap-2">
<Paperclip className="h-4 w-4 text-muted-foreground" />
<span className="text-sm">{attachment.filename}</span>
</li>
))}
</ul>
</div>
)}
</div>
);
}
// If we have text content, display it with proper formatting
// If we have text content, render it
if (parsed.text) {
return (
<div className="whitespace-pre-wrap">
{parsed.text}
</div>
);
}
// If we have attachments, display them
if (parsed.attachments && parsed.attachments.length > 0) {
return (
<div>
<div className="mb-4">
{parsed.attachments.map((attachment, index) => (
<div key={index} className="flex items-center gap-2 p-2 border rounded">
<span className="text-sm">{attachment.filename}</span>
<span className="text-xs text-muted-foreground">
({attachment.contentType})
</span>
</div>
<div className="email-content">
<div className="whitespace-pre-wrap font-sans text-base leading-relaxed">
{parsed.text.split('\n').map((line, i) => (
<p key={i} className="mb-2">{line}</p>
))}
</div>
{parsed.body && (
<div className="whitespace-pre-wrap">
{parsed.body}
{parsed.attachments && parsed.attachments.length > 0 && (
<div className="mt-4">
<h3 className="text-sm font-medium mb-2">Attachments:</h3>
<ul className="space-y-2">
{parsed.attachments.map((attachment, index) => (
<li key={index} className="flex items-center gap-2">
<Paperclip className="h-4 w-4 text-muted-foreground" />
<span className="text-sm">{attachment.filename}</span>
</li>
))}
</ul>
</div>
)}
</div>
);
}
// If parsing failed, try to clean and display the raw body
const cleanedBody = email.body
.replace(/Content-Type:.*?\r\n/g, '')
.replace(/Content-Transfer-Encoding:.*?\r\n/g, '')
.replace(/MIME-Version:.*?\r\n/g, '')
.replace(/--.*?--\r\n/g, '')
.replace(/--.*?\r\n/g, '')
.replace(/=\r\n/g, '')
.replace(/=3D/g, '=')
.replace(/=20/g, ' ')
.replace(/=09/g, '\t')
.replace(/=0A/g, '\n')
.replace(/=0D/g, '\r')
.replace(/=([0-9A-F]{2})/g, (_, hex) => String.fromCharCode(parseInt(hex, 16)));
// If we couldn't parse the content, try to decode and clean the raw body
const decodedBody = decodeMIME(email.body, 'quoted-printable', 'utf-8');
const cleanedContent = cleanHtml(decodedBody);
return (
<div className="whitespace-pre-wrap">
{cleanedBody}
<div className="email-content">
<div className="whitespace-pre-wrap font-sans text-base leading-relaxed">
{cleanedContent.split('\n').map((line, i) => (
<p key={i} className="mb-2">{line}</p>
))}
</div>
</div>
);
} catch (error) {
console.error('Error rendering email content:', error);
return (
<div className="text-muted-foreground">
Unable to display email content
<div className="email-content">
<pre className="whitespace-pre-wrap text-sm">{email.body}</pre>
</div>
);
}