Help: questions about query structure in imap_search

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'm maintaing a website for a group and we have several email addresses
feeding into one inbox, so I want to find a way of finding all the
emails that were sent to a particular address either via "TO" or "CC"
(not interested in "BCC")

imap_search seems to be the solution, but there are several issues.
1. I can get imap_search($conn, "TO test_address"); to work, and
imap_search($conn, "CC test_address");, but not a combined 'OR' search
to grab the "TO" and "CC" together.
2. so using 2 searches and trying to merger the results, the issue is
that while the search returns false if no messages are found, the
returned arrays are not empty, and doing an "array_merge" results in an
array of size 2, even when no results found.

I've ended up with this rather unsatisfying piece of code, it works,
but it looks like a cludge.

<code snippet>
# now lets try searching for TO and CC emails! merge arrays,
$emailSearch1= imap_search($conn, "TO test_address");
$emailSearch2= imap_search($conn, "CC test_address");

# attempt to merge arrays
$emailSearch= array_merge ($emailSearch1, $emailSearch2);

# if one array is 'empty', just assign the other array to be the search
if($emailSearch1 == false) $emailSearch=$emailSearch2;
if($emailSearch2 == false) $emailSearch=$emailSearch1;

$numEmails = sizeof($emailSearch);
if($emailSearch == false) $numEmails=0;

Can any of you experts recommend a better way of performing the
imap_search and merging the results?

Thanks in advance.  


Re: Help: questions about query structure in imap_search wrote:

Quoted text here. Click to load it

I've seen worse.
Quoted text here. Click to load it

foreach(array('TO test_address','CC test address') as $iqry) {
        if ($false !== ($sub = imap_search($conn, $iqry))) {
                $results = array_merge($results, $sub);

...strikes me as more elegant - but less transparent. Also your code will
leave 'false' in the result when both queries fail, whereas my code returns
an empty array.


Re: Help: questions about query structure in imap_search

Thank you,
that is a lot neater way of merging the arrays.

much appreciated.


Site Timeline