• Please be aware: Kaspersky Anti-Virus has been deprecated
    With the upgrade to Plesk Obsidian 18.0.64, "Kaspersky Anti-Virus for Servers" will be automatically removed from the servers it is installed on. We recommend that you migrate to Sophos Anti-Virus for Servers.
  • The Horde webmail has been deprecated. Its complete removal is scheduled for April 2025. For details and recommended actions, see the Feature and Deprecation Plan.
  • We’re working on enhancing the Monitoring feature in Plesk, and we could really use your expertise! If you’re open to sharing your experiences with server and website monitoring or providing feedback, we’d love to have a one-hour online meeting with you.

Create email account with the API

G

GilesW

Guest
I have been trying to create an email account with the API now for 2 hours and it is killling me. This is the code that is relevant to the issue I have.

===

$emailxml = '<packet version="1.4.2.0">
<mail>
<create>
<filter>
<domain_id>127</domain_id>
<mailname>
<name>giles.wells</name>
<mailbox>
<enabled>true</enabled>
</mailbox>
<password>thisisapassword</password>
<password_type>crypt</password_type>
<permissions>
<cp_access>true</cp_access>
</permissions>
</mailname>
</filter>
</create>
</mail>
</packet>';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://www.inthesuntech.com:8443/enterprise/control/agent.php");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_HTTPHEADER,array("HTTP_AUTH_LOGIN"=> "{nottellingyou}",
"HTTP_AUTH_PASSWD:" =>"{stillnottellingyousodontask}",
"HTTP_PRETTY_PRINT:" => "TRUE",
"Content-Type:" => "text/xml")
);
curl_setopt($curl, CURLOPT_POSTFIELDS, "$emailxml");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$emailoutput = curl_exec($curl);
curl_close($curl);

print_r($emailoutput);
die();

===

I bet I am missing something somewhere. I have gotten pass the wrong login credentials and am currently getting this response with every attempt.

===

<?xml version="1.0"?>
<packet version="0.0.0.0">
<system>
<status>error</status>
<errcode>1003</errcode>
<errtext>Wrong request</errtext>
</system>
</packet>

===

What am I doing wrong?
 
I'm running 9.5.2 and xml of this request works for me. If you set the password type to plain, does it work then? If so you might be missing a crypt library.
 
Tried changing it to plain but still getting the same error.
 
Same error... *grumble grumble grumble*

Exact code as it stands now. (logins changed of course)

<?php
$emailxml = '<?xml version="1.0" encoding="UTF-8"?>
<packet version="1.4.2.0">
<mail>
<create>
<filter>
<domain_id>127</domain_id>
<mailname>
<name>george.testing</name>
<mailbox>
<enabled>true</enabled>
</mailbox>
<password>pass1234</password>
<password_type>plain</password_type>
<permissions>
<cp_access>true</cp_access>
</permissions>
</mailname>
</filter>
</create>
</mail>
</packet>';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://www.inthesuntech.com:8443/enterprise/control/agent.php");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_HTTPHEADER,array("HTTP_AUTH_LOGIN"=> "{removed}",
"HTTP_AUTH_PASSWD:" =>"{removed}",
"HTTP_PRETTY_PRINT:" => "TRUE",
"Content-Type:" => "text/xml")
);
curl_setopt($curl, CURLOPT_POSTFIELDS, "$emailxml");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$emailoutput = curl_exec($curl);
curl_close($curl);

echo $emailoutput;
 
I wrote a couple of API calls since i wrote a custom control panel thats similer for my client to use then plesk.

Here are a few of the functions.
and they work...

This is pieced together out of my custom api scripts so hope it works.

=======================

function get_psa_api( $url, $postData='', $Headers='') {
GLOBAL $DEBUG;
$javascript_loop = 0;
$timeout = 5;
$url = str_replace( "&amp;", "&", urldecode(trim($url)) );
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
IF ($Headers != '') {
curl_setopt($ch, CURLOPT_HTTPHEADER, $Headers);
}
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postData );
curl_setopt ( $ch, CURLOPT_POST, 1 );

$content = curl_exec( $ch );
$response = curl_getinfo( $ch );
curl_close ( $ch );
if ($response['http_code'] == 301 || $response['http_code'] == 302) {
ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");
if ( $headers = get_headers($response['url']) ) {
foreach( $headers as $value ) {
if ( substr( strtolower($value), 0, 9 ) == "location:" )
return array( trim( substr( $value, 9, strlen($value) ) ), $response, $cookie );
//return get_url( trim( substr( $value, 9, strlen($value) ) ) );
}
}
}

if ( ( preg_match("/>[[:space:]]+window\.location\.replace\('(.*)'\)/i", $content, $value) || preg_match("/>[[:space:]]+window\.location\=\"(.*)\"/i", $content, $value) ) &&
$javascript_loop < 5
)
{
// return get_url( $value[1], $javascript_loop+1 );
return array( $value[1], $response, $cookie );
} else {
return array( $content, $response, $cookie );
}
}

function PSA_API_REQUEST($Request) {
GLOBAL $_PSA;
$RequestHeaders = array(
'HTTP_AUTH_LOGIN: ' . $_PSA["Username"],
'HTTP_AUTH_PASSWD: ' . $_PSA["Password"],
'Content-Type: text/xml'
);
RETURN get_psa_api( $_PSA["URL"], $Request, $RequestHeaders);
}


$_PSA["Username"] = 'username';
$_PSA["Password"] = 'whatever';
$_PSA["URL"] = 'https://www.inthesuntech.com:8443/enterprise/control/agent.php';
$RequestType = "Create_Email";

$DOM_ID='127';
$EMAIL_USER='gary';
$EMAIL_ALIAS='gman';

$REQUESTS["Create_Email"] = <<<EOP
<packet version="1.6.3.0">
<mail>
<create>
<filter>
<site-id>$DOM_ID</site-id>
<mailname>
<name>$EMAIL_USER</name>
<mailbox>
<enabled>true</enabled>
<quota>10485760</quota>
</mailbox>
<alias>$EMAIL_ALIAS</alias>
<antivir>true</antivir>
</mailname>
</filter>
</create>
</mail>
</packet>
EOP;

PRINT_R(PSA_API_REQUEST($REQUESTS[$RequestType]));

===================================

I originally had lots of problems too and it all came down to CURL settings.
 
Here are the curl related method calls that I'm using sucessfully. I apologize that
this is just a snippet from a large php class.

/**
* Prepares CURL to perform Plesk API request
* @return resource
*/
public function curlInit() {

$host = $this->host;
$login = $this->login;
$password = $this->password;

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://{$host}:8443/enterprise/control/agent.php");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("HTTP_AUTH_LOGIN: {$login}",
"HTTP_AUTH_PASSWD: {$password}",
"HTTP_PRETTY_PRINT: TRUE",
"Content-Type: text/xml")
);
return $curl;
}


/**
* Performs a Plesk API request, returns raw API response text
*
* @return string
* @throws ApiRequestException
*/
public function sendRequest($curl, $packet) {

curl_setopt($curl, CURLOPT_POSTFIELDS, $packet);
$result = curl_exec($curl);
if (curl_errno($curl)) {
$errmsg = curl_error($curl);
$errcode = curl_errno($curl);
curl_close($curl);
throw new ApiRequestException($errmsg, $errcode);
}
curl_close($curl);
return $result;
}


Usage:

$curl = $this->curlInit();
try {
$response = $this->sendRequest($curl, $xml);
} catch (ApiRequestException $e) {
...
 
Success!!!

I originally had lots of problems too and it all came down to CURL settings.

This is what got it for me... I was looking at my headers that I was setting and I tried changing them however I could. This is what worked for me in the end.

curl_setopt($curl, CURLOPT_HTTPHEADER,
array("HTTP_AUTH_LOGIN:username",
"HTTP_AUTH_PASSWD:password",
"HTTP_PRETTY_PRINT:TRUE",
"Content-Type:text/xml")
);

I was originally posting it as a true associative array. I guess that was not too nice in Plesk world.
Thank you all for the help. I had honestly started giving up hope a few days ago when I had no responses.

So in conclusion:
I had issues first with the logins. That worked out but then I started getting 1003 error as originally reported. I guess the content type was not working as it wanted and that was causing the issue that resulted with the 1003. Now all I need to do is do a couple tweaks to enable anti-virus and spam filtering on creation and then I will be all set. Super happy to drop a 15 minute process of provisioning a new employee down to 90 seconds.
 
API warning... sometimes API doc says you can use TRUE and FALSE but you get error....

check the error log.

command prompt in ssh:
tail -f var/log/sw-cp-server/error_log

and then run test again and go look at log.

For me i used "false" just like the document called and then error showed up that said
The value 'false' is not an element of the set {'off', 'inout', 'in', 'out'}.

So in short... while i was trying to say OFF becuase doc said i could use "false" i had to change it to "off" to get it to work.

I bet if you look in this log as well you will find error report from your errors earlier... will help quicken troubleshooting.

Good Luck
 
Back
Top