Коментарии

imap_search() return false if it does not understand the search condition or no messages have been found.

$emails imap_seach($mbox, "UNDELETED SENTSINCE 01-Jan-2002");
if($emails === false)
  echo "The search failed";
2002-01-26 23:03:11
http://php5.kiev.ua/manual/ru/function.imap-search.html
imap_search() only supports IMAP2 search criterias, because the function mail_criteria() (from c-client lib) is used in ext/imap/php_imap.c for parsing the search string.
IMAP2 search criteria is defined in RFC 1176, section "tag SEARCH search_criteria".
2002-10-26 11:16:22
http://php5.kiev.ua/manual/ru/function.imap-search.html
This search looks for messages matching ALL criteria, not ANY criteria. For example the search 

imap_search($mailbox,'FROM "user" TO "user"')

Will return message that have "user" in both the from and to headers, but not messages with "user" in either the from or to header.
2007-09-20 11:57:59
http://php5.kiev.ua/manual/ru/function.imap-search.html
imap_search() always returns false when op_silent flag is set in the connection parameters.
2012-03-26 20:07:27
http://php5.kiev.ua/manual/ru/function.imap-search.html
Автор:
I haven't found any documentation of the allowed date formats, but (for example) "14 May 2012" works.

// Find UIDs of messages within the past week
$date = date ( "d M Y", strToTime ( "-7 days" ) );
$uids = imap_search ( $mbox, "SINCE \"$date\"", SE_UID );
2012-05-14 22:45:24
http://php5.kiev.ua/manual/ru/function.imap-search.html
Автор:
The date format for e.g. SINCE is, according to rfc3501:

date            = date-text / DQUOTE date-text DQUOTE

date-day        = 1*2DIGIT
                    ; Day of month

date-day-fixed  = (SP DIGIT) / 2DIGIT
                    ; Fixed-format version of date-day

date-month      = "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" /
                  "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"

date-text       = date-day "-" date-month "-" date-year

So a valid date is e.g. "22-Jul-2012" with or without the double quotes.
2012-07-22 22:34:30
http://php5.kiev.ua/manual/ru/function.imap-search.html
To set your own CHARSET, which is useful if you are dealing with Chinese Japanese and Korean queries.

<?php imap_search($inbox,'BODY "'.$keyword.'"'SE_FREE"UTF-8"); ?>
2012-09-10 10:31:08
http://php5.kiev.ua/manual/ru/function.imap-search.html
It's not possible to find strings containing double quotes using this function.

For example, if you got a message named : Hello, this is "Bob"
You can try :
     imap_search($inbox, 'SUBJECT "Hello, this is "Bob""')
Or
     imap_search($inbox, "SUBJECT 'Hello, this is \"Bob\"'")

But both are false, because you did not escape double quotes in the first case, and you can NOT use simple quotes in the imap_search criteria in the second case.

The real problem is that you cannot use simple quotes to surround your criteria in the 2nd argument of imap_search, after SUBJECT.
2013-06-04 10:53:40
http://php5.kiev.ua/manual/ru/function.imap-search.html
Hi, 
be aware, that imap_search() does NOT (as you may exspect) return an empty array, if nothing was found! 
As the manual says, it returns FALSE.

Do not test the result like "count($array)" as I did. 
This gives you 1 for an empty result. Took me an hour to found out why :-(  RTFM
2014-02-15 00:18:13
http://php5.kiev.ua/manual/ru/function.imap-search.html
the function "imap_search" not work for some mails , maybe that because header syntax or some bug .

thanks a lot
2014-05-02 20:38:48
http://php5.kiev.ua/manual/ru/function.imap-search.html
the second parameter about Criteria does not work well on ON criterion.
In facts if I wish to put parameters from $_get into the format Day-Month-Year (01-01-14 for example) will return Unknown criterion etc.
Probably is not the right format ?

Even with for example Thu-Jan-2014 get the same message.
2014-09-05 17:25:44
http://php5.kiev.ua/manual/ru/function.imap-search.html
Автор:
about my previous note:

<<the second parameter about Criteria does not work well on ON criterion.
In facts if I wish to put parameters from $_get into the format Day-Month-Year (01-01-14 for example) will return Unknown criterion etc.
Probably is not the right format ?

Even with for example Thu-Jan-2014 get the same message. >>
------------------------------------------------------------------------

Now works ;) 

Just to pass not a date string into criteria parameter but a timestamp returned by mktime function, where you can put your date string.

Solution for any date/time criterion is a unix timestamp.
2014-09-05 21:45:09
http://php5.kiev.ua/manual/ru/function.imap-search.html
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:

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

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...
2016-04-11 14:10:13
http://php5.kiev.ua/manual/ru/function.imap-search.html
Автор:
This is the correct way to use the imap_search with ON "date"

$date = date("j F Y");

$emails = imap_search($inbox,'ON "'.$date.'"' );
2016-08-19 00:44:35
http://php5.kiev.ua/manual/ru/function.imap-search.html
Автор:
If email subject is encoded than use encoding charset and option in search  to find something.

Mail Header:

Subject: =?UTF-8?Q?XYZ?=

<?php imap_search($inbox,'SUBJECT "'.$keyword.'"'SE_FREE"UTF-8"); ?>
2016-10-09 17:12:33
http://php5.kiev.ua/manual/ru/function.imap-search.html
utf-8 charset isn't supported on outlook 365. So you might get a false return value here, if you pass that charset.
2019-10-10 18:20:58
http://php5.kiev.ua/manual/ru/function.imap-search.html
Автор:
Please be aware about UID of the message.
It is NOT an ID that never change!

If you move your message to another folder in your IMAP account, this UID WILL CHANGE. 

So if your message has UID = 100 (in INBOX folder) and you move it to some subfolder and then back to INBOX, it's new UID in INBOX will be 101.
2020-11-02 07:36:36
http://php5.kiev.ua/manual/ru/function.imap-search.html
Автор:
It has been noted that imap_search breaks with imap4 syntax.  To do an imap 4 search use curl and send a custom command, then grab the results.  Its best to do a UID search to get the unique IDs to work with later.  Here's an example with a working curl function.

<?php
$host 
'your-server.tld';
$user 'username';
$pass 'password';
$folder 'INBOX';

function 
send_imap_command($server$user$pass$command$folder="INBOX")
{   
//Send an imap command directly to the imap server

   
$result=["response"=>"""error"=>""];
   
$url "imaps://$server/"rawurlencode($folder);
   
$options=[CURLOPT_URL=>$urlCURLOPT_PORT=> 993CURLOPT_USERNAME=> $user,
       
CURLOPT_PASSWORD=> $passCURLOPT_RETURNTRANSFER=> trueCURLOPT_HEADER=> true,
       
CURLOPT_CUSTOMREQUEST=> $command];
   
$ch curl_init();
   
curl_setopt_array($ch$options);

   
$result["response"] = curl_exec($ch);
    if(
curl_errno($ch)) $response["error"]="Error ("curl_errno($ch) ."): "curl_error($ch);

    return 
$result;
}

//Pull out all the emails returned as undeliverable by the remote mail server in the inbox using curl
$response=send_imap_command($host$user$pass,
           
'UID SEARCH SINCE "01-Jan-2022" (OR FROM "mailer-daemon" FROM "postmaster") (OR SUBJECT "fail" (OR SUBJECT "undeliver" SUBJECT "returned"))',
           
$folder);

if(
$response["error"]!="")
{
    echo 
$response["error"]."\n";
} elseif (
strlen($response["response"])>5){
   
//Server returns a string in the form * SEARCH uid1 uid2 uid3 ...  Clean up and create array of UIDs.
   
$response["response"]=str_replace("* SEARCH ","",$response["response"]);
   
$messages=explode(" ",$response["response"]);
}

print_r($messages);
?>
2022-01-12 14:07:58
http://php5.kiev.ua/manual/ru/function.imap-search.html

    Поддержать сайт на родительском проекте КГБ