Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

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

    May 3, 2012
    Likes Received:
    Trophy Points:
    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

    Dec 17, 2009
    Likes Received:
    Trophy Points:
    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

    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

    // 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 ( ! 
        || ! 
        || ! 
        || ! 
    ) {
    // 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 ( 
        ) {
            foreach (
    $acct_emails['cpanelresult']['data'] as $an_email) {
    // store whatever detail you want for later use in your report;
                //  something like the following
    '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

    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 ;)
    array(2) {
      array(4) {
        string(4) "dave"
        string(16) ""
        string(2) "t4"
        string(19) ""
      array(4) {
        string(4) "dave"
        string(8) ""
        string(2) "t2"
        string(11) ""
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice