PDA

View Full Version : Count IMAP folder messages (read and unread) with PHP?



JAB Creations
07-26-2008, 04:28 PM
I've been working with PHP and the IMAP module and I'm just getting more and more lost as I try to achieve what I expected to be simple stuff such as listing the folder names, in example...

$boxes = imap_getmailboxes($mbox, "{mail.domain.com}", "*");

foreach($boxes as $val) {
$piece1 = explode("}", $val->name);
$piece2 = explode(".", $piece1[1]);

if (empty($piece2[1])) {echo '<div><b>Inbox</b></div>';}

Could someone please give me some direction as to what part of IMAP I should use to determine the message count and unread message count per folder?

thetestingsite
07-27-2008, 12:45 AM
I would look into using imap_mailboxmsginfo() (http://us3.php.net/manual/en/function.imap-mailboxmsginfo.php) . I can't give you an example code at the moment, but I will work on posting something for you later on.

Hope this helps.

After looking into it a bit more, you might want to look into using imap_num_msg() (http://us3.php.net/manual/en/function.imap-num-msg.php) and imap_num_recent (http://us3.php.net/manual/en/function.imap-num-recent.php) as it will give you the total number of messages in the mailbox as well as the number of recent (unread) messages.

JAB Creations
07-27-2008, 02:33 AM
Nah...I'm good now, thanks though. ;)

function email_folder_count($mbox, $folder)
{
//$status = imap_status($mbox, "{imap.example.com}$folder", SA_ALL);
$status = imap_status($mbox, "{imap.example.com}$folder", SA_ALL);
if ($status)
{
echo '('.$status->messages.' Messages, '.$status->unseen.' New)';
}
else
{
echo "imap_status failed: " . imap_last_error() . "\n";
}
return $status;
// EXAMPLE
//email_folder_count($mbox, 'INBOX.Trash');
}

thetestingsite
07-27-2008, 02:36 AM
Thanks for posting that function, I think it may come in handy.

JAB Creations
07-27-2008, 02:45 AM
No prob...I'll most likely post a bit on php.net once I get my email client working. If you'd like an early gander shot me a private message...it's all read-only at the moment on a spam infested account...but the functionality is starting to make it viable. :-)