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;
}
// Improved MIME decoding function
// Improved MIME decoding function for all emails
const decodeMimeContent = (content: string) => {
try {
// First, try to extract the main content
// Remove MIME headers and metadata
let cleanContent = content
// Remove MIME headers and metadata
.replace(/^This is a multi-part message.*?utf-8"/s, '')
// Remove MIME headers
.replace(/^This is a multi-part message.*?charset="utf-8"/s, '')
.replace(/---InfomaniakPhpMail.*?Content-Transfer-Encoding:.*?\n/g, '')
.replace(/Content-Type:.*?\n/g, '')
.replace(/Content-Transfer-Encoding:.*?\n/g, '')
// Clean up special characters and formatting
.replace(/=C2=A0/g, ' ')
.replace(/=\n/g, '')
// Clean up special characters and encoding
.replace(/=C2=A0/g, ' ') // non-breaking space
.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(/\[IMG:.*?\]/g, '')
.replace(/\*([^*]+)\*/g, '$1')
.replace(/\[ ?LINK: ([^\]]+) ?\]/g, '$1')
// Format links and content markers
.replace(/\[IMG:(.*?)\]/g, '') // remove image placeholders
.replace(/\[ ?LINK: ([^\]]+?) ?\]/g, (_, url) => url) // clean up links
.replace(/\*(.*?)\*/g, '$1') // remove asterisk formatting
// Clean up whitespace
.replace(/\s+/g, ' ')
.replace(/\n{3,}/g, '\n\n') // reduce multiple line breaks
.replace(/^\s+|\s+$/gm, '') // trim each line
.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;
} catch (error) {
console.error('Error decoding MIME content:', error);
@ -173,24 +168,14 @@ export default function MailPage() {
return account ? account.color : 'bg-gray-500';
};
// Handle email selection
const handleEmailClick = async (emailId: number) => {
// Mark as read in IMAP
try {
await fetch('/api/mail/mark-read', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ 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);
}
// Update email click handler to work without mark-read endpoint
const handleEmailClick = (emailId: number) => {
// Since the mark-read endpoint is not available, just update the UI
const updatedEmails = emails.map(email =>
email.id === emailId ? { ...email, read: true } : email
);
setEmails(updatedEmails);
setSelectedEmail(emailId);
};
// Toggle starred status
@ -743,7 +728,7 @@ export default function MailPage() {
</div>
<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>
</>
)}