PhP Knowledge needed: Moderate
Linux Knowledge needed: Moderate

In 2003 I wrote “Plesk Auto Email”, the first fully funtional Plesk email automation suite. Now, I am going to show you how to do it. You will need a dedicated server with Linux, Plesk 7.5 and higher, php5, mysql5, and root access. This will show you how to code the auto creation, I am not going into deletions and edits at this time because I don’t have all night to blog.

First, put this into your mysql5 database:

CREATE TABLE `create_users` (
`command_id` int(20) NOT NULL auto_increment,
`command` text NOT NULL,
PRIMARY KEY (`command_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Now, in the software you wish to integrate Plesk email into, put something like this into your registration parsing script:

// The below values are true or false.
$mailboxer = "true"; //allow mailbox
$cpaccess = "true"; //allow user to have limited cp access and webmail control?
$multisessions = "true"; //allow multi-sessions? If false,t ehn is the user forgets to logout and goes back, thaey cannot log back in for about an hour.
$redirector = "false"; //allow mail redirects?
$quota = 100; // membership level 1 quota in MB
$PSAquota = $quota * 1000;
$cmd1 = '/usr/local/psa/bin/mail.sh -c '.$email.' -multiple-sessions '.$multisessions.' -passwd_type plain -passwd "'.$password.'" -cp_access '.$cpaccess.' -mailbox '.$mailboxer.' -mbox_quota "'.$PSAquota.'" -redirect '.$redirector;
$insert_data_res1 = mysql_query('INSERT INTO `create_users` SET `command`="' . $cmd1 . '"') or die(mysql_error());

Now the reason to go through all of this is because I don’t like to run shell commands in php, so, I store the command in the database and write a cron script that is only able to be run by root that will then create the email address. You see, only root can perform this task for security reasons in Plesk.

Now here is an example cron script:

< ?php
// Chown to root:root
// CHMOD this to 700. Only root should have access!
require("../configuration/config.php");
require("../configuration/language.php");
$items="SELECT * FROM `create_users` ORDER BY command_id DESC";
$items=mysql_query($items) or die(mysql_error( ));
if (mysql_num_rows($items) < 1) {
echo 'No accounts in database need created, shutting down until next time.';
die;
} else {
$i = 0;
$output = 'No command given from database.';
while ($row = mysql_fetch_array($items)) {
$command_id = $row['command_id'];
$command = $row['command'];
exec($command,$output);
}
$sql = 'TRUNCATE TABLE `create_users`';
$main = mysql_query($sql);
mysql_close($conn);
}
exit;
?>

Now, this was not meant for beginners. This is an example showing coders a basic guideline of automating Plesk email creation. Sorry for no line breaks in the code exerpts, wordpress kept goofing up.

Bookmark and Share

Tags: , , , , , , ,

Similar Posts: