©
本文档使用 PHP中文网手册 发布
(PHP 4, PHP 5, PHP 7)
imap_search — This function returns an array of messages matching the given search criteria
$imap_stream
, string $criteria
[, int $options
= SE_FREE
[, string $charset
= NULL
]] )This function performs a search on the mailbox currently opened in the given IMAP stream.
For example, to match all unanswered messages sent by Mom, you'd use: "UNANSWERED FROM mom". Searches appear to be case insensitive. This list of criteria is from a reading of the UW c-client source code and may be incomplete or inaccurate (see also » RFC2060, section 6.4.4).
imap_stream
由 imap_open() 返回的 IMAP 流。
criteria
A string, delimited by spaces, in which the following keywords are
allowed. Any multi-word arguments (e.g.
FROM "joey smith") must be quoted. Results will match
all criteria
entries.
options
Valid values for options
are
SE_UID
, which causes the returned array to
contain UIDs instead of messages sequence numbers.
charset
MIME character set to use when searching strings.
Returns an array of message numbers or UIDs.
Return FALSE
if it does not understand the search
criteria
or no messages have been found.
Example #1 imap_search() example
<?php
$conn = imap_open ( '{imap.example.com:993/imap/ssl}INBOX' , 'foo@example.com' , 'pass123' , OP_READONLY );
$some = imap_search ( $conn , 'SUBJECT "HOWTO be Awesome" SINCE "8 August 2008"' , SE_UID );
$msgnos = imap_search ( $conn , 'ALL' );
$uids = imap_search ( $conn , 'ALL' , SE_UID );
print_r ( $some );
print_r ( $msgnos );
print_r ( $uids );
?>
以上例程的输出类似于:
Array ( [0] => 4 [1] => 6 [2] => 11 ) Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 ) Array ( [0] => 1 [1] => 4 [2] => 6 [3] => 8 [4] => 11 [5] => 12 )
[#1] Anonymous [2014-09-05 18:45:09]
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.
[#2] andrea dot job at libero dot it [2014-09-05 14:25:44]
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.
[#3] anonymous at anonymous dot ru [2014-05-02 17:38:48]
the function "imap_search" not work for some mails , maybe that because header syntax or some bug .
thanks a lot
[#4] mail at nikha dot org [2014-02-14 22:18:13]
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
[#5] trimoreau dot yonn at gmail dot com [2013-06-04 07:53:40]
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.
[#6] james at medbirdie dot com [2012-09-10 07:31:08]
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"); ?>
[#7] Anonymous [2012-07-22 19:34:30]
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.
[#8] Brett [2012-05-14 19:45:24]
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 );
[#9] joseph dot cardwell at jbcwebservices dot com [2012-03-26 17:07:27]
imap_search() always returns false when op_silent flag is set in the connection parameters.
[#10] admin at rancid-tea dot com [2007-09-20 08:57:59]
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.
[#11] oliver at samera dot com dot py [2002-10-26 08:16:22]
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".
[#12] oliver at samera dot com dot py [2002-01-26 21:03:11]
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";