Though not a direct answer, I ended up downloading bodies of new messages and scanning them for flags.
How do I formulate search for "HEADER Delivered-To is not empty" in GMail IMAP? (or SEARCH "[Gmail]/All Mail" and exclude "/Drafts")
-
24-06-2022 - |
Question
I'm looking for an IMAP search clause to get GMail's "[Gmail]/All Mail" contents but filter out mails which are in "[Gmail]/Drafts".
IMAP DRAFT flag doesn't help with GMail's IMAP. By comparing full headers, I've noticed that drafts don't have value in "Delivered-To" field - it's missing from the header for all messages in "[Gmail]/Drafts".
I've tried UID search directly on "[Gmail]/Drafts" which has a few messages with following clause/results:
I'm expecting to get a clause which returns no messages - or all messages which have Delivered-To absent:
(HEADER Delivered-To "") <- returns all drafts
(NOT (HEADER Delivered-To "")) <- negating still returns all drafts!
(NOT HEADER Delivered-To "") <- still returns all drafts
(HEADER Delivered-To NIL) <- returns no messages - opposite to expected
(NOT (HEADER Delivered-To NIL)) <- returns all drafts
(NOT HEADER Delivered-To NIL) <- returns all drafts
(NOT (DRAFT)) <- returns all drafts
(DRAFT) <- returns none - opposite to expected, GMail doesn't seem to flag drafts with DRAFT
Will appreciate suggestions for how would you formulate "HEADER Delivered-To is not empty" for GMail IMAP.
Solution 2
OTHER TIPS
x uid search header "delivered-to" "@"
The HEADER key searches for a header field with the given name containing the given substring. That is, header "foo" "" searches for messages that have a foo header field, empty or not. There is no proper way to search for messages that have a nonempty field.
However, you can get around that with a hack in this case: Every nonempty Delivered-To contains an @ sign, so just search for @ and you get the result you want.