Results 1 to 2 of 2

Thread: use XML API to write PHP script to get all mailbox sizes and email results

  1. #1
    Member
    Join Date
    May 2012
    Posts
    5
    cPanel/WHM Access Level

    Root Administrator

    Default use XML API to write PHP script to get all mailbox sizes and email results

    hi all
    i want to be able to get a list of all mailboxes from all accounts on my cpanel server, and then email them to myself each month. i did post this in feature requests and received an email back to say

    You could use the XML API to write a PHP script to gather and email this information for you. You can find some loosely related sample code (and documentation) at cPanel & WHM's SDK
    but feel welcome to post to the cPanel Developers forum if you need help implementing this.


    unfortunately writing php scripts is very far from my strong points so i was wondering if anyone was able to help me with this?

    thanks in advance

  2. #2
    Integration Developer cPanelDavidN's Avatar
    Join Date
    Dec 2009
    Location
    Houston, TX
    Posts
    570

    Default Re: use XML API to write PHP script to get all mailbox sizes and email resu

    Hi bremboy79,

    You don't have to use PHP. The Remote API (XML/JSON API) is an HTTP based API...PHP just happens to be the most common language that we see used.

    What you'd do is make a call as root (or a reseller account with 'list-accts' privilege) to get a list of all the cPanel accounts, listaccts.
    Next you'd make the cPanel API2 call Email::listpopswithdisk for each one of those accounts (which would produce a list of all email accounts for that cPanel user).

    When you make the cPanel API2 call you can do it as the user (if you have their password) or you can do it as someone who has authority over them (aka, their reseller or root). If you perform cPanel API call as someone other than the user, make sure to use a WHM port.

    The following code illustrates how one would make the calls if they were using the PHP client class that we provide; If you are familiar with another interpreted language (or shell scripting and cURL) then you should have little trouble porting the PHP client class and doing the illustrated logic below

    PHP Code:
    <?php
    include "xmlapi.php";

    $pass  'someSecret';
    $auser 'root';

    $server "dev.david.tld"//or IP address
    $port 2087;

    $remote_api = new xmlapi($server);

    // I'm using regular user/password authentication for simplicity
    // Alternatively, you can use a password hash which would be almost as easy and
    //  absolve you from having to put the root password in plain text in a script
     
    $remote_api->password_auth($auser$pass);
    $remote_api->set_port($port);
    $remote_api->set_output('json');

    // if you want to see debug in the console, uncomment the following line
    // $remote_api->set_debug(1);

    // get list of accounts
    $json_list $remote_api->xmlapi_query('listaccts', array( 'api.version'=> 1));
    $list json_decode($json_listtrue);

    if ( ! 
    is_array($list)
        || ! 
    array_key_exists('data'$list)
        || ! 
    is_array($list['data'])
        || ! 
    array_key_exists('acct'$list['data'])
    ) {
        
    // do your own error handling here
        
    die("Invalid response!");
    }

    // this will hold all the data we intend to get
    $email_list = array();

    // iterate through the cpanel accounts and fetch a list of their email accounts
    foreach ($list['data']['acct'] as $acct) {
        
    $username $acct['user'];
        
    $json_emails $remote_api->api2_query($username'Email''listpopswithdisk', array());
        
    $acct_emails json_decode($json_emailstrue);
        
        if ( 
    is_array($acct_emails)
            && 
    array_key_exists('cpanelresult'$acct_emails)
            && 
    is_array($acct_emails['cpanelresult'])
            && 
    array_key_exists('data'$acct_emails['cpanelresult'])
            && 
    is_array($acct_emails['cpanelresult']['data'])
        ) {
            foreach (
    $acct_emails['cpanelresult']['data'] as $an_email) {
                
    // store whatever detail you want for later use in your report;
                //  something like the following
                
    array_push(
                    
    $email_list,
                    array(
                        
    'cpanel_account' => $username,
                        
    'domain'         => $an_email['domain'],
                        
    'email'          => $an_email['user'],
                        
    'full_email'     => $an_email['email'], 
                    )
                );
            }
        }
    }

    # do something with the data...I'll just dump it for example purposes
    var_dump($email_list);

    ?>
    On my test server it shows me that I host only two email accounts on the entire server, each on a different domain, but ultimately owned by the same cPanel account...a real server would be quite different and have lots of data
    Code:
    array(2) {
      [0]=>
      array(4) {
        ["cpanel_account"]=>
        string(4) "dave"
        ["domain"]=>
        string(16) "aioeiri21123.net"
        ["email"]=>
        string(2) "t4"
        ["full_email"]=>
        string(19) "t4@aioeiri21123.net"
      }
      [1]=>
      array(4) {
        ["cpanel_account"]=>
        string(4) "dave"
        ["domain"]=>
        string(8) "dave.net"
        ["email"]=>
        string(2) "t2"
        ["full_email"]=>
        string(11) "t2@dave.net"
      }
    Regards,
    -DavidN
    David Neimeyer
    Integration Developer

    sdk.cpanel.net
    APIs: XML-API API1 & API2
    Check Out: Developer Downloads Integration Blog
    Need Support? Support Ticket Developer Forum Feature Requests

Similar Threads

  1. Cannot Create New Email Accounts with XML API Script
    By christsealed in forum E-mail Discussions
    Replies: 1
    Last Post: 04-30-2011, 06:48 PM
  2. xml-api script works only sometimes?
    By progamerinc in forum cPanel Developers
    Replies: 5
    Last Post: 04-20-2010, 03:52 PM
  3. XML-API and controling WHM using remote Perl script - Help!
    By jonathanwww in forum cPanel Developers
    Replies: 1
    Last Post: 06-30-2008, 10:09 AM
  4. Replies: 1
    Last Post: 06-20-2003, 01:57 PM
  5. How could I write a PHP script to create email accounts?
    By N9ne in forum cPanel & WHM Discussions
    Replies: 4
    Last Post: 04-19-2003, 08:30 PM