From 5d5275183a4d72c58542a1b881bba856c897bbdc Mon Sep 17 00:00:00 2001 From: alma Date: Mon, 21 Apr 2025 15:01:44 +0200 Subject: [PATCH] mail page rest --- app/courrier/page.tsx | 67 ++++++++----------------------------------- lib/imap.ts | 6 +--- 2 files changed, 13 insertions(+), 60 deletions(-) diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index 682814e2..55bc47cb 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -1610,7 +1610,7 @@ export default function CourrierPage() { return selectedEmail.cc || ''; }; - const getReplyBody = (): string => { + const getReplyBody = () => { if (!selectedEmail?.body) return ''; try { @@ -1621,7 +1621,7 @@ export default function CourrierPage() { // Get the content from either HTML or text part if (parsed.html) { // Use MIME decoding for HTML content - originalContent = decodeMimeContent(parsed.html); + originalContent = decodeMIME(parsed.html, 'quoted-printable', 'utf-8'); // Convert HTML to plain text for the reply originalContent = originalContent @@ -1650,66 +1650,23 @@ export default function CourrierPage() { .trim(); } else if (parsed.text) { // Use MIME decoding for plain text content - originalContent = decodeMimeContent(parsed.text).trim(); + originalContent = decodeMIME(parsed.text, 'quoted-printable', 'utf-8').trim(); } else { // Fallback to raw body if parsing fails, but still try to decode it - originalContent = decodeMimeContent( - selectedEmail.body.replace(/<[^>]+>/g, '') + originalContent = decodeMIME( + selectedEmail.body.replace(/<[^>]+>/g, ''), + 'quoted-printable', + 'utf-8' ).trim(); } - // Clean up the content - originalContent = originalContent + // Format the reply with proper indentation + const formattedContent = originalContent .split('\n') - .map(line => line.trim()) - .filter(line => { - // Remove email client signatures and headers - return !line.match(/^(From|To|Sent|Subject|Date|Cc|Bcc):/i) && - !line.match(/^-{2,}/) && - !line.match(/^_{2,}/) && - !line.match(/^={2,}/) && - !line.match(/^This (email|message) has been/i) && - !line.match(/^Disclaimer/i) && - !line.match(/^[*_-]{3,}/) && - !line.match(/^Envoyé depuis/i) && - !line.match(/^Envoyé à partir de/i) && - !line.match(/^Sent from/i) && - !line.match(/^Outlook pour/i) && - !line.match(/^De :/i) && - !line.match(/^À :/i) && - !line.match(/^Objet :/i); - }) - .join('\n') - .trim(); + .map(line => `> ${line}`) + .join('\n'); - // Format the reply - const date = new Date(selectedEmail.date); - const formattedDate = date.toLocaleString('en-GB', { - day: '2-digit', - month: '2-digit', - year: 'numeric', - hour: '2-digit', - minute: '2-digit', - hour12: false - }); - - let replyHeader = ''; - if (type === 'forward') { - replyHeader = `\n\n---------- Forwarded message ----------\n`; - replyHeader += `From: ${selectedEmail.from}\n`; - replyHeader += `Date: ${formattedDate}\n`; - replyHeader += `Subject: ${selectedEmail.subject}\n`; - replyHeader += `To: ${selectedEmail.to}\n`; - if (selectedEmail.cc) { - replyHeader += `Cc: ${selectedEmail.cc}\n`; - } - replyHeader += `\n`; - } else { - // Simple header for reply and reply all - replyHeader = `\n\nOn ${formattedDate}, ${selectedEmail.from} wrote:\n`; - } - - return replyHeader + originalContent; + return `\n\n${formattedContent}\n\n`; } catch (error) { console.error('Error preparing reply body:', error); return ''; diff --git a/lib/imap.ts b/lib/imap.ts index 708afceb..84fd2f6b 100644 --- a/lib/imap.ts +++ b/lib/imap.ts @@ -3,11 +3,7 @@ import { getServerSession } from 'next-auth'; import { authOptions } from '@/app/api/auth/[...nextauth]/route'; import { prisma } from '@/lib/prisma'; -let client: ImapFlow | null = null; - export async function getImapClient() { - if (client) return client; - const session = await getServerSession(authOptions); if (!session?.user?.id) { throw new Error('No authenticated user'); @@ -23,7 +19,7 @@ export async function getImapClient() { throw new Error('No mail credentials found. Please configure your email account.'); } - client = new ImapFlow({ + const client = new ImapFlow({ host: credentials.host, port: credentials.port, secure: true,