php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #53361 imap_search function (almost) always returns false
Submitted: 2010-11-19 19:30 UTC Modified: 2010-12-13 15:11 UTC
From: hm2k@php.net Assigned: hm2k (profile)
Status: Closed Package: IMAP related
PHP Version: 5.2.14 OS: CentOS
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: hm2k@php.net
New email:
PHP Version: OS:

 

 [2010-11-19 19:30 UTC] hm2k@php.net
Description:
------------
---
From manual page: http://www.php.net/function.imap-search
---

The imap_search() function almost always returns false with no hint or indication as to why.

Test script:
---------------
<pre>
<?php

ini_set('display_errors','On');
error_reporting(E_ALL);

include 'settings.php';

//connect to mailbox
$stream=imap_open('{'.$host.'/imap/notls/norsh/novalidate-cert/readonly}INBOX',$user,$pass,OP_SILENT);
           
echo "imap_mailboxmsginfo\n";
var_dump(imap_mailboxmsginfo($stream));
echo "\n";

//searches
$searches=array();
$searches[]='ALL';
$searches[]='ANSWERED';
$searches[]='BCC "@"';
$searches[]='BEFORE 19-11-2010';
$searches[]='SINCE 1-1-1970';
$searches[]='SINCE 01-01-1970';
$searches[]='SINCE 01-Jan-1970';
$searches[]='SINCE "1-1-1970"';
$searches[]='SINCE "1-Jan-1970"';
$searches[]='SINCE "17-Jul-1996 02:44:25 -0700"';
$searches[]='SINCE "17 Nov 2008"';

foreach ($searches as $search) {
  echo "imap_search $search\n";
  var_dump(imap_search($stream,$search)); //bool(false)
  if (imap_last_error()) { echo 'Error: '.imap_last_error()."\n"; }
  echo "\n";
}

Expected result:
----------------
Returns an array of message numbers or UIDs.

Actual result:
--------------
imap_mailboxmsginfo
object(stdClass)#1 (8) {
  ["Unread"]=>
  int(70)
  ["Deleted"]=>
  int(0)
  ["Nmsgs"]=>
  int(974)
  ["Size"]=>
  int(19740065)
  ["Date"]=>
  string(37) "Sat, 20 Nov 2010 02:50:34 +0000 (GMT)"
  ["Driver"]=>
  string(4) "imap"
  ["Mailbox"]=>
  string(96) "{mail.example.com:143/imap/notls/novalidate-cert/readonly/user="email@example.com"}INBOX"
  ["Recent"]=>
  int(0)
}

imap_search ALL
bool(false)

imap_search ANSWERED
bool(false)

imap_search BCC "@"
bool(false)

imap_search BEFORE 19-11-2010
bool(false)
Error: Unknown search criterion: BEFORE

imap_search SINCE 1-1-1970
bool(false)
Error: Unknown search criterion: SINCE

imap_search SINCE 01-01-1970
bool(false)
Error: Unknown search criterion: SINCE

imap_search SINCE 01-Jan-1970
bool(false)
Error: Unknown search criterion: SINCE

imap_search SINCE "1-1-1970"
bool(false)
Error: Unknown search criterion: SINCE

imap_search SINCE "1-Jan-1970"
bool(false)
Error: Unknown search criterion: SINCE

imap_search SINCE "17-Jul-1996 02:44:25 -0700"
bool(false)
Error: Unknown search criterion: SINCE

imap_search SINCE "17 Nov 2008"
bool(false)
Error: Unknown search criterion: SINCE



Notice:  Unknown: Unknown search criterion: BEFORE (errflg=2) in Unknown on line 0



Notice:  Unknown: Unknown search criterion: SINCE (errflg=2) in Unknown on line 0



Notice:  Unknown: Unknown search criterion: SINCE (errflg=2) in Unknown on line 0



Notice:  Unknown: Unknown search criterion: SINCE (errflg=2) in Unknown on line 0



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-19 20:00 UTC] frozenfire@php.net
-Status: Open +Status: Feedback
 [2010-11-19 20:00 UTC] frozenfire@php.net
Looking at your test script, it appears that you are sending each segment of the imap_search command as a separate call.

The documentation clearly states that the "criteria" parameter is a space-delimited set of filtering commands which are sent as a single call.

I'm setting this bug to Feedback while you try modifying your script to, instead of looping through $searches, implode it with space "glue".

An example would be: var_dump(imap_search($stream,implode(' ', $searches)));

If you continue to have issues with it, respond with the result. I'd rather not have to set up the extension on my system to test what seems like a simple EBCAK ;).
 [2010-11-19 20:52 UTC] ceo at l-i-e dot com
Hard won experience:

You don't want just imap_last_error().  Internal imap calls will overwrite that, as I recall...

Oh, and some things most of us would consider as "error" are classified by IMAP as "alert"...

So you want more like:
var_dump(imap_errors($stream));
var_dump(imap_alerts($stream));
 [2010-11-19 23:05 UTC] hm2k@php.net
@frozenfire you have clearly understood. They are meant to by separate calls, as a point of demonstration. Each of them fail individually. Equally, they will fail together too.

ceo at l-i-e dot com: imap_last_error is called after each imap_search call, so being overwritten isn't an issue.

I'm really seeking advise from experienced developers, but thanks for your interest anyway.
 [2010-12-13 15:11 UTC] hm2k@php.net
-Status: Feedback +Status: Closed -Assigned To: +Assigned To: hm2k
 [2010-12-13 15:11 UTC] hm2k@php.net
This bug has been superseded by bug #53375.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu May 29 14:01:29 2025 UTC