Need help with using API in PHP to add/remove email addresses to a Cpanel Account...

rhantson

Member
Mar 7, 2013
6
0
1
cPanel Access Level
DataCenter Provider
Hey guys,

I'm sure there has to be a few of you guys here who are going to say "That's easy..." but for someone who's not exactly a "coder/developer" - it may be for you!

I've got a service organization that I'm building a website for, and want to be able to allow members to sign-up and receive email addresses on the organizations domain - without ME having to go in and manually set them up inside CPanel.

Just something simple that would allow the members to 1) Check to see if the email address name is available, 2) set the email address and password, 3) click submit and viola - the email is setup. (and before anyone goes into the whole form security to keep bots off it, we've got covered already inside the CMS system that will have this form as part of its members area).

I'm more of an ADMIN than a coder... I can hack code to pieces and make it work, but when it comes creating new code from scratch forget it - I'm toast...

So if anyone has some examples, or can point me in the right direction - I'd love to hear it! And anyone willing to volunteer to help write some stuff, by all means! I BARTER... LOL

Robert J. Hantson
380 Mercury
 
Last edited:

KostonConsulting

Well-Known Member
Verifed Vendor
Jun 17, 2010
255
1
68
San Francisco, CA
cPanel Access Level
Root Administrator
If you're not too keen on coding form scratch, check out the PHP client class for the cPanel API:

https://github.com/CpanelInc/xmlapi-php

Here's an example of listing the emails:

Code:
<?php
include '../xmlapi.php';

$ip = ''; #IP of your cPanel/WHM server
$user = ''; #cpanel username
$pass = ''; #password of your cPanel user

$xmlapi = new xmlapi($ip);
$xmlapi->password_auth($user,$pass);
$xmlapi->set_port(2083);
$xmlapi->set_output("json");

$xmlapi->set_debug(1);
print $xmlapi->api2_query($user, "Email", "listpopswithdisk" );
?>
Then you'll want to use json_decode() to pull the JSON into a PHP object and then loop through the results and print them out for people to see. You can copy the format of the email screen in cPanel for a basis for your UI.

Your form should POST to a page that calls this to create a new email:

Code:
<?php
include '../xmlapi.php';

$ip = ''; #IP of your cPanel/WHM server
$user = ''; #cpanel username
$pass = ''; #password of your cPanel user

$xmlapi = new xmlapi($ip);
$xmlapi->password_auth($user,$pass);
$xmlapi->set_port(2083);
$xmlapi->set_output("json");

$email = $_POST["email"];
$pass = $_POST["pass"];

$params = array(
     'domain' => '', #domain to add the email to
     'email' => $email, #part of email before @ symbol
     'password' => $pass, #hopefully a strong, randomly generated password
     'quota' => 1000 #size in MB to allow the account to use
);

$xmlapi->set_debug(1);
print $xmlapi->api2_query($user, "Email", "addpop", $params );
?>