Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP imap_search not detecting all messages in gmail inbox

Tags:

php

gmail

imap

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

like image 533
Ben Avatar asked Jan 06 '11 07:01

Ben


1 Answers

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.

like image 51
Ben Avatar answered Sep 28 '22 19:09

Ben