mail page rest

This commit is contained in:
alma 2025-04-21 15:16:24 +02:00
parent 269b971ac6
commit c039291aea

View File

@ -74,6 +74,13 @@ interface Attachment {
interface ParsedEmailContent { interface ParsedEmailContent {
headers: string; headers: string;
body: string; body: string;
html?: string;
text?: string;
attachments?: Array<{
filename: string;
content: string;
contentType: string;
}>;
} }
interface ParsedEmailMetadata { interface ParsedEmailMetadata {
@ -245,89 +252,79 @@ function decodeMimeContent(content: string): string {
} }
function renderEmailContent(email: Email) { function renderEmailContent(email: Email) {
console.log('=== renderEmailContent Debug ===');
console.log('Email ID:', email.id);
console.log('Subject:', email.subject);
console.log('Body length:', email.body.length);
console.log('First 100 chars:', email.body.substring(0, 100));
try { try {
// First try to parse the full email // First try to parse the full email
const parsed = parseFullEmail(email.body); const parsed = parseFullEmail(email.body);
console.log('Parsed content:', {
hasText: !!parsed.body, // If we have HTML content, display it
hasHtml: !!parsed.headers, if (parsed.html) {
hasAttachments: parsed.headers.length > 0 return (
}); <div
className="prose prose-sm sm:prose lg:prose-lg xl:prose-xl dark:prose-invert max-w-none"
// Determine content and type dangerouslySetInnerHTML={{
let content = ''; __html: parsed.html
let isHtml = false; .replace(/<style[^>]*>[\s\S]*?<\/style>/gi, '')
.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '')
if (parsed.headers) { .replace(/<base[^>]*>/gi, '')
// Use our existing MIME decoding for HTML content .replace(/<meta[^>]*>/gi, '')
content = decodeMIME(parsed.headers, 'quoted-printable', 'utf-8'); .replace(/<link[^>]*>/gi, '')
isHtml = true; .replace(/<title[^>]*>[\s\S]*?<\/title>/gi, '')
} else if (parsed.body) { .replace(/<head[^>]*>[\s\S]*?<\/head>/gi, '')
// Use our existing MIME decoding for plain text content .replace(/<body[^>]*>/gi, '')
content = decodeMIME(parsed.body, 'quoted-printable', 'utf-8'); .replace(/<\/body>/gi, '')
isHtml = false; .replace(/<html[^>]*>/gi, '')
} else { .replace(/<\/html>/gi, '')
// Try to extract content directly from body using our existing functions }}
const htmlMatch = email.body.match(/<html[^>]*>[\s\S]*?<\/html>/i); />
if (htmlMatch) { );
content = decodeMIME(htmlMatch[0], 'quoted-printable', 'utf-8');
isHtml = true;
} else {
// Use our existing text extraction function
content = extractTextFromHtml(email.body);
isHtml = false;
}
} }
if (!content) { // If we have text content, display it
console.log('No content available after all attempts'); if (parsed.text) {
return <div className="text-gray-500">No content available</div>; return (
<div className="whitespace-pre-wrap font-sans text-base leading-relaxed">
{parsed.text.split('\n').map((line: string, i: number) => (
<p key={i} className="mb-2">{line}</p>
))}
</div>
);
} }
// Handle attachments // If we have attachments, display them
const attachmentElements = parsed.headers.split('\n').filter(header => header.startsWith('Content-Type:')).map((header, index) => ( if (parsed.attachments && parsed.attachments.length > 0) {
<div key={index} className="mt-4 p-4 border rounded-lg bg-gray-50"> return (
<div className="flex items-center"> <div className="mt-6 border-t border-gray-200 pt-6">
<Paperclip className="h-5 w-5 text-gray-400 mr-2" /> <h3 className="text-sm font-semibold text-gray-900 mb-4">Attachments</h3>
<span className="text-sm text-gray-600">{header.split(': ')[1]}</span> <div className="space-y-2">
</div> {parsed.attachments.map((attachment: { filename: string }, index: number) => (
</div> <div key={index} className="flex items-center space-x-2 p-2 border rounded">
)); <Paperclip className="h-4 w-4 text-gray-400" />
<span className="text-sm text-gray-600 truncate">
return ( {attachment.filename}
<div className="prose max-w-none dark:prose-invert"> </span>
{isHtml ? ( </div>
<div
className="prose prose-sm sm:prose lg:prose-lg xl:prose-xl dark:prose-invert max-w-none"
dangerouslySetInnerHTML={{
__html: content
.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="whitespace-pre-wrap font-sans text-base leading-relaxed">
{content.split('\n').map((line, i) => (
<p key={i} className="mb-2">{line}</p>
))} ))}
</div> </div>
)} </div>
{attachmentElements} );
}
// If we couldn't parse the content, try to clean and display the raw body
const cleanedContent = email.body
.replace(/Content-Type:[^\n]+/g, '')
.replace(/Content-Transfer-Encoding:[^\n]+/g, '')
.replace(/MIME-Version:[^\n]+/g, '')
.replace(/--[a-zA-Z0-9]+(-[a-zA-Z0-9]+)?/g, '')
.replace(/boundary=[^\n]+/g, '')
.replace(/charset=[^\n]+/g, '')
.replace(/[\r\n]+/g, '\n')
.trim();
return (
<div className="whitespace-pre-wrap font-sans text-base leading-relaxed">
{cleanedContent.split('\n').map((line: string, i: number) => (
<p key={i} className="mb-2">{line}</p>
))}
</div> </div>
); );
} catch (e) { } catch (e) {