mail page rest

This commit is contained in:
alma 2025-04-21 13:27:20 +02:00
parent d88ffc12da
commit 28612fb39e

View File

@ -436,7 +436,7 @@ function cleanHtml(html: string): string {
function decodeMimeContent(content: string): string { function decodeMimeContent(content: string): string {
if (!content) return ''; if (!content) return '';
// Check if this is an Infomaniak multipart message // Check if this is a multipart message
if (content.includes('Content-Type: multipart/')) { if (content.includes('Content-Type: multipart/')) {
const boundary = content.match(/boundary="([^"]+)"/)?.[1]; const boundary = content.match(/boundary="([^"]+)"/)?.[1];
if (boundary) { if (boundary) {
@ -474,93 +474,42 @@ function renderEmailContent(email: Email) {
console.log('Body length:', email.body.length); console.log('Body length:', email.body.length);
console.log('First 100 chars:', email.body.substring(0, 100)); console.log('First 100 chars:', email.body.substring(0, 100));
const parsedContent = parseFullEmail(email.body); try {
console.log('Parsed content:', { const parsed = parseFullEmail(email.body);
hasText: !!parsedContent.text, console.log('Parsed content:', {
hasHtml: !!parsedContent.html, hasText: !!parsed.text,
hasAttachments: parsedContent.attachments.length > 0 hasHtml: !!parsed.html,
}); hasAttachments: parsed.attachments.length > 0
});
// Determine content type and get content // Display HTML content if available, otherwise fallback to text
let content = null; const content = parsed.html || parsed.text || decodeMimeContent(email.body);
let isHtml = false;
if (!content) {
if (parsedContent.html) { console.log('No content available after all attempts');
content = parsedContent.html; return <div className="text-gray-500">No content available</div>;
isHtml = true;
} else if (parsedContent.text) {
content = parsedContent.text;
isHtml = false;
}
if (!content) {
console.log('No content available from parsing, trying direct body');
// Try to extract content directly from body
const htmlMatch = email.body.match(/<html[^>]*>[\s\S]*?<\/html>/i);
if (htmlMatch) {
content = htmlMatch[0];
isHtml = true;
} else {
content = email.body
.replace(/<[^>]+>/g, '')
.replace(/&nbsp;/g, ' ')
.replace(/&amp;/g, '&')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&quot;/g, '"')
.replace(/\r\n/g, '\n')
.replace(/=\n/g, '')
.replace(/=3D/g, '=')
.replace(/=09/g, '\t')
.trim();
isHtml = false;
} }
}
if (!content) { // Handle attachments
console.log('No content available after all attempts'); const attachmentElements = parsed.attachments.map((attachment, index) => (
return <div className="text-gray-500">No content available</div>; <div key={index} className="mt-4 p-4 border rounded-lg bg-gray-50">
} <div className="flex items-center">
<Paperclip className="h-5 w-5 text-gray-400 mr-2" />
// Clean and sanitize content <span className="text-sm text-gray-600">{attachment.filename}</span>
const sanitizedContent = isHtml ? </div>
cleanEmailContent(content) :
content.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/\n/g, '<br>');
// Handle attachments
const attachmentElements = parsedContent.attachments.map((attachment, index) => (
<div key={index} className="mt-4 p-4 border rounded-lg bg-gray-50">
<div className="flex items-center">
<Paperclip className="h-5 w-5 text-gray-400 mr-2" />
<span className="text-sm text-gray-600">{attachment.filename}</span>
</div> </div>
</div> ));
));
return ( return (
<div className="prose max-w-none"> <div className="prose max-w-none">
{isHtml ? ( <div dangerouslySetInnerHTML={{ __html: content }} />
<div dangerouslySetInnerHTML={{ __html: sanitizedContent }} /> {attachmentElements}
) : ( </div>
<div className="whitespace-pre-wrap">{sanitizedContent}</div> );
)} } catch (e) {
{attachmentElements} console.error('Error parsing email:', e);
</div> return <div className="text-gray-500">Error displaying email content</div>;
); }
}
function cleanEmailContent(content: string): string {
// Remove or fix malformed URLs
return content.replace(/=3D"(http[^"]+)"/g, (match, url) => {
try {
return `"${decodeURIComponent(url)}"`;
} catch {
return '';
}
});
} }
// Define the exact folder names from IMAP // Define the exact folder names from IMAP