help centre
For more info visit status.atmail.com

How can we help?


Search our knowledge base for answers to
common questions and latest updates.



My activities New request

Follow

Purging users via CSV

Stewart -

Should you need to purge a large set of users from your Atmail system, you can use the purge-users-csv.php script to batch delete via a CSV file.You will find the script at /usr/local/atmail/webmail/utilities/tools/purge-users-csv.php.

The usage is simple:

[root@test ~]# cd /usr/local/atmail/webmail/utilities/tools/
[root@test tools]# php purge-users-csv.php
Usage: php delete-users-csv.php [/path/to/users.csv] [admin-username]

Where /path/to/users.csv contains a list of users, seperated by a newline.

Example
account1@domain.com
account2@domain.com
account3@domain.com

This script will remove all the users database entries, clear the users maildir and purge the account from the system.

<?php
/*
* Copyright (c) 2009-2010 ATMAIL. All rights reserved
* See http://atmail.com/license.php for license agreement
*/
/**
 * Delete users from csv file
 *
 * csv format:
 * username
 *
 * @author : Ben Duncan
 */
 
require_once('../nfc-bootloader.php');
require_once('Mail/RFC822.php');
 
if ($_SERVER['argc'] < 3) {
    die("Usage: php delete-users-csv.php [/path/to/users.csv] [admin-username]\n");   
}
 
$file = $_SERVER['argv'][1];
if (is_file($file) && !$fh = fopen($file, 'r')) {
    die("Could not open $file\n");   
}
 
require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
//$loader->registerNamespace('App_'); //for model namespacing
$loader->setFallbackAutoloader(true);
$loader->suppressNotFoundWarnings(false);
Zend_Loader::loadFile('Atmail/General.php', null, true);
Zend_Loader::loadFile('Atmail/Exception.php', null, true);
 
 
$siteBaseUrl = substr($_SERVER["SCRIPT_NAME"],0,strpos($_SERVER["SCRIPT_NAME"],'index.php'));
 
Zend_Registry::set( 'siteBaseUrl', $siteBaseUrl );
 
// Get database configuration   
$dbConfig = new Zend_Config_Ini('config/dbconfig.ini', 'production');
Zend_Registry::set('dbConfig', $dbConfig);
$dbAdapter = Zend_Db::Factory($dbConfig->database);
$dbAdapter->query("SET NAMES 'utf8'");
Zend_Db_Table::setDefaultAdapter($dbAdapter);
Zend_Registry::set('dbAdapter', $dbAdapter);
 
//Get main configuration from database
$config = new Atmail_Config_Mysql($dbConfig, true);
 
//$config->global['cacheEnabled'] = false;
$config->global['allowLocalPop3'] = true; //for testing/debugging
$config->global['allowLocalImap'] = true; //for testing/debugging
Zend_Registry::set('config', $config);
 
$locale = new Zend_Locale();
Zend_Registry::set('Zend_Locale', $locale);
 
$log = new Atmail_Log();
$log->addPriority('atmail', 10);
$log->addPriority('sysadmin', 20);
$log->addPriority('imap', 30);
$log->addPriority('firebug', 40);
 
$logBasePath = APP_ROOT . 'log' . DIRECTORY_SEPARATOR;
Zend_Registry::set('log', $log);
 
unset($config);
unset($settings);
 
require_once('../../application/models/api.php');
$_SERVER['PHP_AUTH_USER'] = $_SERVER['argv'][2];
$api = new api( array('directApi' => 1) );
 
while (false !== $users = fgetcsv($fh, 10000, ",")) {
    foreach($users as $user)
    {
        $user = trim($user);
 
        // Skip blank lines
        if ($user == '')
            continue;
 
        $res = array();
        try {
            echo "Going to delete " . $user . ' ';
 
            $res = $api->userDelete($user);
        } catch( Exception $e ) {
            fwrite(STDOUT, "ERROR: " . $e->getMessage() . "\n");
        }
 
        fwrite(STDOUT, ((array_key_exists('status', $res) && $res['status'] == 'failed') ? 'FAILED' : 'OK') . "\n");
 
    }
}
 
fwrite(STDOUT, "DONE\n");
Have more questions? Submit a request

Comments


Contact our support team


+61 (7) 5357 6605       support@atmail.com