创建postfix用户的php脚本
#!/usr/local/bin/php -f
<?
/**************************
* create postfix user
* script by silence.z
* (c) 2005 iturn.com.cn
*************************/
$mail_box_root = "/usr/postfix/";
$mail_domain = $argv[1];
$mail_user_name = $argv[2];
$mail_user_pass = $argv[3];
$mysql_host = "localhost";
$mysql_user = "postfix";
$mysql_pass = "iturn.heima";
$mysql_db = "postfix";
//check if argv I need is input
if (empty($mail_domain) || empty($mail_user_name) || empty($mail_user_pass)) {
echo "Usage: ";
echo "\tcreate_mail_user DOMAIN USERNAME PASSWORD [OPTION: SIZE]\n";
echo " DOMAIN\t\tDomain name witch want user to place.eg: wuhongsheng.com\n";
echo " USERNAME\t\tUsername,eg: silence\n";
echo " PASSWORD\t\tPassword,eg: 123456\n";
echo " SIZE\t\t\tUser’s mailbox size.eg: 1024000";
echo "\n";
exit();
}
//check if domain directory exists
if (!file_exists($mail_box_root.$mail_domain)) {
echo "ERROR:\tdomain not exists!\n";
exit();
}
//check if user directory exists
if (file_exists($mail_box_root.$mail_domain."/".$mail_user_name)) {
echo "ERROR:\tuser already exists!\n";
exit();
}
//create user directory
if (!mkdir($mail_box_root.$mail_domain."/".$mail_user_name,0777)) {
echo "ERROR:\tfailed to create user directory,check permission please!\n";
exit();
}
//create user mailbox
exec("/usr/local/bin/maildirmake ".$mail_box_root.$mail_domain."/".$mail_user_name."/Mail");
exec("/usr/sbin/chown -R postfix:postfix ".$mail_box_root.$mail_domain."/".$mail_user_name."/Mail");
//write user to db
mysql_connect($mysql_host,$mysql_user,$mysql_pass);
mysql_select_db($mysql_db);
$query = mysql_query("SELECT `domain_id` FROM `domaininfo` WHERE `domain`=’$mail_domain’");
if (!$domain_id = mysql_fetch_array($query)) {
echo "ERROR:\tdomain not exists!\n";
exit();
}
$sql = "INSERT INTO `userinfo`
(`userid`,`domain_id`,`address`,`passwd`,`quota`,`active`,`homedir`,`maildir`,`create_time`) VALUES
(’$mail_user_name’,{$domain_id[0]},’$mail_user_name@$mail_domain’,encrypt(’$mail_user_pass’),209715200,1,’"
.$mail_box_root.$mail_domain."/".$mail_user_name."/’,'".$mail_box_root.$mail_domain."/".$mail_user_name."/Mail/’,now())";
mysql_query($sql) or die(
"ERROR:\tcan’t insert user to database\n"
);
echo "NOTICE:\tuser $mail_user_name created successful!";
exit();
?>