MailSuite Pro documentation

Checking account Inbox with PHP API

The idea of the sample is to demonstrate how PHP API can be used to retrieve information about account Inbox: total number of mails, number of unread mails, and a list of N newest messages in Inbox (date, sender, subject). This could be very handy if MailSuite Pro is integrated as a part of a bigger web application, and users can have their account status displayed on a dashboard of some kind.

We will make use of PHP API and will invoke Core and Mail decorators. We're assuming the sample is placed in root directory of MailSuite Pro installation; if not, the filesystem path in the first line of the sample needs to be adjusted accordingly.

include './system/autoload.php';
\Aurora\System\Api::Init(true);
$oCoreDecorator = \Aurora\Modules\Core\Module::Decorator();
$oMailDecorator = \Aurora\Modules\Mail\Module::Decorator();

We'll reuse the same approach we used to get number of unread mails in user accounts, but this time, for specific account defined by email address. User and account need to be located, first:

$sEmail = "user@domain.com";
$oUser = $oCoreDecorator->GetUserByPublicId($sEmail);
$iUserId = $oUser->EntityId;
$oAccount = $oCoreDecorator->GetAccountUsedToAuthorize($sEmail);
$iAcctId = $oAccount->EntityId;

Once we have account and user object, we can retrieve and display Inbox statistics:

$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>";

The next part is retrieving last 3 messages from the server:

$aMessages = $oMailDecorator->GetMessages( $iAcctId, "INBOX", $aFolders["Counts"]["INBOX"][0]-3, 3) ->GetAsArray();

We will only need small subset of the information returned so we'll get it into a separate array:

$arr = []; array_reverse($aMessages);

Note that we're reversing $aMessages array as items there are in their natural Inbox order, but we wish to display the latest item first.

foreach ($aMessages as $oMsg) {
    array_unshift($arr, array(
        "date"=>date("j M Y, h:i",$oMsg->getReceivedOrDateTimeStamp()),
        "from"=>$oMsg->GetFrom()->GetAsArray()[0]->GetEmail(),
        "subject"=>$oMsg->GetSubject()
    ));
}

Please bear in mind that date here is returned in UTC, you may wish to adjust it to some particular timezone.

We have all the information we need to show a list of messages:

foreach ($arr as $item) {
    echo "<p>".$item["from"]."<br />".$item["date"]."<br />".$item["subject"]."</p>"; 
}

Below, you can find the complete sample, with basic error handling added:

 include './system/autoload.php';
\Aurora\System\Api::Init(true);
$oCoreDecorator = \Aurora\Modules\Core\Module::Decorator();
$oMailDecorator = \Aurora\Modules\Mail\Module::Decorator();
    
$sEmail = "user@domain.com";
$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>";
    
    $aMessages = $oMailDecorator->GetMessages( $iAcctId, "INBOX", $aFolders["Counts"]["INBOX"][0]-3, 3) ->GetAsArray();
    $arr = []; array_reverse($aMessages);
    foreach ($aMessages as $oMsg) {
        array_unshift($arr, array(
        "date"=>date("j M Y, h:i",$oMsg->getReceivedOrDateTimeStamp()),
        "from"=>$oMsg->GetFrom()->GetAsArray()[0]->GetEmail(),
        "subject"=>$oMsg->GetSubject()
        ));
    }
    foreach ($arr as $item) {
        echo "<p>".$item["from"]."<br />".$item["date"]."<br />".$item["subject"]."</p>"; 
    }
}
catch(Exception $e)
{
    echo "<p><b>".$sEmail."</b> could not be accessed.";
}