php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64271 imap_search has a bug
Submitted: 2013-02-21 21:11 UTC Modified: 2015-08-29 13:38 UTC
From: rixsta at hotmail dot com Assigned: cmb (profile)
Status: Not a bug Package: IMAP related
PHP Version: Irrelevant OS: Cent OS
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
28 + 25 = ?
Subscribe to this entry?

 
 [2013-02-21 21:11 UTC] 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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-02-24 15:50 UTC] inefedor at gmail dot com
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);
 [2015-08-29 13:38 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2015-08-29 13:38 UTC] cmb@php.net
> This function returns false if no messages were found.

Indeed. And count(false) === 1.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 14:01:29 2024 UTC