Avatar of Marthaj
MarthajFlag for United States of America

asked on 

IMAP multiple searches - array_intersect and PHP 7.4 Problem

I need to do a multiple search on an email box. I am using PHP 7.4.
I know imap has  a problem with multiple search criteria from everything I have read.
So I thought I would issue a search for each criteria and then use array_intersect  with the results of each  search giving me exactly what I want.
I want to select only emails that match a 'from' email address and the subject line is 'INVOICE'
The 'from' address will vary - it's read from a table.
But I can not seem to get the syntax correct.  I keep receiving: Notice: Undefined variable: emails.
And it should contain two valid email that match the search criteria of the 'from' address and the subject line  'INVOICE'. But it does not work.
Any help appreciated. And thank you in advance.
 $strFromEmail  = 'testir@gmail.com';

 $arrayFrom = imap_search($inbox, 'FROM "' .  $strFromEmail . '"'', SE_UID);
 
 $arrayInvoice = imap_search($inbox, 'SUBJECT "INVOICE"', SE_UID);
 
 if((!empty($arrayFrom)) && (!empty($arrayInvoice)))
 {
   $emails = array_intersect($arrayFrom, $arrayInvoice);
 }


Open in new window




PHP

Avatar of undefined
Last Comment
Marthaj
Avatar of David Favor
David Favor
Flag of United States of America image

The code snippet you provided is missing the authentication (login), so you must authenticate or $emails will always be FALSE.

Also for login + all other imap calls, check errors + raise exceptions if they occur.

If you write code... always expecting every call to fail... adding error capture + raising exceptions... you'll have instant debugging clues, when things go wrong.
Avatar of Marthaj
Marthaj
Flag of United States of America image

ASKER

Thank you for responding. I do check for a valid login - I just didn't post the code. It connects to the email box just fine but won't accept any other search criteria other than 'All'. 
Avatar of David Favor
David Favor
Flag of United States of America image

Likely next steps.

1) If FALSE is returned, this indicates the IMAP server has responded the search criteria is unrecognized.

2) Refer to your IMAP server logs to determine syntax problem.

3) First experiment, try escaping the double quotes... something like this...

$arrayFrom = imap_search($inbox, 'FROM \"' .  $strFromEmail . '\"'', SE_UID);

Open in new window

Avatar of Marthaj
Marthaj
Flag of United States of America image

ASKER

Thank you for responding. I have tried that but does not like the syntax - Format needs to
$arrayFrom = imap_search($inbox, 'FROM "somevalue" ' ,  SE_UID);
With your code, it's missing the single quote to end the search FROM parameter.  
So, I tried this :
    $emails = array();
    $strEmail=  'FROM "' .  $strFromEmail . '"';
    $strEmail = "'" . $strEmail . " '";
    echo '<BR> $strEmail: ' .  $strEmail;
    $arrayfrom = imap_search($inbox, $strEmail, SE_UID);
   
   
   $strChkSubject = 'SUBJECT "' . 'INVOICE' . '"';
   $strChkSubject = "'" . $strChkSubject . " '";
   echo '<BR> $strChkSubject: ' .  $strChkSubject;
   $arrayInvoice = imap_search($inbox, $strChkSubject, SE_UID);
     
   
 
    if((!empty($arrayfrom)) && (!empty($arrayInvoice)))
    {
      $emails = array_intersect($arrayfrom, $arrayInvoice);
    }

Open in new window

And this is what I received - ( no errors in logs)
SUCESSFUL CONNECTION TO EMAIL BOX: mytest@mytest.com
 $strEmail: 'FROM "myemail@gmail.com" '
 $strChkSubject: 'SUBJECT "INVOICE" '
NO INVOICE EMAILS TO PROCESS FOR REGION: 1001 EMAIL BOX: myemail@gmail.com
NO WORK ERROR FILES TO DELETE
NO WORK FILES TO DELETE

Open in new window

I also did a var_dump on each array and this is what it shows - Line 224 is where I execute the var_dump for arrayfrom.
And it displays the same for the array message for when I dump arrayInvoice.
But I know I have two emails that match the 'from' address and the subject line 'INVOICE'.

C:\wamp\www\Invoices\Invoices-ONLY-V7.php:224:boolean false

Open in new window

Avatar of David Favor
David Favor
Flag of United States of America image

Try escaping your \" (double quotes) + see if you get the correct returned UID list.

Your IMAP logs likely won't show any errors for the search syntax, just a list of UIDs or not... if the problem relates to double quote escaping problems.

Tip: Rather than using PHP for this, first debug your syntax using openssl, as this allows faster interactive testing of syntax.
Avatar of Marthaj
Marthaj
Flag of United States of America image

ASKER

Thank you for responding. I apologize for not responding sooner - get caught-up in another situation ( resolved - whew!)  The UIDs returned are correct. So I don't know what exactly is the problem.
I am going view the data in the table again and see if anything may be causing it to fail.
I would think that if there aren't matches in the arrays, $arrayfrom and $arrayInvoice, that my checking for being empty
would have caught it. And therefore, not create the intersected array, $emails.Makes sense to me.

ASKER CERTIFIED SOLUTION
Avatar of Marthaj
Marthaj
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo