mail page imap connection 14

This commit is contained in:
alma 2025-04-15 21:57:48 +02:00
parent f717203e65
commit 3daa93bc95

View File

@ -41,35 +41,30 @@ interface Email {
category: string; category: string;
} }
// Improved MIME decoding function // Improved MIME decoding function for all emails
const decodeMimeContent = (content: string) => { const decodeMimeContent = (content: string) => {
try { try {
// First, try to extract the main content // Remove MIME headers and metadata
let cleanContent = content let cleanContent = content
// Remove MIME headers and metadata // Remove MIME headers
.replace(/^This is a multi-part message.*?utf-8"/s, '') .replace(/^This is a multi-part message.*?charset="utf-8"/s, '')
.replace(/---InfomaniakPhpMail.*?Content-Transfer-Encoding:.*?\n/g, '') .replace(/---InfomaniakPhpMail.*?Content-Transfer-Encoding:.*?\n/g, '')
.replace(/Content-Type:.*?\n/g, '') .replace(/Content-Type:.*?\n/g, '')
.replace(/Content-Transfer-Encoding:.*?\n/g, '') .replace(/Content-Transfer-Encoding:.*?\n/g, '')
// Clean up special characters and formatting // Clean up special characters and encoding
.replace(/=C2=A0/g, ' ') .replace(/=C2=A0/g, ' ') // non-breaking space
.replace(/=\n/g, '') .replace(/=E2=80=(93|94)/g, '-') // dashes
.replace(/=\n/g, '') // soft line breaks
.replace(/=([0-9A-F]{2})/g, (_, hex) => String.fromCharCode(parseInt(hex, 16))) .replace(/=([0-9A-F]{2})/g, (_, hex) => String.fromCharCode(parseInt(hex, 16)))
.replace(/\[IMG:.*?\]/g, '') // Format links and content markers
.replace(/\*([^*]+)\*/g, '$1') .replace(/\[IMG:(.*?)\]/g, '') // remove image placeholders
.replace(/\[ ?LINK: ([^\]]+) ?\]/g, '$1') .replace(/\[ ?LINK: ([^\]]+?) ?\]/g, (_, url) => url) // clean up links
.replace(/\*(.*?)\*/g, '$1') // remove asterisk formatting
// Clean up whitespace // Clean up whitespace
.replace(/\s+/g, ' ') .replace(/\n{3,}/g, '\n\n') // reduce multiple line breaks
.replace(/^\s+|\s+$/gm, '') // trim each line
.trim(); .trim();
// Extract just the welcome message and first paragraph if it's the welcome email
if (cleanContent.includes('Welcome to your professional mailbox')) {
const welcomeMatch = cleanContent.match(/Welcome to your professional mailbox!(.+?)(?=\*Getting started\*|$)/s);
if (welcomeMatch) {
return welcomeMatch[0].trim();
}
}
return cleanContent; return cleanContent;
} catch (error) { } catch (error) {
console.error('Error decoding MIME content:', error); console.error('Error decoding MIME content:', error);
@ -173,24 +168,14 @@ export default function MailPage() {
return account ? account.color : 'bg-gray-500'; return account ? account.color : 'bg-gray-500';
}; };
// Handle email selection // Update email click handler to work without mark-read endpoint
const handleEmailClick = async (emailId: number) => { const handleEmailClick = (emailId: number) => {
// Mark as read in IMAP // Since the mark-read endpoint is not available, just update the UI
try { const updatedEmails = emails.map(email =>
await fetch('/api/mail/mark-read', { email.id === emailId ? { ...email, read: true } : email
method: 'POST', );
headers: { 'Content-Type': 'application/json' }, setEmails(updatedEmails);
body: JSON.stringify({ emailId }) setSelectedEmail(emailId);
});
const updatedEmails = emails.map(email =>
email.id === emailId ? { ...email, read: true } : email
);
setEmails(updatedEmails);
setSelectedEmail(emailId);
} catch (error) {
console.error('Error marking email as read:', error);
}
}; };
// Toggle starred status // Toggle starred status
@ -743,7 +728,7 @@ export default function MailPage() {
</div> </div>
<div className="border-t border-gray-200 pt-6 prose max-w-none"> <div className="border-t border-gray-200 pt-6 prose max-w-none">
<p>{getSelectedEmail()?.body}</p> <p className="whitespace-pre-wrap">{decodeMimeContent(getSelectedEmail()?.body || '')}</p>
</div> </div>
</> </>
)} )}