tags
-
Recent Posts
- Internet blackout day: The day the internet goes dark
- Apple will release new iPhone in September
- Backup your files online and sync to multiple device
- Test Driven Development with QUnit
- An excellent introduction to SCRUM
- Creating multiple email account in cPanel with PHP and XMLAPI
- jQuery for iPhone and iPad
- Using jQuery wysiwyg
- Top 5 jQuery based menu

A few days ago I was tasked to create email accounts for our clients via cPanel, creating a single account is easy. cPanel comes with an excellent account management feature, but I am going to create 400+ email address.
As the old saying goes, RTFM! I dug through its massive documentation http://docs.cpanel.net and after googling a few keywords it pointed me to its XML and JSON API’s.
“XML API allows you to issue commands to cPanel and WHM using the XML language, while our JSON API allows you to submit requests to the system and receive a JSON response. By including these commands in your custom scripts, you can perform functions remotely, without having to access the cPanel or WHM user interface.”
Now I have the API I know exactly what to do:
1. Create a form to upload the email accounts.
2. Make an API call to add the account
Easy huh? Well, soft of.
Here’s the basic form upload:
There are a lot of ways to read the uploaded CSV file, the easiest would be the http://php.net/manual/en/function.fgetcsv.php function. After parsing the csv file, assigned the fields to variable then we start using the API.
<?php $ip = "your_ip_address"; // should be WHM ip address $account = "your_account"; // cpanel user account name $passwd ="your_password"; // cpanel user password $port =2082; // cpanel secure authentication port unsecure port# 2082 $email_domain = 'http://www.homebasedcoder.com:2082'; // email domain (usually same as cPanel domain) $email_quota = 10; // default amount of space in megabytes $xmlapi = new xmlapi($ip); $xmlapi->set_port($port); //set port number. cpanel client class allow you to access WHM as well using WHM port. $xmlapi->password_auth($account, $passwd); // authorization with password. not as secure as hash. $msg = ''; foreach($data as $row){ $call = array(domain=>$email_domain, email=>$row['email_user'], password=>$row['email_pass'], quota=>$email_quota); $xmlapi->set_debug(1); //output to error file set to 1 to see error_log. $result = $xmlapi->api2_query($account, "Email", "addpop", $call ); // making call to cpanel api } ?>