WebMail Pro 7 documentation

Accessing calendars and events

Aside from the need to log into WebMail account and obtain information about number of unread mails or headers of few last email messages, you might need to retrieve calendar-related information - for example, to get a list of events scheduled for specific date range.

Below, you'll find a basic example of how this can be done. The code uses WebMail API, and it looks similar to code found at "Logging in programmatically" page, since the first thing which should be done regardless of specific task is to log user in. Then we're retrieving list of calendars and their IDs, and get a list of events within a range of dates. Internally, all events are stored in GMT timezone, which is why we'll have to modify date and time a bit.



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

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

    $oCals = null;

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

        $oAccount = $oApiIntegratorManager->LoginToAccount($sEmail, $sPassword);
        if ($oAccount)
        {
            $oApiCalManager = CApi::Manager("calendar");
            $oCals = $oApiCalManager->getCalendars($oAccount);
            $oCalIds = array();
            if ($oCals) {
                foreach ($oCals as $oCal) {
                $oCalIds[]=$oCal["Id"];
                }
            }
            $MYTZ = new DateTimeZone('Etc/GMT+0');
            $oStart = new DateTime("2013-11-27 00:00:00", $MYTZ);
            $oEnd = new DateTime("2013-11-28 23:59:59" , $MYTZ);
            $oEvents = array();
            $oEvents = $oApiCalManager->getEvents($oAccount,$oCalIds,$oStart->format("U"),$oEnd->format("U"));
            if ($oEvents) {
                //echo "<pre>"; print_r($oEvents); echo "</pre>";
                foreach ($oEvents as $oEvent) {
                echo "<br />Start: ".$oEvent["start"];
                echo "<br />End: ".$oEvent["end"];
                echo "<br />Subject: ".$oEvent["subject"];
                echo "<br />Description: ".$oEvent["description"];
                echo "<br />";
                }
            }
        }
        else
        {
            // login error
            echo $oApiIntegratorManager->GetLastErrorMessage();
        }
    }
    catch (Exception $oException)
    {
        // login error
        echo $oException->getMessage();
    }
}
else
{
    echo 'AfterLogic API isn\'t available';
}