diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx
index c45bacb1..65bc47f6 100644
--- a/app/courrier/page.tsx
+++ b/app/courrier/page.tsx
@@ -135,24 +135,17 @@ function EmailContent({ email }: { email: Email }) {
if (mounted) {
// Update the email content with the fetched full content
email.content = fullContent.content;
+ email.contentFetched = true;
- // Render the content
- const sanitizedHtml = DOMPurify.sanitize(fullContent.content);
- setContent(
-
{parsedEmail.text}
@@ -198,9 +182,36 @@ function EmailContent({ email }: { email: Email }) {
);
setDebugInfo('Rendered text content from parser');
} else {
- setContent(
No displayable content available
);
- setDebugInfo('No HTML or text content in parsed email');
+ // Fallback to direct display if parser didn't give us anything
+ if (formattedEmail.startsWith('<') && formattedEmail.endsWith('>')) {
+ // Content is likely HTML, sanitize and display directly
+ const sanitizedHtml = DOMPurify.sanitize(formattedEmail);
+ setContent(
+
+ );
+ setDebugInfo('Rendered raw HTML content');
+ } else {
+ // Just display as text
+ setContent(
+
+ {formattedEmail}
+
+ );
+ setDebugInfo('Rendered raw text content');
+ }
}
+ } catch (parseError) {
+ console.error('Error parsing email with mailparser:', parseError);
+ // Fallback to direct display if parser fails
+ if (formattedEmail.startsWith('<') && formattedEmail.endsWith('>')) {
+ // Content is likely HTML, sanitize and display directly
+ const sanitizedHtml = DOMPurify.sanitize(formattedEmail);
+ setContent(
+
${decoded.text}
`;
} else {
- cleanContent = '
No displayable content available
';
+ // Fallback to raw content if parsing failed
+ if (formattedEmail.startsWith('<') && formattedEmail.endsWith('>')) {
+ cleanContent = DOMPurify.sanitize(formattedEmail);
+ } else {
+ cleanContent = `
${formattedEmail}
`;
+ }
}
- // Helper function to safely extract email information
- const getSafeEmailInfo = (email: any) => {
- const fromValue = email && email.from ?
- (typeof email.from === 'object' && email.from.text) ? email.from.text :
- (Array.isArray(email.from) && email.from[0]) ?
- (email.from[0].name || email.from[0].address) : 'Unknown Sender'
- : 'Unknown Sender';
-
- const dateValue = email && email.date ?
- new Date(email.date).toLocaleString() : new Date().toLocaleString();
-
- const subjectValue = email && email.subject ? email.subject : 'No Subject';
+ // Extract reliable metadata
+ const from = decoded.from ||
+ (emailToProcess.fromName ? `${emailToProcess.fromName} <${emailToProcess.from}>` : emailToProcess.from) ||
+ 'Unknown Sender';
- const toValue = email && email.to ?
- (typeof email.to === 'object' && email.to.text) ? email.to.text :
- (Array.isArray(email.to)) ? email.to.map((t: any) => t.address || t.name || '').filter(Boolean).join(', ') : ''
- : '';
-
- const ccValue = email && email.cc ?
- (typeof email.cc === 'object' && email.cc.text) ? email.cc.text :
- (Array.isArray(email.cc) && email.cc.length > 0) ?
- email.cc.map((c: any) => c.address || c.name || '').filter(Boolean).join(', ') : ''
- : '';
-
- return { from: fromValue, date: dateValue, subject: subjectValue, to: toValue, cc: ccValue };
- };
+ const date = decoded.date ?
+ new Date(decoded.date).toLocaleString() :
+ (emailToProcess.date ? new Date(emailToProcess.date).toLocaleString() : new Date().toLocaleString());
+
+ const subject = decoded.subject || emailToProcess.subject || 'No Subject';
- // Get info from both sources and combine
- const decodedInfo = getSafeEmailInfo(decoded);
- const emailInfo = getSafeEmailInfo(emailToProcess);
+ const to = decoded.to ||
+ (emailToProcess.to && Array.isArray(emailToProcess.to) ?
+ emailToProcess.to.map((t: any) => t.address || t.name || '').filter(Boolean).join(', ') :
+ emailToProcess.to) ||
+ '';
+
+ const cc = decoded.cc ||
+ (emailToProcess.cc && Array.isArray(emailToProcess.cc) ?
+ emailToProcess.cc.map((c: any) => c.address || c.name || '').filter(Boolean).join(', ') :
+ emailToProcess.cc) ||
+ '';
- // Use the most complete information available
- const from = decodedInfo.from !== 'Unknown Sender' ? decodedInfo.from : emailInfo.from;
- const date = decodedInfo.date;
- const subject = decodedInfo.subject !== 'No Subject' ? decodedInfo.subject : emailInfo.subject;
- const to = decodedInfo.to || emailInfo.to;
- const cc = decodedInfo.cc || emailInfo.cc;
-
// Format the content based on reply type
const quotedContent = type === 'forward' ? `
@@ -235,10 +233,10 @@ export default function ComposeEmail({
` : `
- On ${decoded.date ? decoded.date.toLocaleString() : new Date().toLocaleString()}, ${decoded.from || 'Unknown Sender'} wrote:
+ On ${date}, ${from} wrote:
- ${cleanContent}
+ ${cleanContent || '
No content available
'}
`;
@@ -303,20 +301,29 @@ export default function ComposeEmail({
console.log('[DEBUG] Successfully set compose content with scrollable message area');
}
} catch (error) {
- console.error('[DEBUG] Error parsing email:', error);
- // Fallback to simple content extraction
- const fallbackContent = emailToProcess.content.replace(/<[^>]*>/g, '');
- composeBodyRef.current.innerHTML = `
+ console.error('[DEBUG] Error parsing email for compose:', error);
+
+ // Fallback to basic content display
+ const errorContent = `
-
Error loading original message.
-
- Technical details: ${error instanceof Error ? error.message : 'Unknown error'}
+
+ ---------- Original Message ---------
+ ${emailToProcess.subject ? `Subject: ${emailToProcess.subject}
` : ''}
+ ${emailToProcess.from ? `From: ${emailToProcess.from}
` : ''}
+ ${emailToProcess.date ? `Date: ${new Date(emailToProcess.date).toLocaleString()}
` : ''}
+
+
+ ${emailToProcess.preview || 'No content available'}
`;
- setComposeBody(fallbackContent);
- setLocalContent(fallbackContent);
+
+ if (composeBodyRef.current) {
+ composeBodyRef.current.innerHTML = errorContent;
+ setComposeBody(errorContent);
+ setLocalContent(errorContent);
+ }
}
} catch (error) {
console.error('[DEBUG] Error initializing compose content:', error);