stacky67
stacky67

Reputation: 78

perl Net::IMAP::Client: how to express complex search query

I am using Net::IMAP::Client to clean up my mail folders on my providers host. At first I search the messages matching some criteria:

The doc says:

search($criteria, $sort, $charset)

Executes the "SEARCH" or "SORT" IMAP commands (depending on wether $sort is undef) and returns the results as an array reference containing message ID-s.

Note that if you use $sort and the IMAP server doesn't have this capability, this method will fail. Use "capability" to investigate.

  • $criteria

    Can be a string, in which case it is passed literally to the IMAP command (which can be "SEARCH" or "SORT").

It can also be an hash reference, in which case keys => values are collected into a > string and values are properly quoted, i.e.:

{ subject => 'foo',
  from    => 'bar' }

will translate to:

'SUBJECT "foo" FROM "bar"' 

which is a valid IMAP SEARCH query.

If you want to retrieve all messages (no search criteria) then pass 'ALL' here.

I need to search for all mails where subject matches 'this' or 'that'.

This is what I tried:

{ subject => 'this', subject => 'that' }

But it correctly returns only mails with 'that' in subject line.

Any suggestions ?

Upvotes: 1

Views: 226

Answers (1)

Dada
Dada

Reputation: 6626

In a hash, each key is unique. { subject => 'this', subject => 'that' } results in the same hash as { subject => 'that' }, which explains why you only get emails starting with that.

I would suggest to use a string instead of a hash reference, and use OR to combine multiple SUBJECT:

"OR SUBJECT this SUBJECT that"

(use quotes around this and that if they contain spaces)

Upvotes: 1

Related Questions