ISPConfig: Mass Email User Creation
Preparing for migration from an old email system (±15 years old FreeBSD...) to the latest ISPConfig. One of the steps is creating email users. ISPConfig has minimalist documentation and few examples.
Yes, the old system stored plain text passwords.
Plain text password list
...
email1@domainname.com plain_text_password1
email2@domainname.com plain_text_password2
email3@domainname.com plain_text_password3
...
The idea is simple: I created one user with all needed settings, and then used him as a default template for others.
PHP script for creating users via ISPConfig remote API
<?php
$remote_user = 'admin-api-user';
$remote_pass = 'admin-api-password';
$remote_url = 'https://127.0.0.1:8080/remote/json.php';
function restCall($method, $data) {
global $remote_url;
if(!is_array($data)) return false;
$json = json_encode($data);
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
if($data) curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $remote_url . '?' . $method);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
$result = restCall('login', array('username' => $remote_user, 'password' => $remote_pass, 'client_login' => false));
if($result) {
$data = json_decode($result, true);
if(!$data) die("ERROR!\n");
$session_id = $data['response'];
$client_id = 1;
$result = restCall('mail_user_get', array('session_id' => $session_id, 'primary_id' => '1'));
if(!$result){
print "Could not get client_get result\n";
print_r(json_decode($result, true));
}
$fn = fopen("p1.txt","r");
while(!feof($fn)) {
$r = fgets($fn);
$m_mail = explode(" ", $r)[0];
$m_pass = trim(explode(" ", $r)[1]);
str_replace(array("\r", "\n"), '', $m_pass);
$params = json_decode($result, true)['response'];
$params['email'] = $m_mail;
$params['login'] = $params['email'];
$params['password'] = $m_pass;
$params['maildir'] = '/var/vmail/' . explode("@", $params['email'])[1] . '/' . explode("@", $params['email'])[0];
unset($params['mailuser_id']);
unset($params['sys_userid']);
unset($params['sys_groupid']);
unset($params['sys_perm_user']);
unset($params['sys_perm_group']);
unset($params['sys_perm_other']);
$result1 = restCall('mail_user_add', array('session_id' => $session_id, 'client_id' => $client_id, 'params' => $params));
if($result1) {
print_r(json_decode($result1, true));
} else {
print "Could not get mail_user_add result\n";
print_r(json_decode($result1, true));
}
echo "<hr>";
}
fclose($fn);
$result = restCall('logout', array('session_id' => $session_id));
if(!$result) print "Could not get logout result\n";
}
?>
Human Logic, AI Syntax...
Note on Content: I'm a Systems Engineer, not a native English writer. To ensure my technical ideas are clear and accessible, I use AI tools to polish the grammar and style. The workflow is simple: I provide the logic, the code, and the real-world experience. The AI handles the "English-to-Human" translation layer. If you find a bug, that's on me. If you find a perfectly placed comma, that's probably the AI.
Comments
Post a Comment