The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Auto create email with PHP

Discussion in 'cPanel Developers' started by bwor, Feb 5, 2010.

  1. bwor

    bwor Member

    Joined:
    Jun 15, 2008
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    1
    I have cpanel version 11 and i try to auto create email from PHP using this code

    <?php
    $cpuser = "cpanel_user_name";
    $cppass = "cpanel_password";
    $euser = "email_user_name";
    $epass = "email_password";
    $equota = "email_quota";
    $domain = "domian.com";
    $url = "http://".$cpuser.":".$cppass."@".$domain."2082/frontend/x3/mail/doaddpop.html?email=".$euser."&domain=".$domain."&password=".$epass."&quota=".$equota;
    if($cpanel = fopen($url, "r"))
    {
    echo "Success email creation.";
    fclose($cpanel);
    }
    else
    {
    echo "Failed email creation.";
    }
    ?>

    When I run this code I get the result "Success email creation." but when I login to cpanel I not found the new email in the list.

    What the wrong ?
     
  2. nelwa

    nelwa Registered

    Joined:
    Jul 21, 2008
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    I have cPanel version 11.24.5 and I can confirm that I have almost exactly the same problem. I run my script, which returns this array:

    Code:
    array(3) {
      ["apiversion"]=>
      string(1) "2"
      ["func"]=>
      string(6) "addpop"
      ["module"]=>
      string(5) "Email"
    but when I login on cpanel, the email address isn't shown...this is driving me mad...

    I run the instruction with an api2 query like this:
    PHP:
    $arr $xmlapi->api2_query($account"Email""addpop", array(domain=>$res['domain'], email=>$email1password=>$password1quota=>0) );
    var_dump($arr);
    I know I'm authenticating correctly, because I can list bandwidth usage, etc. without any problems...

    This is an urgent problem and any help would be greatly appreciated.
     
  3. rjf1505

    rjf1505 Member

    Joined:
    Apr 16, 2005
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    1
    Same here

    Hi,

    I also use a script to create email and other stuff.
    All works fine exept the email part.

    Anyone got news from Cpanel ?

    Thanks
    Robert-Jan
     
  4. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    addpop via XML-API

    Hi guys,

    Using the XMLAPI.php class, you should be about to add pop emails to an account like so:
    Code:
    $username = 'dave'; //cpanel username associated with our domain
    $newpopdata = array(
     'domain'=>'dave.com',
     'email'=>'mr_dave',
     'password'=>'my_s3cr3t_pop!passwd',
     'quota'=>0,
    );
    $result = $xmlapi->api2_query($username, "Email", "addpop", $newpopdata); 
    
    This is the same as nelwa's code. The main difference is I'm quoting my array keys; not fatal, but depending it may affect your code.

    I know for certain that his works on 11.25.0. However, I will try on an 11.24.5 server later and will post my findings.

    Regards,
    -Dave
     
  5. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    api call to add pop

    Hi guys,

    Unfortunately, the documentation isn't crystal clear on api2 calls via URL. Short answer: not all api2 calls are supported in 11.24.5. All api1 calls are. Use an api1 like the following example for 11.24.5:
    Code:
    $account = "dave";
    $email_user = "mr_dave";
    $email_password = "my_s3cr3t_pop!passwd";
    $email_domain = "dave.com";
    $email_quota = '0';
    
    $xmlapi = new xmlapi($ip);
    $xmlapi->password_auth("root",$root_pass);
    $xmlapi->set_output('xml');
    
    $result = $xmlapi->api1_query($account, "Email", "addpop", array($email_user, $email_password, $email_quota, $email_domain) );
    
    The actual url query string would look like:
    Code:
    https://10.1.5.162:2087/xml-api/cpanel?user=dave&cpanel_xmlapi_module=Email&cpanel_xmlapi_func=addpop&cpanel_xmlapi_apiversion=1&arg-0=mr_dave&arg-1=my_s3cr3t_pop!passwd&arg-2=0&arg-3=dave.com
    
     
  6. MattDees

    MattDees cPanel Product Owner
    Staff Member

    Joined:
    Apr 29, 2005
    Messages:
    417
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    More clarification:

    The API2 documentation was targetted @ 11.25.0. At the time we started developing it we decided that it was less of a time sync to target the release that would be coming out shortly.

    I apologize for the confusion.
     
  7. bwor

    bwor Member

    Joined:
    Jun 15, 2008
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    1
    I think the previous code using with WHM account only but in my status I have only cpanel account.
     
  8. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    You can authenticate with your user credentials and as long as you're only requesting action for your account, it should succeed.

    Code:
    $account = "dave";
    $account_pass = "d4v3sSecret!";
    
    $email_user = "mr_dave";
    $email_password = "my_s3cr3t_pop!passwd";
    $email_domain = "dave.com";
    $email_quota = '0';
    
    $xmlapi = new xmlapi($ip);
    $xmlapi->password_auth($account, $account_pass);  <-- USE USER ACCOUNT AUTH
    $xmlapi->set_output('xml');
    
    $result = $xmlapi->api1_query($account, "Email", "addpop", array($email_user, $email_password, $email_quota, $email_domain) );
    
     
  9. bwor

    bwor Member

    Joined:
    Jun 15, 2008
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    1
    I get an error when I execute this code
    (HTTP 500 Internal Server Error)
    The website cannot display the page
    HTTP 500
    Most likely causes:
    •The website is under maintenance.
    •The website has a programming error.

    What you can try:
    Refresh the page.

    Go back to the previous page.

    More information

    This error (HTTP 500 Internal Server Error) means that the website you are visiting had a server problem which prevented the webpage from displaying.

    For more information about HTTP errors, see Help.
     
  10. bwor

    bwor Member

    Joined:
    Jun 15, 2008
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    1
    cPanelDavidN what is $ip ? I what the value of $ip here ?
     
    #10 bwor, Jun 13, 2010
    Last edited: Jun 13, 2010
  11. bwor

    bwor Member

    Joined:
    Jun 15, 2008
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    1
    You can check the output of execution this code from this link
    2shared - download Error_01.jpg
     
  12. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    $ip is a variable containing the IP address of the server. You could also use you domain name. You must populate this with your own information.

    The xmlapi object must have the address (IP or domain) to know where to send the requests.

    If you don't have the right web address, then I'd expected to see the 500 errors you're getting. Try putting in your domain name and see what you get.

    Regards,
    -DavidN
     
  13. bwor

    bwor Member

    Joined:
    Jun 15, 2008
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    1
    I checked the error_log file and I get this result after execution your code
    PHP:
    [15-Jun-2010 15:52:59PHP Fatal error:  Class 'xmlapi' not found in /home/xxxx/public_html/yyyy/test/test.php on line 13
    What is that meaning ?
     
  14. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    in your test.php, you need to include/require the xmlapi client class.
    Code:
    include('xmlapi.php');
    
    you'll need to download the class from here http://sdk.cpanel.net/lib/xmlapi/php/cp_xmlapi_php_v1.0.5.tar.gz and extract it. Place "xmlapi.php" somewhere that's in you PHP include_path (or modify your include_path in test.php to include the directory where you placed "xmlapi.php").

    the download also includes examples of how to use the class.

    -DavidN
     
  15. bwor

    bwor Member

    Joined:
    Jun 15, 2008
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    1
    Thanks alot and it's working now and I think you can close this thread.
     
  16. toxic360

    toxic360 Registered

    Joined:
    Sep 4, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    i have this error ! please help

    Uncaught exception 'Exception' with message 'No host defined' in /home/xxxxx/public_html/test/xmlapi.php:205 Stack trace: #0 /home/xxxxx/public_html/test/test.php(11): xmlapi->__construct(NULL) #1 {main} thrown in /home/xxxxx/public_html/test/xmlapi.php on line 205
     
  17. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    It means that you're script is trying to instantiate the xmlapi object without passing the IP address (or FQDN).

    PHP:
    <?php
    include 'xmlapi.php';

    $ip 'my.website.com';

    // you MUST pass the IP or hostname of the cPanel box
    //  you're gonna send the requests to when you create
    //  the xmlapi object.
    $xmlapi_obj = new xmlapi($ip);

    // do stuff...
    -DavidN
     
  18. lbeachmike

    lbeachmike Well-Known Member

    Joined:
    Dec 27, 2001
    Messages:
    313
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Long Beach, NY
    cPanel Access Level:
    Root Administrator
    Hi there -

    I am still successfully using the older format in my scripts to provision email accounts, per the original poster's inquiry in this thread -

    Code:
    $url = "http://".$cpuser.":".$cppass."@".$domain."2082/frontend/x3/mail/doaddpop.html?email=".$euser."&domain=".$domain."&password=".$epass."&quota=".$equota;
    Although the new API format appears as if it may be marginally simpler to work with, is there any reason that I need to consider migrating my code at any point?

    Is the old format going to be supported indefinitely or is there any concern of its discontinuation?

    Basically, what would be the reason to use the new API format over the old?

    Thanks.

    Mike
     
  19. echogaurav

    echogaurav Registered

    Joined:
    Feb 26, 2013
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Please help me with my code. I have tried the steps that has been described but still no success. Please take a look at the code and see if anything i have missed! Follow is my code:-

    <?php
    include ("xmlapi.php"); // I have this file included and placed correctly

    $account = "cpUsername"; // not real but i have deployed the correct one
    $account_pass = "phpisechoing";

    $email_user = "gaurav";
    $email_password = "pwd4Gaurav";
    $email_domain = "gauravsingh.com"; // not real but i have deployed the correct one
    $email_quota = '0';

    $xmlapi = new xmlapi('192.168.182.195'); // not real but i have deployed the correct one
    $xmlapi->password_auth($account, $account_pass);
    $xmlapi->set_output('xml');

    echo $result = $xmlapi->api1_query($account, "Email", "addpop", array($email_user, $email_password, $email_quota, $email_domain) );

    ?>


    The output i got is:-

    Access denied 0 Access denied
     
    #19 echogaurav, Feb 26, 2013
    Last edited: Feb 26, 2013
Loading...

Share This Page