Add mail users using php?


Well-Known Member
Mar 27, 2003
I know it's an old topic, however....

I know this is an old topic, however, I needed a script to add the e-mail accounts and ran accross this thread. The code posted was all mixed up with "amps" etc. so it could not be copied directly from the thread. I've gone ahead and cleaned up the code and fixed some problems that were in it like the quota's not being setup, and some variables that were wrong.

Here is the code for each file:




$newemail = "$newuser@$domain";
$mpassword = $password2;

$link = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname) or die("Could not select database");
$query="SELECT username FROM $dbtable WHERE((username = '$newuser'))";
$result= mysql_query($query,$link);
//echo mysql_num_rows($result);
if(mysql_num_rows($result) == 0)
$query="INSERT INTO $dbtable(name,username,password,email) VALUES ('$name','$newuser','$mpassword','$replymail');";
$socket = fsockopen($host,2082);
$authstr = "$cpaneluser:$cpanelpass";
$pass = base64_encode($authstr);
$in = "GET /frontend/$cpaneltheme/mail/doaddpop.html?email=$newemail&domain=$domain&password=$mpassword&quota=$quota\r\n HTTP/1.0\r\nAuthorization: Basic $pass \r\n";
fclose( $socket );
?> <BR><BR>Your account has been setup successfully please login <a href="<? echo "$mailurl";?>">here</a>
else {
echo "Username already in use please try another one.<br><a href=signup.php>Back</a>";

else { ?>
<form name="orderform" method="post" action="signup.php">
<table border="0" align="center" cellpadding="0" cellspacing="1">
  <td colspan="2">&nbsp;</td>
<td>Your full Name:</td>
<td colspan="2"><input name="name" type="text"></td>
<td>Prefered Email Address:</td>
<td colspan="2"><input name="newuser" type="text"></td>
<td colspan="2"><input name="password1" type="password"></td>
<td>Retype Password:</td>
<td colspan="2"><input name="password2" type="password"></td>
<td colspan="2">&nbsp;</td>
<td width="344"> <div align="center">
<input name="signupbtn" type="button" id="signupbtn" value="Signup" onClick="validateform()">
<td width="163">&nbsp;</td>
<script language="JavaScript1.2">

function validateform()
if(document.orderform.password1.value == document.orderform.password2.value)
alert("The passwords you typed do not appear to match");

function checkEmailAddress(field) {
var good;
var goodEmail = field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.go v)|(\.org)|(\..{2,2}))$)\b/gi);

if (goodEmail){
good = true
} else {
alert('Please enter a valid e-mail address.')
good = false
return good;

<? }


//before this will work you will need to make a database and then run signup.sql in phpmyadmin
//if you need any help email me at [email protected]
//also once you have made the table you Must add any email addresses that you already have
//to the data base manually otherwise this script can overwrite mailboxes

$host = "";//your url
$domain = "";//your domain without the www
$cpaneluser = "usernmae";//your cpanel username
$cpanelpass = "password";//your cpanel password
$cpaneltheme= "themename"; //this is the word after frontend/ and the next / when you login to cpanel
$dbhost="localhost"; //Usually localhost
$dbuser="databaseuser";//mysql username
$dbtable="users";//only change this if you have access to only one database
$dbpass="dbpass";//mysql password
$dbname="databasename";//The name of the database
$quota=100;//how much space in k you want to give the user
$mailurl="http://yourdomain/webmail";//The url for your web based mail program I use but you can use anything

//End of settings! you should not need to change anything below this

and the sql file that needs to be run after you create the database:

CREATE TABLE `users` (
`name` char(50) NOT NULL default '',
`username` char(50) NOT NULL default '',
`password` char(20) NOT NULL default '',
`id` int(11) NOT NULL auto_increment,
`email` char(50) NOT NULL default '',

A couple other items I would recommend when using this script. Place your config.php file outside of your public_html folder, and set the path in the $pathtoconfig. Since this file contains your cpanel username/password you don't want it placed inside the public_html folder where it can be read by the world....

Second, place the signup.php file in it's own directory and setup a web protect on that directory so it is password protected (unless of course you want anyone to be able to access it).


Damion McHenry