Edit report at https://bugs.php.net/bug.php?id=64271&edit=1

 ID:                 64271
 Comment by:         inefedor at gmail dot com
 Reported by:        rixsta at hotmail dot com
 Summary:            imap_search has a bug
 Status:             Open
 Type:               Bug
 Package:            IMAP related
 Operating System:   Cent OS
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

This function returns false if no messages were found. Yeah, it's a bit tricky, 
but you just need to do:

$items = imap_search($imap_stream, 'UNSEEN');
$newCount = $items === false ? 0 : count($items);


Previous Comments:
------------------------------------------------------------------------
[2013-02-21 21:11:24] rixsta at hotmail dot com

Description:
------------
---
>From manual page: http://www.php.net/function.imap-search#refsect1-
function.imap-search-description
---

The following example should highlight this bug clearly:
$newCount = count(imap_search($imap_stream, 'UNSEEN'));

We use the function above to count the number of 'UNSEEN' messages in the 
mailbox folders. It works fine until we have read all messages and in this case 
we would expect the $newCount to be 0. However it remains at 1!  We have been 
able to work around this bug with the following fix/function (see test script)



This returns the count as we would expect.  If you do address this bug we would 
appreciate a quick email and advise on the version of PHP it will be included 
in.

Many thanks.

Test script:
---------------
$newCount = $this->count_unread_email($imap_stream);

  function count_unread_email($imap_stream) {
      $count = 0;
      if (!$imap_stream) {
          echo "Error";
      } else {
          $headers = imap_headers($imap_stream);
          foreach ($headers as $mail) {
              $flags = substr($mail, 0, 4);
              $isunr = (strpos($flags, "U") !== false);
              if ($isunr)
              $count++;
          }
      }
      return $count;
  }

Expected result:
----------------
We expect the function to return 0 when there are no remaining 'UNSEEN' 
messages, 
not 1.

Actual result:
--------------
We receive a 1 even though there are 0 'UNSEEN' messages remaining in our 
mailbox 
folder.


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=64271&edit=1

Reply via email to