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.

Need help with using API in PHP to add/remove email addresses to a Cpanel Account...

Discussion in 'cPanel Developers' started by rhantson, Jan 29, 2014.

  1. rhantson

    rhantson Member

    Joined:
    Mar 7, 2013
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    DataCenter Provider
    Hey guys,

    I'm sure there has to be a few of you guys here who are going to say "That's easy..." but for someone who's not exactly a "coder/developer" - it may be for you!

    I've got a service organization that I'm building a website for, and want to be able to allow members to sign-up and receive email addresses on the organizations domain - without ME having to go in and manually set them up inside CPanel.

    Just something simple that would allow the members to 1) Check to see if the email address name is available, 2) set the email address and password, 3) click submit and viola - the email is setup. (and before anyone goes into the whole form security to keep bots off it, we've got covered already inside the CMS system that will have this form as part of its members area).

    I'm more of an ADMIN than a coder... I can hack code to pieces and make it work, but when it comes creating new code from scratch forget it - I'm toast...

    So if anyone has some examples, or can point me in the right direction - I'd love to hear it! And anyone willing to volunteer to help write some stuff, by all means! I BARTER... LOL

    Robert J. Hantson
    380 Mercury
     
    #1 rhantson, Jan 29, 2014
    Last edited: Jan 29, 2014
  2. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,854
    Likes Received:
    676
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    Hello :)

    Our documentation website is a good place to start:

    cPanel & WHM's SDK

    You will find information on using our API for functions like creating email accounts. Note that if you need to have the script developed for you, the following is a list of development services on our application catalog:

    cPanel Application Catalog - Development Services

    Thank you.
     
  3. vanessa

    vanessa Well-Known Member
    PartnerNOC

    Joined:
    Sep 26, 2006
    Messages:
    817
    Likes Received:
    22
    Trophy Points:
    18
    Location:
    Virginia Beach, VA
    cPanel Access Level:
    DataCenter Provider
  4. KostonConsulting

    KostonConsulting Well-Known Member

    Joined:
    Jun 17, 2010
    Messages:
    255
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    San Francisco, CA
    cPanel Access Level:
    Root Administrator
    If you're not too keen on coding form scratch, check out the PHP client class for the cPanel API:

    https://github.com/CpanelInc/xmlapi-php

    Here's an example of listing the emails:

    Code:
    <?php
    include '../xmlapi.php';
    
    $ip = ''; #IP of your cPanel/WHM server
    $user = ''; #cpanel username
    $pass = ''; #password of your cPanel user
    
    $xmlapi = new xmlapi($ip);
    $xmlapi->password_auth($user,$pass);
    $xmlapi->set_port(2083);
    $xmlapi->set_output("json");
    
    $xmlapi->set_debug(1);
    print $xmlapi->api2_query($user, "Email", "listpopswithdisk" );
    ?>
    
    Then you'll want to use json_decode() to pull the JSON into a PHP object and then loop through the results and print them out for people to see. You can copy the format of the email screen in cPanel for a basis for your UI.

    Your form should POST to a page that calls this to create a new email:

    Code:
    <?php
    include '../xmlapi.php';
    
    $ip = ''; #IP of your cPanel/WHM server
    $user = ''; #cpanel username
    $pass = ''; #password of your cPanel user
    
    $xmlapi = new xmlapi($ip);
    $xmlapi->password_auth($user,$pass);
    $xmlapi->set_port(2083);
    $xmlapi->set_output("json");
    
    $email = $_POST["email"];
    $pass = $_POST["pass"];
    
    $params = array(
         'domain' => '', #domain to add the email to
         'email' => $email, #part of email before @ symbol
         'password' => $pass, #hopefully a strong, randomly generated password
         'quota' => 1000 #size in MB to allow the account to use
    );
    
    $xmlapi->set_debug(1);
    print $xmlapi->api2_query($user, "Email", "addpop", $params );
    ?>
    
     
Loading...

Share This Page