From 1a030d5536a22800f92f559cdf6061add8a3de0e Mon Sep 17 00:00:00 2001 From: Matthias 't Jong Date: Tue, 9 Jun 2026 11:39:35 +0200 Subject: [PATCH] fix(mail): use UID range string in fetch for IMAP server compatibility Some IMAP servers reject array-based UID fetch. Using "uid1:uid2" range string with {uid: true} option works universally. Tested against Combell (mailprotect.be) IMAP server. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/lib/server/services/mail.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/lib/server/services/mail.ts b/src/lib/server/services/mail.ts index 4f1133d..eded98e 100644 --- a/src/lib/server/services/mail.ts +++ b/src/lib/server/services/mail.ts @@ -170,7 +170,6 @@ export async function search( Object.keys(criteria).length > 0 ? criteria : { all: true }; const result = await client.search(searchCriteria, { uid: true }); - // search() returns number[] | false const uids: number[] = result === false ? [] : result; if (uids.length === 0) return []; @@ -178,11 +177,17 @@ export async function search( // Take the most recent `limit` UIDs (largest UIDs = newest) const slicedUids = uids.slice(-limit); + // Build UID range string — some IMAP servers reject array-based fetch + const uidRange = slicedUids.length === 1 + ? String(slicedUids[0]) + : `${slicedUids[0]}:${slicedUids[slicedUids.length - 1]}`; + const results: MessageSummary[] = []; for await (const msg of client.fetch( - slicedUids, + uidRange, { uid: true, envelope: true, flags: true, bodyStructure: true }, + { uid: true }, )) { const env = msg.envelope; const hasAttachments = hasAttachmentInStructure(msg.bodyStructure);