Script create email in PHP - ISP 3.2

Discussion in 'Developers' Forum' started by KilioZ, Oct 11, 2020.

  1. KilioZ

    KilioZ New Member

    I would like to know how to create an email address via ISPConfig 3.2 in PHP.

    I have seen some things, but it is still "website", not email address only.

    Thank you,
  2. till

    till Super Moderator Staff Member ISPConfig Developer

    You can find API usage examples for various functions incl. functions to add email accounts in the remote_client folder of the ISPConfig tar.gz file.
  3. KilioZ

    KilioZ New Member

    Hello, I have a problem:
    If I use: HTTPS:// I have:
    login_failedThe login failed. Username or password wrong. SOAP Error: The login failed. Username or password wrong.

    If I use: HTTP:// I have:
    SOAP Error: Bad Request

    My code is:

    $username 'ID_USER_C4';
    $password 'PASSWORD_USER_C4';
    $soap_location 'http://url_domain.ext:8080/remote/index.php';
    $soap_uri 'http://url_domain.ext:8080/remote/';

    $client = new SoapClient(null, array('location' => $soap_location,
    'uri'      => $soap_uri,
    'trace' => 1,
    'exceptions' => 1));

                                            try {
    $session_id $client->login($username$password)) {
    'Logged successfull. Session ID:'.$session_id.'<br />';

    //* Set the function parameters.
    $client_id "4"// 4 BECAUSE I USE C4
    $password0 $request->GetPost('dvtme__email_mail_password');
    $params = array(
    'server_id' => 1,
    'email' => $request->GetPost('dvtme__email_mail_username').'@my_domain.ext',
    'login' => $request->GetPost('dvtme__email_mail_username').'@my_domain.ext',
    'password' => $password0,
    'name' => $request->GetPost('dvtme__email_mail_username').' by '.$user['pseudo'],
    'uid' => 5000,
    'gid' => 5000,
    'maildir' => '/var/vmail/my_domain.ext/'.$request->GetPost('dvtme__email_mail_username').'',
    'quota' => 5242880,
    'cc' => '',
    'homedir' => '/var/vmail',
    'autoresponder' => 'n',
    'autoresponder_start_date' => '',
    'autoresponder_end_date' => '',
    'autoresponder_text' => 'hallo',
    'autoresponder_subject' => 'Out of office reply',
    'move_junk' => 'n',
    'custom_mailfilter' => 'spam',
    'postfix' => 'n',
    'access' => 'n',
    'disableimap' => 'n',
    'disablepop3' => 'n',
    'disabledeliver' => 'n',
    'disablesmtp' => 'n'
    $mailuser_id $client->mail_user_add($session_id$client_id$params);

    "New user: ".$mailuser_id."<br>";

    $client->logout($session_id)) {
    //echo 'Logged out.<br />';
                                            } catch (
    SoapFault $e) {
    'SOAP Error: '.$e->getMessage());
    What the problem @till ?
  4. Th0m

    Th0m ISPConfig Developer Staff Member ISPConfig Developer

    You have to use a username and password for a remote user, which you can add under System - Remote Users. You can't use client logins for the API.

    You also have to grant the correct rights to the needed functions for your remote user.

    The http error is probably caused because you are using HTTPS for the panel. So use HTTPS with a correct username and password.
    ahrasis and KilioZ like this.
  5. KilioZ

    KilioZ New Member

    Hello !
    I have a problem
    this is the code I have above just in case.
  6. Taleman

    Taleman Well-Known Member HowtoForge Supporter

    The error message says value for purge_trash_days is not Integer. I am too lazy to count fields in that picture, so you have to find yourself which value is for purge_trash_days.
    ahrasis likes this.
  7. TonyG

    TonyG Active Member

    I've spent the last couple days in both client and server-side code in this area, so at this moment my eyes are very familiar with those queries. It looks to me like the fields are really out of line with the data. I'd check to ensure there are no commas or quotes in the source data. Use print_r() to verify your $params. (Or var_dump where it's appropriate.)

    As noted in an Issue that I posted yesterday, there seems to be an error with API in what it does with the data received. I've been using JSON remoting, not SOAP, but it all goes to the same place, and code that leads to the final INSERT might be corrupting your query in a way similar to what's been happening with mine.

    If you look at my other threads recently, I've been saying the API doc doesn't tell us everything that we need to pass in these queries. The old doc hasn't been updated with new fields. A couple months ago I kinda took responsibility for updating those docs. I've been writing scripts for each function, both for myself and so that I can update all of those API docs. It's a slow process.

    For now, verify 100% exactly what you are passing into the $params. Then we can compare that to the INSERT query. If you are passing in the wrong data, of course that will be what is in the INSERT. But if you are passing in X and the INSERT is using Y, then there is a bug ... and that's what I was reporting. Again, you might have found a similar issue that might actually help in the resolution of that Issue.


    P.S. "successfull" only has one "L".
  8. TonyG

    TonyG Active Member

    This might help to get us on the same page. This is what I've been building for myself and publication.

    Start with a small bit of code common to all API requests:
    $base_url 'https://localhost:8080/remote/json.php?'// client is running on ISPConfig server, change as required
    $user "yyyyyyyyyyy";
    $psw "zzzzzzzzzzzzzzzzzzzzzzz";

    // all functions need a session ID, get it here in this common code
    $session_id '"session_id": "' login($user$psw) . '"';

    Common functions are in one file. Individual API calls are in their own files.
    require_once 'HTTP/Request2.php'// Not using SOAP, requests are JSON.

    function query_server($id$body)
    // echo "----------";
          // echo $body;
          // echo "----------";
    $json null;
    $errmsg "";

    $request = new HTTP_Request2();
    'follow_redirects' => TRUE,
    'ssl_verify_peer' => FALSE,
    'ssl_verify_host' => FALSE
    'Content-Type' => 'application/json'
    $request->setUrl($base_url $id);
          try {
    $response $request->send();
                if (
    $response->getStatus() == 200) {
    $text $response->getBody();
    $json json_decode($text$assoc true);
    //var_dump($json); // . "\n";
    if ($json == null)
    "Unexpected null result");
                } else {
    $errmsg 'Unexpected HTTP status: ' $response->getStatus() . ' ' .
          } catch (
    HTTP_Request2_Exception $e) {
    $errmsg 'Error: ' $e->getMessage();
          if (
    $errmsg != "") {
          if (
    $json["code"] == "remote_fault")

            "username": "' 
    $user '",
            "password": "' 
    $psw '"

    These are $param defaults for mail_user_add functions, maybe others. Change to set your defaults:
    function getMailUserParams()
    // any value with a # should be replaced in each call
    return  array(
    "_ispconfig_pw_crypted" => 0,
    "access" => "y",
    "autoresponder_end_date" => null,
    "autoresponder_start_date" => null,
    "autoresponder_subject" => "Out of office reply",
    "autoresponder_text" => "",
    "autoresponder" => "n",
    "backup_copies" => 5,
    "backup_interval" => "daily",
    "cc" => "",
    "custom_mailfilter" => "",
    "disabledeliver" => "n",
    "disabledoveadm" => "n",
    "disableimap" => "n",
    "disableindexer-worker" => "n",
    "disablelda" => "n",
    "disablelmtp" => "n",
    "disablepop3" => "n",
    "disablequota-status" => "n",
    "disablesieve-filter" => "n",
    "disablesieve" => "n",
    "disablesmtp" => "n",
    "email" => "#email",
    "forward_in_lda" => "n",
    "greylisting" => "n",
    "homedir" => "/var/vmail",
    "login" => "#login",
    "maildir" => "#maildir",
    "move_junk" => "y",
    "name" => "#name",
    "password" => "#password",
    "postfix" => "y",
    "purge_junk_days" => 0,
    "purge_trash_days" => 0,
    "quota" => 0,
    "sender_cc" => "",
    "server_id" => 1,
    "sys_userid" => 1,
    "sys_groupid" => 2,
    "uid" => 5000,
    "gid" => 5000,
    Now customize this for each new mail user.
    require_once '../common.php'// note file locations
    require_once './mail_user_add_template.php';

    2// int primary_key, should be retrieved, not hardcoded

    // Pass ALL params to the server, more than in the API docs which will get updates soon
    $params getMailUserParams();
    $domain ""// call custom functions for this data
    $username "foo";
    $params["email"] = $username "@" $domain;
    $params["login"] = $username "@" $domain;
    $params["password"] = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzz";
    $params["name"] = $username;
    $params["maildir"] = "/var/vmail/" $domain "/" $username;

    $data json_encode(array(
    "session_id" =>  login($user$psw),
    "client_id" => getClientID(),
    "params" => $params

    $json_result query_server(

    // use your own code here as required. the get/show code is not provided here
    $id $json_result["response"]
    //$user = get_mail_user($id)

    Unfortunately with good data, that currently leads to a failed INSERT, similar to the that reported by the OP, but for different reasons.

    This is the pattern that I'm following. If there's anything wrong with the basic style, please let me know before I publish a whole library of this crap. :) The code will be enhanced later with OOP, and I'll remove hard-coding, with population of fields abstracted out to user-defined functions. For now, it is what it is.
    ahrasis and Jesse Norell like this.
  9. Jesse Norell

    Jesse Norell ISPConfig Developer Staff Member ISPConfig Developer

    I'll try to get my start of those to you as well, and see if any of it is useful.
    Th0m likes this.
  10. TonyG

    TonyG Active Member

    All cool, TY. Since the tform fields are used to construct queries. What do you think about a routine that runs through the formDef['fields'] to create both current documentation and client routines like getMailUserParams() above? There's a real opportunity with this that wouldn't be possible if the queries weren't generated as they are.
  11. Jesse Norell

    Jesse Norell ISPConfig Developer Staff Member ISPConfig Developer

    ahrasis likes this.

Share This Page