PHP email admin script

If you’ve read the article about my email server setup you quickly realize that adding a new email account is quite onerous, a combination of adding records to tables in a MySQL database and creating folders on the harddrive. This can easily be automated with for instance PHP and I’ll show you how below.

$GLOBALS['db_host'] = 'localhost'; 
$GLOBALS['db_user'] = 'user';
$GLOBALS['db_pass'] = 'pwd';
$GLOBALS['db_name'] = 'mailserver';
$GLOBALS['vmail_dir']	= "/var/spool/vmail";


	shell_exec("mkdir {$GLOBALS['vmail_dir']}");

function getDomainId($domain){
	return Mysql::tbl('virtual_domains')->getValue($domain, 'name', 'id');

function domainExists($domain){
	$res = Mysql::tbl('virtual_domains')->findOne($domain, 'name');
	return empty($res) ? false : true;

function emailExists($email){
	$res = Mysql::tbl('virtual_users')->findOne($email, 'email');
	return empty($res) ? false : true;

function aliasExists($alias){
	$res = Mysql::tbl('virtual_aliases')->findOne($alias, 'source');
	return empty($res) ? false : true;

The above script (email-admin.php) makes use of my simple MySQL class and will be included by the below scripts which are the ones that are actually called from the command line.

$user 		= $_SERVER['argv'][1];
$password 		= $_SERVER['argv'][2];
$domain 		= $_SERVER['argv'][3];

	shell_exec("mkdir {$GLOBALS['vmail_dir']}/$domain");

shell_exec("chmod 700 {$GLOBALS['vmail_dir']}/$domain");

	Mysql::tbl('virtual_domains')->insertArray(array('name' => $domain));	

$email 	= "$user@$domain";

	shell_exec("mkdir {$GLOBALS['vmail_dir']}/$domain/$user");
	foreach(array('new', 'cur', 'tmp') as $dir){
			shell_exec("mkdir {$GLOBALS['vmail_dir']}/$domain/$user/$dir");
		shell_exec("chmod 700 {$GLOBALS['vmail_dir']}/$domain/$user/$dir");
shell_exec("chmod 700 {$GLOBALS['vmail_dir']}/$domain/$user");

	$domain_id = getDomainId($domain);
	Mysql::tbl('virtual_users')->insertArray(array('email' => $email, 'password' => md5($password), 'domain_id' => $domain_id));

shell_exec("chown -R virtual:virtual /var/spool/vmail");

This one will add a new email address to the database and /var/spool/vmail. Note that we’re doing the directory checks and creating non-existent directories independently of what already exists in the database or not.

This is due to the fact that you might already have information in the database that is not reflected in the vmail folder so it can’t hurt to try and create the folders anyway.

Example usage: php email-add.php henrik password

$alias			= $_SERVER['argv'][1];
$original		= $_SERVER['argv'][2];
$password 		= $_SERVER['argv'][3];
$domain 		= $_SERVER['argv'][4];

$alias_email = "$alias@$domain";

shell_exec("php email-add.php $original $password $domain");

if(!aliasExists($alias_email) && domainExists($domain)){
		'domain_id' 	=> getDomainId($domain), 
		'source' 		=> $alias_email, 
		'destination' 	=> "$original@$domain"

Finally the add alias script which should not be used if you alias to a domain not hosted on the server in question. Proper usage is to for instance alias from to when domain is hosted on the server in question.

Example usage: php email-alias.php henrik henrik.sarvell password

Note how in this case the address doesn’t need to exist before the henrik alias is added since we call email-add.php from inside the email-alias.php script.

Related Posts

Tags: , , , ,