imap_search() unknown search criterion "OR"
<p>Trying to build a IMAP Search query to filter out specific mails via php code... Pushed into a situation where I must use the &quot;OR&quot; search criteria.</p>
<pre><code>&lt;?php
$search_string = 'OR (SUBJECT &quot;FedEx&quot; SUBJECT &quot;USPS&quot;)';
$search_string = 'OR SUBJECT &quot;FedEx&quot; SUBJECT &quot;USPS&quot;';
$search_string = 'SUBJECT &quot;FedEx&quot; OR SUBJECT &quot;USPS&quot;';
$search_string = 'OR (SUBJECT &quot;FedEx&quot;) OR (SUBJECT &quot;USPS&quot;)';
$search_string = 'OR (SUBJECT &quot;FedEx&quot;) (SUBJECT &quot;USPS&quot;)';
//all of the search strings when used separately didn't work

$emails = imap_search($inbox, $search_string);
?&gt;
</code></pre>
<p>But when used, php throws the error <code>PHP Notice:  Unknown: Unknown search criterion: OR (errflg=2) in Unknown on line 0</code></p>
<p>When referred <a href="http://php.net/manual/en/ref.imap.php" rel="nofollow noreferrer">php docs for IMAP</a> it says the documentation might be inaccurate and directed to <a href="http://www.faqs.org/rfcs/rfc2060.html" rel="nofollow noreferrer">RFC 2060</a> so When referring section 6.4.4 for search command, it mentions that we can use OR, NOT and few extra search criteria...</p>
<p>so used it in prefix position, infix position and tried using parenthesis as suggested in RFC 2060 but all in vain</p>
<p>Also Came across the <a href="https://bugs.php.net/bug.php?id=45140" rel="nofollow noreferrer">php bug entry</a> but not sure if this is a bug or it needs to be used in some other way!</p>
<p>Anybody with solutions / workarounds?</p>
<p>I would hate to iterate over every mail and check in code if it satisfies the condition... I'd like that to be accomplished via IMAP search</p>
<hr />
<p><strong>Edit/Updates:</strong></p>
<p>imap_search function is not fully compatible with IMAP4. the c-client used as of now supports only IMAP2 and some search criterion will not be available for use such as &quot;OR&quot;</p>
<p><strong>So a php code similar to:</strong></p>
<pre><code>$inbox   = imap_open('{imap.example.com:993/imap/ssl}INBOX', 'foo@example.com', 'pass123', OP_READONLY);
$search_string = 'SUBJECT &quot;FedEx&quot; OR SUBJECT &quot;USPS&quot;';   
$emails = imap_search($inbox, $search_string);
</code></pre>
<p>will throw an error saying &quot;Unknown search criterion&quot;</p>
<p><strong>observations and reference:</strong><br>
git repo : <a href="https://github.com/php/php-src" rel="nofollow noreferrer">https://github.com/php/php-src</a><br></p>
<p><strong>PHP source trace:</strong>( ref: <a href="https://github.com/php/php-src/blob/master/ext/imap/php_imap.c" rel="nofollow noreferrer">https://github.com/php/php-src/blob/master/ext/imap/php_imap.c</a> )<br>
/ext/imap/php_imap.c -&gt; line no : 4126<br>
imap_search =&gt; line no : 4148<br></p>
<p><strong>c-client library source trace:</strong><br>
src/c-client/mail.c -&gt; line no : 3973<br></p>
<p>/docs/internal.txt -&gt; line no : 1919 =&gt; mail_criteria()<br>
criteria IMAP2-format search criteria string<br>
WARNING: This function does not accept IMAP4 search criteria.</p>
<p><strong>IMAP2 RFC1064</strong> =&gt; [ ref: https://www.rfc-editor.org/rfc/rfc1064 ] [page: 13]<br>
<strong>IMAP4 RFC2060</strong> =&gt; [ ref: http://www.faqs.org/rfcs/rfc2060.html ] [section: 6.4.4]</p>
<p>Note:
The core search functionality in a core module(IMAP) is still not available in PHP. Hope this will be brought to the developer community's attention...</p>
<p>But I'm not sure about which PHP version uses the IMAP4 compatible c-client...</p>
<ol>
<li>Is there a way to force php to just update/use the latest c-client
library?</li>
<li>Or the current latest(as of 11Apr 2016) c-client supports only IMAP2?</li>
<li>Should we modify the library source code and compile and
install?</li>
<li><em><strong>Or Is there any other PHP libraries developed by 3rd parties that we
can use to accomplish the same(imap protocol mail search, retrieve
for those mail providers who doesn't support OAuth2)?</strong></em></li>
</ol>