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.

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

Discussion in 'cPanel Developers' started by bremboy79, Jul 20, 2012.

  1. bremboy79

    bremboy79 Member

    Joined:
    May 3, 2012
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    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. 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
    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:
    <?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
     
Loading...

Share This Page