From 181375fe08584ed55f25dedf98ef433cd8766941 Mon Sep 17 00:00:00 2001 From: alma Date: Mon, 28 Apr 2025 14:35:48 +0200 Subject: [PATCH] courrier multi account restore compose --- lib/services/email-service.ts | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/lib/services/email-service.ts b/lib/services/email-service.ts index eaaee321..24a20602 100644 --- a/lib/services/email-service.ts +++ b/lib/services/email-service.ts @@ -292,16 +292,19 @@ export async function getEmails( } try { - // Select the mailbox - await client.mailboxOpen(folder); + // Select the mailbox and wait for it to be fully opened + const mailbox = await client.mailboxOpen(folder); + if (!mailbox) { + throw new Error(`Failed to open mailbox ${folder}`); + } - // Get total count - const totalEmails = await client.mailbox.messages.total; + // Get total count from the mailbox object + const totalEmails = mailbox.exists || 0; const totalPages = Math.ceil(totalEmails / perPage); // Calculate range for this page - const start = (page - 1) * perPage + 1; - const end = Math.min(start + perPage - 1, totalEmails); + const start = Math.max(1, totalEmails - (page * perPage) + 1); + const end = Math.max(1, totalEmails - ((page - 1) * perPage)); // Fetch messages const messages = await client.fetch(`${start}:${end}`, { @@ -310,9 +313,7 @@ export async function getEmails( bodyStructure: true, internalDate: true, size: true, - bodyParts: [ - { part: 'TEXT', query: { headers: true } } - ] + bodyParts: ['HEADER'] }); // Process messages @@ -320,8 +321,14 @@ export async function getEmails( for await (const message of messages) { const email: EmailMessage = { id: message.uid.toString(), - from: message.envelope.from[0]?.address || '', - to: message.envelope.to.map(addr => addr.address).join(', '), + from: message.envelope.from.map(addr => ({ + name: addr.name || '', + address: addr.address + })), + to: message.envelope.to.map(addr => ({ + name: addr.name || '', + address: addr.address + })), subject: message.envelope.subject || '', date: message.internalDate, flags: {