MailSuite Pro 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 MailSuite Pro, 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 '/opt/afterlogic/html/system/autoload.php';
\Aurora\System\Api::Init(true);
$oCoreDecorator = \Aurora\System\Api::GetModuleDecorator('Core');
$ul = $oCoreDecorator->GetUsers();
$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 MailSuite Pro 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 '/opt/afterlogic/html/system/autoload.php';
\Aurora\System\Api::Init(true);
$oCoreDecorator = \Aurora\System\Api::GetModuleDecorator('Core');
foreach ($aDelUsers as $sEmail) {
    $oUser = $oCoreDecorator->GetUserByPublicId($sEmail);
    if ($oUser !== null) {
        $iUserId = $oUser->EntityId;
        $oCoreDecorator->DeleteUser($iUserId);
    }
}