MailSuite Pro documentation

Batch export of contacts and calendars

The code provided here will allow you to export contacts and calendars for the users. It can be very helpful if you need to backup user data.

To use the code, you will need to supply the following parameters:

$aDomains

Contacts and calendars will only be exported for users in these domains. It's not required to have the domain actually added in AdminPanel - if user is in default domain, but their email address belongs to one of the listed domains, the account will be processed.

$sStorePath

Filesystem path contacts and calendars will be saved to.

For each exported user, a subdirectory will be created, with email address used for directory name. Contacts are saved as contacts.csv file, and each calendar is saved as ICS file, where calendar ID is used as file name.

$aDomains=array("mydomain.com", "anotherdomain.com");
$sStorePath="/var/www/webmail/backup/";

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

if (class_exists('CApi') && CApi::IsValid())
{
    $oApiUsersManager = CApi::Manager('users');
    $oApiContactsManager = CApi::Manager('contacts');
    $oApiCalendarManager = CApi::Manager('calendar');

    $aUserlist = $oApiUsersManager->getDefaultAccountList();
    foreach ($aUserlist as $oUser)
    {
        $sEmail = $oUser->email;
        $sDomain = \api_Utils::GetDomainFromEmail($sEmail);
        if (in_array($sDomain,$aDomains))
	{
            $oAccount = $oApiUsersManager->getAccountByEmail($sEmail);

            $sStoreDir = $sStorePath.$sEmail;
            mkdir($sStoreDir);

            $sContacts = $oApiContactsManager->export($oAccount->IdUser,"csv");
            file_put_contents($sStoreDir."/contacts.csv", $sContacts);

            $aCalendarList = $oApiCalendarManager->getUserCalendars($oAccount);
            foreach ($aCalendarList as $sCalId=>$aCalData)
            {
                $sCalendar = $oApiCalendarManager->exportCalendarToIcs($oAccount, $sCalId);
                file_put_contents($sStoreDir."/".$sCalId.".ics", $sCalendar);
            }
        }
    }
}
else
{
    echo 'AfterLogic API isn\'t available';
}

To import data back into MailSuite Pro, you can use samples for importing contacts and importing calendars.