WebMail Pro 8 documentation

Getting number of unread mails in user accounts

In this tutorial, we provide guidelines on retrieving email-related information on user accounts. The sample code allows you to go through Inbox folders in all of the user accounts, and for each Inbox, total number of mails as well as number of unread mails will be displayed.

Full sample will be provided at the bottom of the page - but first, we'll go through the code step-by-step.

The starting point is loading and initializing API of WebMail Pro 8:

include './system/autoload.php';
\Aurora\System\Api::Init(true);

For the sake of simplicity, we're assuming the code is located in root directory of WebMail Pro 8 installation - if not, you'll need to adjust path to system/autoload.php file accordingly.

Note that Init method has its parameter set to true - it means API is used in superadmin mode, that allows accessing all the user accounts without having to supply their credentials explicitly.

We'll be making use of Core and Mail modules:

$oCoreModule = \Aurora\System\Api::GetModule("Core");
$oMailModule = \Aurora\System\Api::GetModule("Mail");

We need to obtain full list of users, which is done as follows:

$ul = $oCoreModule->getUserList();
$ulItems = $ul["Items"];

Then we go through the list, and for each user, we locate their email address and get the respective Account object:

foreach ($ulItems as $key=>$item) {
     $sEmail = $item["PublicId"];
     $oUser = $oCoreModule->GetUserByPublicId($sEmail);
     $iUserId = $oUser->EntityId;
     $oAccount = $oCoreModule->GetAccountUsedToAuthorize($sEmail);
     $iAcctId = $oAccount->EntityId;
     ...
}

The core part of this sample is getting statistics of user's Inbox folder. Please bear in mind that this information isn't cached in the database, WebMail Pro 8 performs actual IMAP login - so retrieving such statistic for any significant number of accounts may take a while and you may wish to split the output into pages and/or log into a file.

$aFolders = $oMailModule->GetRelevantFoldersInformation( $iAcctId, array("INBOX"), true);
echo "<p><b>".$sEmail."</b><br />Total: ".$aFolders["Counts"]["INBOX"][0]."<br />Unread: ".$aFolders["Counts"]["INBOX"][1]."</p>";

Below, please find the complete sample code. It includes an additional check for any error when querying an account, such an account will be skipped.

include './system/autoload.php';
\Aurora\System\Api::Init(true);
$oCoreModule = \Aurora\System\Api::GetModule("Core");
$oMailModule = \Aurora\System\Api::GetModule("Mail");
$ul = $oCoreModule->getUserList();
$ulItems = $ul["Items"];
foreach ($ulItems as $key=>$item) {
    $sEmail = $item["PublicId"];
    $oUser = $oCoreModule->GetUserByPublicId($sEmail);
    $iUserId = $oUser->EntityId;
    $oAccount = $oCoreModule->GetAccountUsedToAuthorize($sEmail);
    $iAcctId = $oAccount->EntityId;
    try {
         $aFolders = $oMailModule->GetRelevantFoldersInformation( $iAcctId, array("INBOX"), true);
         echo "<p><b>".$sEmail."</b><br />Total: ".$aFolders["Counts"]["INBOX"][0]."<br />Unread: ".$aFolders["Counts"]["INBOX"][1]."</p>";
    }
    catch(Exception $e)
    {
         echo "<p><b>".$sEmail."</b> could not be accessed.";
    }
}