imap_search() unknown search criterion “OR”

放肆的年华 提交于 2021-02-08 21:03:59

问题


Trying to build a IMAP Search query to filter out specific mails via php code... Pushed into a situation where I must use the "OR" search criteria.

<?php
$search_string = 'OR (SUBJECT "FedEx" SUBJECT "USPS")';
$search_string = 'OR SUBJECT "FedEx" SUBJECT "USPS"';
$search_string = 'SUBJECT "FedEx" OR SUBJECT "USPS"';
$search_string = 'OR (SUBJECT "FedEx") OR (SUBJECT "USPS")';
$search_string = 'OR (SUBJECT "FedEx") (SUBJECT "USPS")';
//all of the search strings when used separately didn't work

$emails = imap_search($inbox, $search_string);
?>

But when used, php throws the error PHP Notice: Unknown: Unknown search criterion: OR (errflg=2) in Unknown on line 0

When referred php docs for IMAP it says the documentation might be inaccurate and directed to RFC 2060 so When referring section 6.4.4 for search command, it mentions that we can use OR, NOT and few extra search criteria...

so used it in prefix position, infix position and tried using parenthesis as suggested in RFC 2060 but all in vain

Also Came across the php bug entry but not sure if this is a bug or it needs to be used in some other way!

Anybody with solutions / workarounds?

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


Edit/Updates:

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 "OR"

So a php code similar to:

$inbox   = imap_open('{imap.example.com:993/imap/ssl}INBOX', 'foo@example.com', 'pass123', OP_READONLY);
$search_string = 'SUBJECT "FedEx" OR SUBJECT "USPS"';   
$emails = imap_search($inbox, $search_string);

will throw an error saying "Unknown search criterion"

observations and reference:
git repo : https://github.com/php/php-src

PHP source trace:( ref: https://github.com/php/php-src/blob/master/ext/imap/php_imap.c )
/ext/imap/php_imap.c -> line no : 4126
imap_search => line no : 4148

c-client library source trace:
src/c-client/mail.c -> line no : 3973

/docs/internal.txt -> line no : 1919 => mail_criteria()
criteria IMAP2-format search criteria string
WARNING: This function does not accept IMAP4 search criteria.

IMAP2 RFC1064 => [ ref: https://tools.ietf.org/html/rfc1064 ] [page: 13]
IMAP4 RFC2060 => [ ref: http://www.faqs.org/rfcs/rfc2060.html ] [section: 6.4.4]

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

But I'm not sure about which PHP version uses the IMAP4 compatible c-client...

  1. Is there a way to force php to just update/use the latest c-client library?
  2. Or the current latest(as of 11Apr 2016) c-client supports only IMAP2?
  3. Should we modify the library source code and compile and install?
  4. 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)?

回答1:


I solved this doing several imap_search sentences to simulate "OR".

$criteria = 'UNSEEN FROM "someaddr@gmail.com"|UNSEEN FROM "@onedomain.com"|UNSEEN FROM "@anotherdomain.org"'
$criterias = explode('|', $criteria);
$emails = array();
foreach ($criterias as $search) {
    $emails_ = imap_search($inbox, $search);
    if ($emails_)
        $emails = array_merge($emails, $emails_);
}
$emails = array_unique($emails);



回答2:


Did you try this.?

$inbox   = imap_open('{imap.example.com:993/imap/ssl}INBOX', 'foo@example.com', 'pass123', OP_READONLY);

$search_string = 'SINCE "08-Mar-2011" OR SUBJECT "FedEx" OR SUBJECT "USPS"';

$emails = imap_search($inbox, $search_string);


来源:https://stackoverflow.com/questions/36356715/imap-search-unknown-search-criterion-or

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!