4
votes

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.

2
You seem to have assumed a solution, without attacking the problem more directly. Did you try something along the lines of UID SEARCH NOT X-GM-LABELS \Draft? Gmail IMAP Extensions includes Search Terms for labels, which seems more to be what you want.Max
@Max, thank you for the suggestion and the link! I agree about assumption, but had to "engineer" it not finding direct answer. Tried with search - unfortunately it fails on both '[GMail]/All Mail' or on '[Gmail]/Drafts' (plural) even without "NOT": 29:27.40 imap.gmail.com writer > BGOD4 UID SEARCH (X-GM-LABELS (\Drafts))\r\n 29:27.50 imap.gmail.com reader < BGOD4 BAD Could not parse command\r\nkuz8
@Max, but searching just for "foo" works: > NBHH4 UID SEARCH (X-GM-LABELS foo)\r\n < * SEARCH\r\n, the question is how to search for \Drafts.. I'm using Python's imaplib2 if it makes any difference.. Sometimes it duplicates \ as \\.. So still > HIPP4 UID SEARCH (X-GM-LABELS (\Drafts))\r\n < HIPP4 BAD Could not parse command\r\n and without parens > HNEN4 UID SEARCH (X-GM-LABELS \Drafts)\r\n < HNEN4 BAD Could not parse command\r\nkuz8
Maybe "\Drafts" with quotes.Max
@Max, unfortunately "-s don't do: status, uidsString = M.uid('search', None, '(X-GM-LABELS "\Drafts")') results in > OONJ4 UID SEARCH (X-GM-LABELS "\Drafts")\r\n < OONJ4 BAD Could not parse command\r\n; but double (*2) slash treated as non-existing label M.uid('search', None, '(X-GM-LABELS "\\\\Drafts")') results in legal search but no results > OCIF4 UID SEARCH (X-GM-LABELS "\\Drafts")\r\n < * SEARCH\r\nkuz8

2 Answers

1
votes
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.

0
votes

Though not a direct answer, I ended up downloading bodies of new messages and scanning them for flags.