WebMail 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.
  • $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.
<?php
 
$aDomains=array("mydomain.com", "anotherdomain.com");
$sStorePath="/var/www/html/webmail/backup/";
 
include __DIR__.'/system/autoload.php';
\Aurora\System\Api::Init(true);
 
$oCoreDecorator = \Aurora\System\Api::GetModuleDecorator('Core');
$oContactsDecorator = \Aurora\System\Api::GetModuleDecorator('Contacts');
$oCalendarDecorator = \Aurora\System\Api::GetModuleDecorator('Calendar');
$oSync = new Aurora\Modules\Contacts\Classes\Csv\Sync();
 
$ul = $oCoreDecorator->GetUsers();
$ulItems = $ul["Items"];
foreach ($ulItems as $key=>$item) {
    $sEmail = $item["PublicId"];
    $oUser = $oCoreDecorator->GetUserByPublicId($sEmail);
    $iUserId = $oUser->EntityId;
    $sDomain = \MailSo\Base\Utils::GetDomainFromEmail($sEmail);
 
    if (in_array($sDomain,$aDomains)) {
        $sStoreDir = $sStorePath.$sEmail;
        mkdir($sStoreDir);
        
        $aContactsList = $oContactsDecorator->GetContacts($iUserId, 'personal', 0,0, \Aurora\Modules\Contacts\Enums\SortField::Name, \Aurora\System\Enums\SortOrder::ASC);
        $aContacts = array();
        foreach ($aContactsList["List"] as $aContactsEntry) {
            $aContacts[]=$oContactsDecorator->GetContact($aContactsEntry["UUID"],$iUserId);
        }
        file_put_contents($sStoreDir."/contacts.csv", $oSync->Export($aContacts));
 
        $aCalendarList = $oCalendarDecorator->getManager()->getUserCalendars($sEmail);
        foreach ($aCalendarList as $sCalId=>$aCalData)
        {
            $sCalendar = $oCalendarDecorator->getManager()->exportCalendarToIcs($sEmail, $sCalId);
            file_put_contents($sStoreDir."/".$sCalId.".ics", $sCalendar);
        }
    }
}

Now let's solve the opposite task - importing the data we have exported. But first, let's adjust the above sample a little. Instead of calendar ID, we use display name as a filename:

file_put_contents($sStoreDir."/".($aCalData->DisplayName).".ics", $sCalendar);

It is assumed the data are imported into a blank installation; if you attempt to import contacts into accounts which already has contacts, there may be duplicates.

<?php
 
$sStorePath="/var/www/html/webmail/backup/";
 
include __DIR__.'/system/autoload.php';
\Aurora\System\Api::Init();
\Aurora\System\Api::skipCheckUserRole(true);
 
$oCoreDecorator = \Aurora\System\Api::GetModuleDecorator('Core');
$oContactsDecorator = \Aurora\System\Api::GetModuleDecorator('Contacts');
$oCalendarDecorator = \Aurora\System\Api::GetModuleDecorator('Calendar');
$oSync = new Aurora\Modules\Contacts\Classes\Csv\Sync();
 
$dirs = array_filter(glob($sStorePath.'*'), 'is_dir');
foreach ($dirs as $dir) {
    $sEmail = basename($dir);
    $UserId = \Aurora\System\Api::getUserIdByPublicId($sEmail);
    $aCalendars = $oCalendarDecorator->GetCalendars($UserId);
    $aCalendars = $aCalendars["Calendars"];
 
    $files = array_filter(glob($sStorePath.$sEmail.'/*'), 'is_file');
    foreach ($files as $file) {
        $ffile = basename($file);
        $fname = pathinfo($ffile, PATHINFO_FILENAME);
        $fext = strtolower(pathinfo($ffile, PATHINFO_EXTENSION));
        if ($fext === "ics") {
            $sCalId = "";
            foreach ($aCalendars as $sCalIdLoop => $oCalendar) {
                if ($oCalendar->DisplayName == $fname) {
                    $sCalId = $sCalIdLoop;
                }
            }
            if ($sCalId == "") {
                $mResult = $oCalendarDecorator -> CreateCalendar($UserId, $fname, "", "#f09650");
                $sCalId = $mResult["Id"];
            }
            $mResult = $oCalendarDecorator->getManager()->importToCalendarFromIcs($sEmail, $sCalId, $file);
            var_dump($mResult);
        } elseif ($fext === "csv") {
            $oSync->Import($UserId, $file, "", "personal");
        }
    }
}