javascriptnode.jscoffeescriptimap

Searching for emails with empty subjects using IMAP


I am trying to search emails from my draft folder using node-imap package

So what my file does is, it uses an imap search function based on Subjects and since the number of subjects is dynamic, we build the search query on the go.

Here's a sample search query

[
    'UNDELETED',
    [
      'OR',
      [
        'OR',
        [
          'OR',
          [ 'SUBJECT', '<My Subject 3>' ],
          [ 'SUBJECT', ' <My Subject 2>  ' ]
        ],
        [ 'SUBJECT', '<My Subject 1>' ]
      ],
      [ 'SUBJECT', 'abc' ]
    ]
]

Now, so far it is working fine. But if, in case, the subject field is blank and I rewrite the same code as given below, I get Error: infeasible query (Failure)

[
    'UNDELETED',
    [
      'OR',
      [
        'OR',
        [
          'OR',
          [ 'SUBJECT', '<My Subject 3>' ],
          [ 'SUBJECT', ' <My Subject 2>  ' ]
        ],
        [ 'SUBJECT', '<My Subject 1>' ]
      ],
      [ 'SUBJECT', '' ]
    ]
]

I have already tried replacing '' with null and also leaving it blank but to no avail. Any help in this matter would be greatly appreciated.


Solution

  • RFC 9051 section 6.4.4 says: In all search keys that use strings, and unless otherwise specified, a message matches the key if the string is a substring of the associated text.

    The empty string is a substring of every (other) string, so your search means "any subject". I can see how that's infeasible.

    Depending on what you want to do, rewriting the search to use NOT SUBJECT "foo" may be a possibility, if you have a suitable value of foo.