Search code examples
imap

Retrieve IMAP headers for select list of emails?


I want to query all undeleted emails in my Inbox, then retrieve the headers for those emails. It's not working.

First, I query for undeleted emails:

a0003 SEARCH UNDELETED
* SEARCH 14 56 242 243 244 405

Then, I want to query for some header info:

a0004 FETCH 14,56,242,243,244,405 (FLAGS BODY.PEEK[HEADER.FIELDS (SUBJECT DATE FROM)])

...and this generates an error:

a0004 BAD Error in IMAP command FETCH: Invalid arguments.

Now, this works if I do something like 14:* for my message list. But that's not what I want; I want to query a very specific list of message numbers.

I tried using UIDs as well to see if that fixed the problem:

a0003 UID SEARCH UNDELETED
* SEARCH 5536 5537 5538 5561 5568 5655

a004 UID FETCH 5536,5537,5538,5561,5568,5655, etc.

...but I get a similar error.

Any suggestions on how best to accomplish this? Thanks!


Solution

  • False alarm. I solved my own problem. Turns out, a \r was sneaking into the FETCH string. I cleared it up and it worked fine.

    Thanks for the confirmation that it should indeed work!