Skip to content

Instantly share code, notes, and snippets.

@scintill
Created March 4, 2012 02:52
Show Gist options
  • Save scintill/1970217 to your computer and use it in GitHub Desktop.
Save scintill/1970217 to your computer and use it in GitHub Desktop.
Dump message count from IMAP server
<?php
$host = '{imap.gmail.com:993/imap/ssl}';
$user = '';
$pass = '';
$imap = imap_open($host, $user, $pass, OP_READONLY | OP_HALFOPEN) or die("Can't connect: ".imap_last_error());
header('Content-Type: application/json');
echo json_encode(getCounts($imap, $host));
function getCounts($imap, $host) {
$counts = array();
foreach (imap_getmailboxes($imap, $host, '*') as $mailbox) {
$status = imap_status($imap, $mailbox->name, SA_MESSAGES | SA_RECENT | SA_UNSEEN);
unset($status->flags);
$niceName = str_replace($host, '', $mailbox->name);
$counts[$niceName] = $status;
}
ksort($counts);
return $counts;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment