cPanel Email Account Creator

This sections will contains code regarding different PHP & MySQL features and contains help about php/mysql issues.

cPanel Email Account Creator

Postby MT Shahzad on March 18th, 2008, 10:01 am

Create new email accounts on cPanel based hosting with just typing all the required data in URL. No need to login to cPanel. Provide cPanel login and password once, and just use this script each time you need new email account.

Will show input form if ran without parameters. Can be setup to avoid spam using CAPTCHA.

Usage:
* Update script with your cPanel login, password, skin (once)
* When running script to create new email account, provide following data:
o user - new account name
o pass - password for new email account
o domain - email domain (optional)
o quota - email quota in megabytes (optional)

Sample:
cpemail.php?user=newuser&pass=password&quota=50

cPanel Skin:
Try following steps if you do not know what your current cPanel theme is.
* Login to your cPanel account
* Look at the URL in your browser. It would look somewhat similar to http://www.domain.com:2082/frontend/x/index.html
* cPanel theme name is everything after the "/frontend/", and before the next slash "/". In above example cPanel theme is "x". It could be "x2", "rvblue", etc.

cpemail.php
Code: Select all
<?php

###############################################################
# You can pass following parameters in calling URL. They will
# override those specified below.
# user - new email user
# pass - password
# domain - email domain
# quota - email quota, Mb
# Example: cpemail.php?user=newuser&pass=password&quota=50
###############################################################

// Antispam image
// Show CAPTCHA - true, do not show - false
// In case you set this to true, you may want to update settings in the antispam.php
// Also when using this feature, fonts must exist on your system.
// By default antispam.php is setup to use arial.ttf
$antispam = true;

// cPanel info
$cpuser = 'userhere'; // cPanel username
$cppass = 'passwordhere'; // cPanel password
$cpdomain = 'mysite.com'; // cPanel domain or IP
$cpskin = 'x';  // cPanel skin. Mostly x or x2.

// Default email info for new email accounts
// These will only be used if not passed via URL
$epass = 'hispassword'; // email password
$edomain = 'mysite.com'; // email domain (usually same as cPanel domain above)
$equota = 20; // amount of space in megabytes

###############################################################
# END OF SETTINGS
###############################################################

function getVar($name, $def = '') {
  if (isset($_REQUEST[$name]))
    return $_REQUEST[$name];
  else
    return $def;
}

// check if overrides passed
$euser = getVar('user', '');
$epass = getVar('pass', $epass);
$edomain = getVar('domain', $edomain);
$equota = getVar('quota', $equota);

$msg = '';

if (!empty($euser))
while(true) {

  if ($antispam) {
    @session_start(); // start session if not started yet
    if ($_SESSION['AntiSpamImage'] != $_REQUEST['anti_spam_code']) {
      // set antispam string to something random, in order to avoid reusing it once again
      $_SESSION['AntiSpamImage'] = rand(1,9999999);

      // let user know incorrect code entered
      $msg = '<h2>Incorrect antispam code entered.</h2>';
      break;
    }
    else {
      // set antispam string to something random, in order to avoid reusing it once again
      $_SESSION['AntiSpamImage'] = rand(1,9999999);
    }
  }

  // Create email account
  $f = fopen ("http://$cpuser:$cppass@$cpdomain:2082/frontend/$cpskin/mail/doaddpop.html?email=$euser&domain=$edomain&password=$epass&quota=$equota", "r");
  if (!$f) {
    $msg = 'Cannot create email account. Possible reasons: "fopen" function allowed on your server, PHP is running in SAFE mode';
    break;
  }

  $msg = "<h2>Email account {$euser}@{$edomain} created.</h2>";

  // Check result
  while (!feof ($f)) {
    $line = fgets ($f, 1024);
    if (ereg ("already exists", $line, $out)) {
      $msg = "<h2>Email account {$euser}@{$edomain} already exists.</h2>";
      break;
    }
  }
  @fclose($f);

  break;

}

?>
<html>
<head><title>cPanel Email Account Creator</title></head>
<body>
<?php echo '<div style="color:red">'.$msg.'</div>'; ?>
<h1>cPanel Email Account Creator</h1>
<form name="frmEmail" method="post">
<table width="400" border="0">
<tr><td>Username:</td><td><input name="user" size="20" value="<?php echo htmlentities($euser); ?>" /></td></tr>
<tr><td>Password:</td><td><input name="pass" size="20" type="password" /></td></tr>
<?php if ($antispam) { ?>
<tr><td><img src="antispam.php" alt="CAPTCHA" /></td><td><input name="anti_spam_code" size="20" /></td></tr>
<?php } ?>
<tr><td colspan="2" align="center"><hr /><input name="submit" type="submit" value="Create Email Account" /></td></tr>
</table>
</form>
</body>
</html>


antispam.php
Code: Select all
<?php
// Font name to use. Make sure it is available on the server.
// You could upload it to the same folder with script if it cannot find font.
// By default it uses arial.ttf font.
$font = 'arial';

// list possible characters to include on the CAPTCHA
$charset = '0123456789';

// how many characters include in the CAPTCHA
$code_length = 6;

// antispam image height
$height = 20;

// antispam image width
$width = 80;

############################################################
#  END OF SETTINGS
############################################################

// this will start session if not started yet
@session_start();

$code = '';
for($i=0; $i < $code_length; $i++) {
  $code = $code . substr($charset, mt_rand(0, strlen($charset) - 1), 1);
}

$font_size = $height * 0.7;
$image = @imagecreate($width, $height);
$background_color = @imagecolorallocate($image, 255, 255, 255);
$noise_color = @imagecolorallocate($image, 20, 40, 100);

/* add image noise */
for($i=0; $i < ($width * $height) / 4; $i++) {
  @imageellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
}
/* render text */
$text_color = @imagecolorallocate($image, 20, 40, 100);
@imagettftext($image, $font_size, 0, 7,17,
              $text_color, $font , $code)
  or die('Cannot render TTF text.');

/* output image to the browser */
header('Content-Type: image/png');
@imagepng($image) or die('imagepng error!');
@imagedestroy($image);
$_SESSION['AntiSpamImage'] = $code;
exit();
?>
MT Shahzad
Web/Software Developer
http://mts.sw3solutions.com
User avatar
MT Shahzad
Site Admin
 
Posts: 300
Joined: February 29th, 2008, 8:11 am
Location: Muridke, Pakistan

Return to PHP / MySQL / XML

Who is online

Users browsing this forum: No registered users and 0 guests