Unable to create email account via xml api

techahamed

Registered
Oct 26, 2013
2
0
1
cPanel Access Level
Root Administrator
Hi friends. I would like to introduce myself as a root administration, application developer in an organization mbrace technologies in india. Let me tell you the problem iam facing. I'm trying to create email accounts via xml api using php script.I have corrected the all the variables in xml api, and query page but iam getting this error

Fatal error: Uncaught exception 'Exception' with message 'No host defined' in /home/mbrace/public_html/appln_enterprise/healthcare/test_mail.php:237 Stack trace: #0 /home/mbrace/public_html/appln_enterprise/healthcare/test_mail1.php(5): xmlapi->__construct(NULL) #1 {main} thrown in /home/mbrace/public_html/appln_enterprise/healthcare/test_mail.php on line 237

Does anybody have a solution.

Techahamed
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,913
2,201
363
Hello :)

Please check to ensure you have working resolvers configured for your system in the /etc/resolv.conf file. You can try using public resolvers (8.8.8.8 and 8.8.4.4) to see if the error message continues.

Thank you.
 

KostonConsulting

Well-Known Member
Verifed Vendor
Jun 17, 2010
255
1
68
San Francisco, CA
cPanel Access Level
Root Administrator
Welcome to the forums. In the future, it would be quite helpful if you can post a code snippet along with the error messages. Since I don't know what's on line 237 of test_mail.php, I'll give you my best guess:

Sounds like you are not passing the ip address of your cPanel/WHM server when creating an xmlapi object. I'd imagine your code looks like:

Code:
$xmlapi = new xmlapi();
where it should specify the IP, like so:

Code:
$xmlapi = new xmlapi($ip);
 

techahamed

Registered
Oct 26, 2013
2
0
1
cPanel Access Level
Root Administrator
Thanks for the reply,koston and cpanel michael.That problem is solved by correcting host resolvers. Now i'am now getting a new error Fatal error: Uncaught exception 'Exception' with message 'curl_exec threw error "Unknown SSL protocol error in connection to ip number.

Here is my xml api code for helping you find the problem

require_once('api.php');
$ip = 'myip';
$xmlapi = new xmlapi($ip);
$xmlapi->password_auth("myuser","mypass"); //the server login info for the user you want to create the emails under
$xmlapi->set_output('json');

$params = array(domain=>'mbracecloud.com', email=>'hello', password=>'hello0513', quota=>250); //quota is in MB
$addEmail = json_decode($xmlapi->api1_query("root", "Email", "addpop", $params), true);

if($addEmail['cpanelresult']['data'][0]['result']){
echo "success";
}
else {
echo "Error creating email account:\n".$addEmail['cpanelresult']['data'][0]['reason'];
}

Techahamed
 

KostonConsulting

Well-Known Member
Verifed Vendor
Jun 17, 2010
255
1
68
San Francisco, CA
cPanel Access Level
Root Administrator
I've noticed that it seems (my assumption) like you are connecting as a cPanel user for password_auth() and then connecting as root for the query. That won't work. You can connect as root for password_auth() and then have $user set to a cPanel user for api1_query() but not the other way around.

If you want to connect as the user for password_auth(), you should specify the cPanel port in your xmlapi object like so:

Code:
$xmlapi->set_port('2083');
and you need to specify the domain owner for any api1/api2 queries, regardless of which user you are using to authenticate:

Code:
$xmlapi->api1_query($domain_owner, "Email", "addpop", $params)

As far as the SSL error goes, this could be a number of things. The XMLAPI library is using curl to connect to your cPanel/WHM server and the response it is getting back is not what curl is expecting. You should test to see if you can connect via command line curl to your cPanel/WHM server (this should return some JSON with access denied):

Code:
curl -k "https://hostname:2087/json-api/cpanel?user=username&cpanel_jsonapi_module=Email&cpanel_jsonapi_func=addpop&cpanel_jsonapi_version=2&domain=mbracecloud.com&email=hello&password=hello0513&quota=250"
If you're not getting JSON back, then curl is having issues connecting to cPanel/WHM and you may have an invalid SSL cert, or ports 2083/2087 may be blocked, or you're connecting to a machine that isn't broadcasting SSL, or some other issue is preventing a SSL connection.

Try connecting with openssl to see if you get any errors:

Code:
openssl s_client -connect hostname:2087
Also try curl's verbose mode on the headers:

Code:
curl --verbose -i -k "https://hostname:2087/json-api/cpanel?user=username&cpanel_jsonapi_module=Email&cpanel_jsonapi_func=addpop&cpanel_jsonapi_version=2&domain=mbracecloud.com&email=hello&password=hello0513&quota=250"
It should also be noted that in API1, you shouldn't pass the argument names in the params array. Instead, you just need to present the arguments as a properly ordered array of values like so:

Code:
$params = array('hello','hello05013','250','mbracecloud.com'); //(email,password,quota,domain)
Since it's easier to visually debug associative arrays (and because it's always best to use the newest API), you should use API2 rather than API1:

Code:
$xmlapi->api2_query($domain_owner, "Email", "addpop", $params)