6

When I run a very simple imap_search on my GMail inbox, the search returns less messages than it should.

Here is the script that anyone with a GMail account can run.

$host = '{imap.gmail.com:993/imap/ssl}';
$user = 'foo';
$pass = 'bar';

$imapStream = imap_open($host,$user,$pass) or die(imap_last_error());

$messages = imap_search($imapStream,"ALL");

echo count($messages);

imap_close($imapStream);

This returns 39 messages. But, I've got 100 messages in my inbox, some bundled in conversations, some forwarded from another account (SquirrelMail).

Can anyone duplicate these results, and/or tell me what's going on?


Other server strings I've tried, all returning the same results:

{imap.gmail.com:993/imap/ssl/novalidate-cert}
{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX
{imap.gmail.com:993/imap/ssl}INBOX

GMail's IMAP feature support: http://mail.google.com/support/bin/answer.py?hl=en&answer=78761

Ben
  • 54,723
  • 49
  • 178
  • 224

2 Answers2

8

After significant hair loss, I've found the answer. It was a misleading UI.

GMail groups one's messages into "Conversations" by default. These conversations can include archived messages.

So, for example, Bob's inbox looks like there's 4 conversations of 25 messages, which should apparently return 100 inbox messages. In reality, 60 of the messages are in the archive (not the inbox), so the imap_search() returns 40. These messages are magically pulled out of the archive and placed into inbox conversations.

In the Settings->General menu, you can toggle conversation view, which will put all of those naughty archived messages back where they belong, and show your true inbox view.

Ben
  • 54,723
  • 49
  • 178
  • 224
0

imap_search criteria ALL - return all messages matching the rest of the criteria , so i ask you where is the rest of the criteria ?

You could use imap_sort($imapStream, 'SORTDATE', 0); ( imap_sort - Gets and sorts message numbers by the given parameters imap_sort ) .


Edit , here is some code that goes thru all messages in you're inbox , instead of imap_num_msg , you could use imap_sort as stated before , so you get you're inbox sorted if you like .

<?php
    $imap = imap_open("{mail.yourserver.com:143}INBOX", "username", "password");
    $message_count = imap_num_msg($imap);

    for ($i = 1; $i <= $message_count; ++$i) {
        $header = imap_header($imap, $i);
        $body = trim(substr(imap_body($imap, $i), 0, 100));
        $prettydate = date("jS F Y", $header->udate);

        if (isset($header->from[0]->personal)) {
            $personal = $header->from[0]->personal;
        } else {
            $personal = $header->from[0]->mailbox;
        }

        $email = "$personal <{$header->from[0]->mailbox}@{$header->from[0]->host}>";
        echo "On $prettydate, $email said \"$body\".\n";
    }

    imap_close($imap);
?>
Poelinca Dorin
  • 9,577
  • 2
  • 39
  • 43
  • @poelinca - "ALL" is the criterion, I want all of the messages in the inbox. I tried `imap_sort` just now, and it returned the same results. :/ – Ben Jan 06 '11 at 07:49
  • please see my update answer , looking at php.net ALL shouldn't be used alone , that would mean you can use ANY as a standalone criteria witch whont work would it ? – Poelinca Dorin Jan 06 '11 at 07:50
  • @poelinca - `ALL` by itself returns the same results as when I split it up with criteria. Do you have a source when you say "shouldn't be used alone"? Also, ANY won't work because isn't in the list of criteria. – Ben Jan 06 '11 at 07:57
  • if you just whant to display the number of messages you can use imap_sort or imap_num_msg . ( i still don't think ALL should work as a standalone criteria , since you allready having problems getting all emails ) – Poelinca Dorin Jan 06 '11 at 08:01
  • @poelinca - I understand what you're saying, but I don't know any other way to return "all inbox messages". – Ben Jan 06 '11 at 08:11
  • @poelinca - I tried `imap_sort`, `imap_num_msg`, and `imap_search`, which all return 39. :| Thanks for the tips though. – Ben Jan 06 '11 at 08:12
  • for me both imap_search and imap_sort return the correct number – Poelinca Dorin Jan 06 '11 at 08:20