• If you are still using CentOS 7.9, it's time to convert to Alma 8 with the free centos2alma tool by Plesk or Plesk Migrator. Please let us know your experiences or concerns in this thread:
    CentOS2Alma discussion

[API] Using API for creating mailboxes

F

Flyer@

Guest
Ok, I have an own server running Plesk 7.5 Reloaded.

To begin: I have some "big" websites that I run. And one of that big website we are going to upgrade building an webmail application to it. To use that webmail, people first use to sign up before they can use it.

Then, looking hard on this website, I found a solution: Shell scripts. But, a little problem, I don't have the user-rights to access them. And because I don't have the root password I can't change anything of that..

So I look further on the website and I called the people who are controlling my server. And I get an other option: Using API. I don't know API but it must work with API. So I have search on plesk and I have search google for finding some information about plesk & api. I didn't find anything. The only thing I found was an dead link linking to an doc here on plesk that maybe could help me.

So my guestion is, How can I write an script in PHP that will be able create mailboxes? If it is possible with API, where can I find the doc's of this API so I know what to do.
 
Please some??? I need this. It is very inportant.

I need to build an php-script than can create mail-accounts. And I can't use the the shell scripts only the API scripts. So please if someone has an answer for me.. I will thank you forever. Also if needed I will pay for it...
 
Plesk 7.5.3 adds support for mailname operations through API RPC. They didn't update SDK about it, so the only source of information available is schemas from psa-api-rpc-protocol package. Generally it should be the same as with other packet types (clients/domains).

The bad thing about this schemas - you'll need XMLSpy to read them easily..
 
Hi,

Did you manage to figure out how to create mailboxes using the API ? If so could you provide an example of the XML you used to create the accounts?

Thanks in advance,
 
I found a way to do it but you have to be fully updated to the newest build. i can't remember the excate version number but i'll post a small example using php.

PHP:
<?php

define("HOST", "pleskhostname");
define("PORT", 8443);
define("PATH", "enterprise/control/agent.php");
define("LOGIN", "admin");
define("PASSWD", "admin-password");
define("PROTO_VER", "1.3.1.0");

$proto = PROTO_VER;
$data =<<<EOF
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<packet version="$proto">
<mail>
        <create>
                <filter>
                        <domain_id>4</domain_id> // You need to know your domain id number you can find this in the domains table i beleave
                        <mailname>
                                <name>username</name>
                                <password>password</password>
                                <mailbox><enabled>1</enabled></mailbox>
                        </mailname>
                </filter>
        </create>
</mail>
</packet>
EOF;

function write_callback($ch, $data)
{
        echo $data;
        return strlen($data);
}

function sendCommand()
{
        $url = "https://" . HOST . ":" . PORT . "/" . PATH;

        $headers = array(
        "HTTP_AUTH_LOGIN: " . LOGIN,
        "HTTP_AUTH_PASSWD: " . PASSWD,
        "HTTP_PRETTY_PRINT: TRUE",
        "Content-Type: text/xml",
        );

        // Initalize the curl engine
        $ch = curl_init();

        // Set the curl options
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        // this line makes it work under https
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, &$headers);

        // Set the URL to be processed
        curl_setopt($ch, CURLOPT_URL, $url);

        // Set the callback functions
        curl_setopt($ch, CURLOPT_WRITEFUNCTION, write_callback);

        // Set the data to be send
        global $data;
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        // Debug, however...
        curl_setopt($ch, CURLOPT_VERBOSE, 1);

        $result = curl_exec($ch);

        if ($result == CURL_OK) {
                //print_r(curl_getinfo($ch));
        } else {
                echo "\n\n-------------------------\n" .
                "cURL error number:" .
                curl_errno($ch);
                echo "\n\ncURL error:" . curl_error($ch);
        }

        curl_close($ch);

        return;
}

sendCommand();

?>

I can't say this is the best way to do it or not. i'm not a super php guru but it did work for me :)

I did use xmlspy to find all the info for it. there are a few more options thinking about making a simple little configurable script to release with all the options displayed so others can use it. if there is any interest i may do it
 
Thanks, that worked. Is it possible to enable spam filtering using the API ?
 
i honestly can't remember i'll have to remember where i got all the info to make that
 
Sorry i'm not seeing a spam option of any kind but i could be missing it somewhere.

if you have updated to the newest version just grab a copy of the
xsd files and open them with xmlspy or something like it.

/usr/local/psa/admin/plib/api-rpc/protocol/

they are all in that folder if i remember right.
it took me a bit to get the hang of understanding it but its easy enough once you get it.
 
I took a look through that file and it doesn't apear to be possible to enable spam filtering via the API.
 
Have any of you used the API for updating mail accounts? For example changing the password of a mail account? I'm still trying to comprehend this XML API and any help would be apreciated.
 
I beleave you just change create to edit but i don't have a copy of teh api xml near me. to look for sure.
 
Tried changing it to update like it said in the file but that didn't work.
 
Try this xml (mailname with mailbox):

<packet version="1.3.4.1">
<mail>
<create>
<filter>
<domain_id>DOMAIN_ID_HERE</domain_id>
<mailname>
<name>some_mailname</name>
<mailbox>
<enabled>true</enabled>
</mailbox>
<password>SOMEPASSWORD</password>
</mailname>
</filter>
</create>
</mail>
</packet>

It's described in plesk_mailname.xsd and mailname_input.xsd shiped in psa-api-rpc-doc package...
 
Hello

This script works also for me. Have somebody an idea how we can integrate this in an application, in the way, that we have the result in a array and not in xml?

With regards
Peter


PHP:
<?php

define("HOST", "pleskhostname");
define("PORT", 8443);
define("PATH", "enterprise/control/agent.php");
define("LOGIN", "admin");
define("PASSWD", "admin-password");
define("PROTO_VER", "1.3.1.0");

$proto = PROTO_VER;
$data =<<<EOF
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<packet version="$proto">
<mail>
        <create>
                <filter>
                        <domain_id>4</domain_id> // You need to know your domain id number you can find this in the domains table i beleave
                        <mailname>
                                <name>username</name>
                                <password>password</password>
                                <mailbox><enabled>1</enabled></mailbox>
                        </mailname>
                </filter>
        </create>
</mail>
</packet>
EOF;

function write_callback($ch, $data)
{
        echo $data;
        return strlen($data);
}

function sendCommand()
{
        $url = "https://" . HOST . ":" . PORT . "/" . PATH;

        $headers = array(
        "HTTP_AUTH_LOGIN: " . LOGIN,
        "HTTP_AUTH_PASSWD: " . PASSWD,
        "HTTP_PRETTY_PRINT: TRUE",
        "Content-Type: text/xml",
        );

        // Initalize the curl engine
        $ch = curl_init();

        // Set the curl options
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        // this line makes it work under https
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, &$headers);

        // Set the URL to be processed
        curl_setopt($ch, CURLOPT_URL, $url);

        // Set the callback functions
        curl_setopt($ch, CURLOPT_WRITEFUNCTION, write_callback);

        // Set the data to be send
        global $data;
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        // Debug, however...
        curl_setopt($ch, CURLOPT_VERBOSE, 1);

        $result = curl_exec($ch);

        if ($result == CURL_OK) {
                //print_r(curl_getinfo($ch));
        } else {
                echo "\n\n-------------------------\n" .
                "cURL error number:" .
                curl_errno($ch);
                echo "\n\ncURL error:" . curl_error($ch);
        }

        curl_close($ch);

        return;
}

sendCommand();

?>
 
Do you want to have result of mailname creation in array form, or do you have parameters of mailname creation as an array?

Anyway you'll need a xml parser, to make an array <-> xml conversion. You may search on pear.php.net or write it itself, using DOM or PHP5's SimpleXML extension (very simple actually).
 
Hello

We have found the following convert-function, but i don't know, how to implented in the script.

With regards
Peter

function XMLtoARRAY($rawxml) {
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $rawxml, $vals, $index);
xml_parser_free($xml_parser);

$params = array();
$level = array();
foreach ($vals as $xml_elem) {
if ($xml_elem['type'] == 'open') {
$level[$xml_elem['level']] = $xml_elem['tag'];
}
if ($xml_elem['type'] == 'complete') {
$start_level = 1;
$php_stmt = '$params';
while($start_level < $xml_elem['level']) {
$php_stmt .= '[$level['.$start_level.']]';
$start_level++;
}
$php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
eval($php_stmt);
}
}
return($params);
}
 
Originally posted by peter3030
Hello

We have found the following convert-function, but i don't know, how to implented in the script.

With regards
Peter

function XMLtoARRAY($rawxml) {
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $rawxml, $vals, $index);
xml_parser_free($xml_parser);

$params = array();
$level = array();
foreach ($vals as $xml_elem) {
if ($xml_elem['type'] == 'open') {
$level[$xml_elem['level']] = $xml_elem['tag'];
}
if ($xml_elem['type'] == 'complete') {
$start_level = 1;
$php_stmt = '$params';
while($start_level < $xml_elem['level']) {
$php_stmt .= '[$level['.$start_level.']]';
$start_level++;
}
$php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
eval($php_stmt);
}
}
return($params);
}
don't work :confused:
you have a script that works?
 
Back
Top