Create new email account using php script.

chetanmadaan

Active Member
Jun 18, 2010
41
7
58
hi -

we have a website and we need assign every new user to create have an email account.

I know for the mysql database... we can give a root used enough rights and that user will be able to create databases.

is that the same Case for email??

can you use a simple php script to create email account on our cpanel/WHM.

we have a dedicated server.

Thanks
 

rlesterphx

Member
Jul 22, 2010
21
0
51
I don't know your technical level, but here's a quick run-through.

First you're going to need to download the PHP XML API Class: https://github.com/CpanelInc/xmlapi-php

Upload it to your server and include it in the script you're building to create email accounts. Here is some sample code adapted from a script I made:
Code:
require_once('/home/serverusername/public_html/includes/xmlapi.php');
$ip = "123.123.123.123"; //your server's IP
$xmlapi = new xmlapi($ip);
$xmlapi->password_auth("serverusername","password"); //the server login info for the user you want to create the emails under
$xmlapi->set_output('json');

$params = array(domain=>'domain.com', email=>'newemailusername', password=>'newpassword', quota=>25); //quota is in MB
$addEmail = json_decode($xmlapi->api2_query("serverusername", "Email", "addpop", $params), true);

if($addEmail['cpanelresult']['data'][0]['result']){
	echo "success";
}
else {
	echo "Error creating email account:\n".$addEmail['cpanelresult']['data'][0]['reason'];
}
Additional notes:
-Make sure you have the Services_JSON extension installed. You can do this in WHM under Module Installers>PHP Extensions and search for JSON.
-If you get an error saying SSL support is not available follow the instructions on this post to enable SOAP with SSL: http://forums.cpanel.net/f5/soap-w-ssl-112613.html
-A whole lot more info and things you can do with the API now that you have it installed:
Calling API 2 Functions
WebHome < ApiDocs/Api2 < TWiki