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:
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 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 module decorators:
$oCoreDecorator = \Aurora\Modules\Core\Module::Decorator();
$oMailDecorator = \Aurora\Modules\Mail\Module::Decorator();
We need to obtain full list of users, which is done as follows:
$ul = $oCoreDecorator->GetUsers();
$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 = $oCoreDecorator->GetUserByPublicId($sEmail);
$iUserId = $oUser->EntityId;
$oAccount = $oCoreDecorator->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 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 = $oMailDecorator->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);
$oCoreDecorator = \Aurora\Modules\Core\Module::Decorator();
$oMailDecorator = \Aurora\Modules\Mail\Module::Decorator();
$ul = $oCoreDecorator->GetUsers();
$ulItems = $ul["Items"];
foreach ($ulItems as $key=>$item) {
$sEmail = $item["PublicId"];
$oUser = $oCoreDecorator->GetUserByPublicId($sEmail);
$iUserId = $oUser->EntityId;
$oAccount = $oCoreDecorator->GetAccountUsedToAuthorize($sEmail);
$iAcctId = $oAccount->EntityId;
try {
$aFolders = $oMailDecorator->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.";
}
}