Chris
Chris

Reputation: 105

using cpanel XML API with PHP to create email

Im using XML API to cpanel to create email accounts from a PHP page.

Im having problem with creating a code that will see if the email account was setup successfully or not.

My PHP file looks like this:

<?
if(isset($_POST[token])) {
include("xmlapi.php");        //XMLAPI cpanel client class

$email = $_POST['email'];
$password = $_POST['password'];

$ip = "IP";            // should be server IP address or 127.0.0.1 if local server
$account = "USERNAME";        // cpanel user account name
$passwd ="PASSWORD";          // cpanel user password
$port =2083;                  // cpanel secure authentication port unsecure port# 2082
$email_domain ="DOMAIN";
$email_user ="$email";
$email_pass ="$password";
$email_quota = 500;             // 0 is no quota, or set a number in mb

$xmlapi = new xmlapi($ip);
$xmlapi->set_port($port);     //set port number.
$xmlapi->password_auth($account, $passwd);
$xmlapi->set_debug(1);        //output to error file  set to 1 to see error_log.

$call = array(domain=>$email_domain, email=>$email_user, password=>$email_pass, quota=>$email_quota);

$result = $xmlapi->api2_query($account, "Email", "addpop", $call );

?>

If the email account is createde successfully i get this response:

SimpleXMLElement Object ( [apiversion] => 2 [data] => SimpleXMLElement Object ( [reason] => SimpleXMLElement Object ( ) [result] => 1 ) [event] => SimpleXMLElement Object ( [result] => 1 ) [func] => addpop [module] => Email )

If not i get this:

SimpleXMLElement Object ( [apiversion] => 2 [data] => SimpleXMLElement Object ( [reason] => The password that you entered has a strength rating of “23”. You cannot use it because it is too weak... [result] => 0 ) [error] => The password that you entered has a strength rating of “23”. You cannot use it because it is too weak... [event] => SimpleXMLElement Object ( [result] => 1 ) [func] => addpop [module] => Email )

I tried using this code but didn't work:

if ($result->result->status) {
print "Creation of account worked successfully!";
}
else {
print "Creation Failed:" $result->result->statusmsg;
}

I would appreciate help and quick response, thanks.

Upvotes: 0

Views: 1528

Answers (1)

jagjeet
jagjeet

Reputation: 377

You can use response as below:-

-- to generate a new email address

   $api2args = array(
        'domain'          => $domain, 
        'email'           => $email_to_create, 
        'password'        => $password,
        'quota'           => '2048',                                
    );
    $result = $xmlapi->api2_query($username, 'Email', 'addpop',$api2args);

     if(isset($result->error) && $result->error!=""){               
          // error handling will be here                
      }
      else{
          // success message can be render here. 
      }

Upvotes: 0

Related Questions