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.

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

include_once('/opt/lib/Mysql.php');

if(!is_dir($GLOBALS['vmail_dir']))
	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.

<?php
include_once('email-admin.php');		
$user 		= $_SERVER['argv'][1];
$password 		= $_SERVER['argv'][2];
$domain 		= $_SERVER['argv'][3];

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

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

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

$email 	= "$user@$domain";

if(!is_dir("{$GLOBALS['vmail_dir']}/$domain/$user")){
	shell_exec("mkdir {$GLOBALS['vmail_dir']}/$domain/$user");
	foreach(array('new', 'cur', 'tmp') as $dir){
		if(!is_dir("{$GLOBALS['vmail_dir']}/$domain/$user/$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");

if(!emailExists($email)){
	$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 domain.com

<?php
include_once('email-admin.php');
$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)){
	Mysql::tbl('virtual_aliases')->insertArray(array(
		'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 firstname@domain.com to firstname.lastname@domain.com when domain is hosted on the server in question.

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

Note how in this case the henrik.sarvell@domain.com 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: , , , ,