This is a simple class that can be used create e-mail addresses for domains hosted using a cPanel account.
It extends a script originally made available in the zubrag.com Web site.
The class accesses a cPanel Web server and sends a command to create an e-mail account with a given address, password and quota.
The class retrieves and process the cPanel server response to determine whether the account creation was successful or it could not be created when the account already exists.
cpmail_class.php
<?php
/*
This class is an extension of script made by www.zubrag.com. You can access the original link from here
http://www.zubrag.com/scripts/cpanel-create-email-account.php
Class Name: cpmail
Clas Title: cPanel Mail Accounts Creator
Purpose: Create cPanel email account without loggin in to cPanel.
Version: 1.0
Author: Md. Zakir Hossain (Raju)
URL: http://www.rajuru.xenexbd.com
Company: Xenex Web Solutions
URL: http://www.xenexbd.com
License: GPL
You can freely use, modify, distribute this script. But a credit line is appreciated.
Installation:
see example.php for details
*/
//definding main class
class cpmail{
//declare public variables
var $cpuser; // cPanel username
var $cppass; // cPanel password
var $cpdomain; // cPanel domain or IP
var $cpskin; // cPanel skin. Mostly x or x2.
//defining constructor
function cpmail($cpuser,$cppass,$cpdomain,$cpskin='x'){
$this->cpuser=$cpuser;
$this->cppass=$cppass;
$this->cpdomain=$cpdomain;
$this->cpskin=$cpskin;
// See following URL to know how to determine your cPanel skin
// http://www.zubrag.com/articles/determine-cpanel-skin.php
}
//now create email account, function takes three arguments
/*
$euser = email id
$epass = email password
$equota = mailbox allocated size
*/
function create($euser,$epass,$equota){
$path="http://".$this->cpuser.":".$this->cppass."@".$this->cpdomain.":2082/frontend/".$this->cpskin."/mail/doaddpop.html?quota=".$equota."&email=".$euser."&domain=".$this->cpdomain."&password=".$epass;
$f = fopen($path,"r");
if (!$f) {
return('Cannot create email account. Possible reasons: "fopen" function not allowed on your server, PHP is running in SAFE mode');
}
//check if the account exists
while (!feof ($f)) {
$line = fgets ($f, 1024);
if (ereg ("already exists!", $line, $out)) {
return('Such email account already exists.');
}
}
fclose($f);
//return success message
return "Email account created.";
}
}
?>
//Usage Example
<?php
/*
This is the example script for cpmail class
*/
//include class file
require_once('cpmail_class.php');
/*
instanceiate class & pass three arguments cpanelusername, cpanelpassword,yourdomainname,cpanelskin
See following URL to know how to determine your cPanel skin
http://www.zubrag.com/articles/determine-cpanel-skin.php
if you don't pass cpanelskin argument, default will be x
*/
$cpanel=new cpmail("rubdnet","secret2001","rubd.net","rvblue");
//call create function and you have to pass three arguments as follows:
//emailid, password, quota