MailSuite Pro 7 documentation

Getting new or last messages

We have already provided sample code for basic overview of a specific folder. But what if you need to get information about messages themselves rather than folder - for example, headers of last 5 mails? That can be done with the API, too:

<?php

    include_once __DIR__.'/../libraries/afterlogic/api.php';

    if (class_exists('CApi') && CApi::IsValid())
    {
        // data for logging into account
        $sEmail = 'user@domain.com';
        $sPassword = 'PassWord';

        $sFolder = 'INBOX';
        $iOffset = 0;
        $iLimit = 5;

        $oCollection = null;

        try
        {
            $oApiIntegratorManager = CApi::Manager('integrator');

            $oAccount = $oApiIntegratorManager->LoginToAccount($sEmail, $sPassword);
            if ($oAccount)
            {
                $oApiMailManager = CApi::Manager('mail');
                $oCollection =  $oApiMailManager->getMessageList($oAccount, $sFolder, $iOffset, $iLimit);

                if ($oCollection)
                {

                    echo '<b>'.$oAccount->Email.':</b><br />';
                    echo '<pre>';
                    echo 'Folder:   '.$sFolder."\n";
                    echo 'Count:    '.$oCollection->MessageCount."\n"; // $oCollection->MessageResultCount
                    echo 'Unread:   '.$oCollection->MessageUnseenCount."\n";
                    echo 'List:   '."\n";

                    $oCollection->ForeachList(function ($oMessage) {
                        $oFrom = $oMessage->GetFrom();
                        echo "\t".htmlentities($oMessage->GetUid().') '.$oMessage->GetSubject().($oFrom ? ' ('.$oFrom->ToString().')' : ''))."\n";
                    });

                    echo '</pre>';
                }
                else
                {
                    echo $oApiMailManager->GetLastErrorMessage();
                }
            }
            else
            {
                echo $oApiIntegratorManager->GetLastErrorMessage();
            }
        }
        catch (Exception $oException)
        {
            echo $oException->getMessage();
        }
    }
    else
    {
        echo 'AfterLogic API isn\'t available';
    }