Skip to content

Commit a3c8424

Browse files
authored
fix: handle Proton Bridge IMAP response format in _batch_fetch_headers (#112)
Proton Bridge returns UID in a separate item after header data: - [i] = b'N FETCH (BODY[HEADER] {size}' - [i+1] = bytearray(headers) - [i+2] = b' UID xxx)' This adds a fallback to check for UID in data[i+2] when not found in data[i]. Related to #87 (Proton Mail Bridge compatibility)
1 parent fce15ad commit a3c8424

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

mcp_email_server/emails/classic.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,24 @@ async def _batch_fetch_headers(
334334
for i, item in enumerate(data):
335335
if not isinstance(item, bytes) or b"BODY[HEADER]" not in item:
336336
continue
337+
# First try to find UID in the same line (standard format)
337338
uid_match = re.search(rb"UID (\d+)", item)
338339
if uid_match and i + 1 < len(data) and isinstance(data[i + 1], bytearray):
339340
uid = uid_match.group(1).decode()
340341
raw_headers = bytes(data[i + 1])
341342
metadata = self._parse_headers(uid, raw_headers)
342343
if metadata:
343344
results[uid] = metadata
345+
# Proton Bridge format: UID comes AFTER header data in a separate item
346+
# Format: [i]=b'N FETCH (BODY[HEADER] {size}', [i+1]=bytearray(headers), [i+2]=b' UID xxx)'
347+
elif i + 2 < len(data) and isinstance(data[i + 1], bytearray):
348+
uid_after_match = re.search(rb"UID (\d+)", data[i + 2]) if isinstance(data[i + 2], bytes) else None
349+
if uid_after_match:
350+
uid = uid_after_match.group(1).decode()
351+
raw_headers = bytes(data[i + 1])
352+
metadata = self._parse_headers(uid, raw_headers)
353+
if metadata:
354+
results[uid] = metadata
344355

345356
return results
346357

0 commit comments

Comments
 (0)