WebMail Pro 8 documentation

Batch delete user accounts

One of our valued customers have asked if there's a way to batch delete user accounts with PHP API. The idea was to get a full list of user accounts of WebMail Pro 8, save such a list as a text file (one email address per line), then edit a list so it only contains user accounts to be deleted, and remove user accounts which are found in the list. As the scripts for doing that could prove useful to other customers, we're posting the solution below.

The first step is to retrieve full list of user accounts:

<?php
function entry_add($str, $add=TRUE) {
    file_put_contents("./users.txt", $str."\r\n", $add?FILE_APPEND:0);
}
include '/var/www/webmail/system/autoload.php';
\Aurora\System\Api::Init(true);
$oCoreModule = \Aurora\System\Api::GetModule("Core");
$ul = $oCoreModule->getUserList();
$ulItems = $ul["Items"];
foreach ($ulItems as $key=>$item) {
    entry_add($item["PublicId"]);
}

Make sure to provide valid path to system/autoload.php file of your WebMail Pro 8 installation.

The list will be created in the same directory where the scripts resides, and called users.txt.

Now, you'll need to copy that file into users-del.txt, edit it so it only contains user accounts to be deleted, and put it alongside with the 2nd script which will do the deletion:

<?php
$aDelUsers = file('./users-del.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
include '/var/www/webmail/system/autoload.php';
\Aurora\System\Api::Init(true);
$oCoreModule = \Aurora\System\Api::GetModule("Core");
foreach ($aDelUsers as $sEmail) {
    $oUser = $oCoreModule->GetUserByPublicId($sEmail);
    if ($oUser !== null) {
        $iUserId = $oUser->EntityId;
        $oCoreModule->DeleteUser($iUserId);
    }
}