How can I use the Conditional OR to search emails with imap_search - PHP IMAP library
I need to use an OR statement in an imap_search() command.
I found that a condition OR is not supported in this library. Imap_search Bug Report Link There has to be a way that I can search through the email using开发者_开发技巧 a conditional OR, how would I go about doing it? I'm not sure where I'd start.
What I'd like to be able to do:
$boxes = imap_search($connection,'SINCE "08-Mar-2011" AND (BODY "bobby" OR BODY "robert" OR BODY "bob"');
Your SEARCH syntax is wrong, as the operators are prefix and not infix. The correct search string should be 'SINCE "08-Mar-2011" OR BODY "bobby" OR BODY "robert" BODY "bob"'
.
But if PHP simply doesn't support OR
in imap_search
-- and your link indicates that they don't, despite c-client support for it for almost 10 years -- then you'll have to do the conditional OR in your application. Run
$box_bobby = imap_search($connection, 'SINCE "08-Mar-2011" BODY "bobby"');
$box_robert = imap_search($connection, 'SINCE "08-Mar-2011" BODY "robert"');
$box_bob = imap_search($connection, 'SINCE "08-Mar-2011" BODY "bob"');
and then merge the three result sets.
or use instead an array_merge to join the results in a single array:
//search codes: 1 -> FROM, 2 -> SUBJECT
array_push($filtri, array(1,"no-reply@accounts.google.com"));
array_push($filtri, array(1,"Tophost"));
array_push($filtri, array(2,"promemoria"));
$yesterday = date("Y-m-d", strtotime ("-1 days"));
foreach($filtri as $filtro){
if ($filtro[0] == 1) $addFiltri = ' FROM "'.$filtro[1].'" ';
if ($filtro[0] == 2) $addFiltri = ' SUBJECT "'.$filtro[1].'" ';
$temp = imap_search($inbox,'SINCE "'.$yesterday.'" '.$addFiltri);
$emails = array_merge($emails , $temp);
}
Just FYI on IMAP OR syntax, OR is a prefix operator and can only take two operands.
SINCE "08-Mar-2011" OR BODY "bobby" BODY "robert"'
If you have more than two, you need to add an OR: e.g., 3 operands
SINCE "08-Mar-2011" OR OR BODY "bobby" BODY "robert" BODY "bob"'
e.g., 4 operands
SINCE "08-Mar-2011" OR OR OR BODY "bobby" BODY "robert" BODY "bob" BODY "bobert"'
Also, I saw in other posts that IMAP uses the polish syntax, so parens are not necessary.
精彩评论