Help: questions about query structure in imap_search

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chris_fieldhouse@hotmail.com

    Help: questions about query structure in imap_search

    Hi,

    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($co nn, "TO test_address"); to work, and
    imap_search($co nn, "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_merg e" 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($co nn, "TO test_address");
    $emailSearch2= imap_search($co nn, "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
    result
    if($emailSearch 1 == false) $emailSearch=$e mailSearch2;
    if($emailSearch 2 == false) $emailSearch=$e mailSearch1;

    $numEmails = sizeof($emailSe arch);
    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.

    Chris

  • Colin McKinnon

    #2
    Re: Help: questions about query structure in imap_search

    chris_fieldhous e@hotmail.com wrote:

    [color=blue]
    >
    > I've ended up with this rather unsatisfying piece of code, it works,
    > but it looks like a cludge.
    >[/color]

    I've seen worse.
    [color=blue]
    > <code snippet>
    > # now lets try searching for TO and CC emails! merge arrays,
    > $emailSearch1= imap_search($co nn, "TO test_address");
    > $emailSearch2= imap_search($co nn, "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
    > result
    > if($emailSearch 1 == false) $emailSearch=$e mailSearch2;
    > if($emailSearch 2 == false) $emailSearch=$e mailSearch1;
    >[/color]


    $results=array( );
    foreach(array(' TO test_address',' CC test address') as $iqry) {
    if ($false !== ($sub = imap_search($co nn, $iqry))) {
    $results = array_merge($re sults, $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.

    C.

    Comment

    • chris_fieldhouse@hotmail.com

      #3
      Re: Help: questions about query structure in imap_search

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

      much appreciated.

      Chris.

      Comment

      Working...