PHP : how to get the used space by an email account ?

trucmuche

Well-Known Member
Nov 20, 2014
98
4
58
cPanel Access Level
Root Administrator
Hello,

In my WHM/Cpanel account, there is a user named "Margaret", which has an email account : "[email protected]".
Using PHP, how can I get the disk space occupied by that account ?

Thanks for your help !

T.
 
Last edited by a moderator:

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
14,241
2,217
363
cPanel Access Level
Root Administrator
Hey there! If you want to use PHP, it might be best to use an API call to get that disk usage. We have details on how to use that API call here:


Let me know if that helps!
 

trucmuche

Well-Known Member
Nov 20, 2014
98
4
58
cPanel Access Level
Root Administrator
Hello,
Thank you very much for your help. But I didn't managed to make it run as I want. The method described in LiveAPI PHP tab asks to create a file "Email_get_disk_usage.live.php" and put some code in it, then use an established CPANEL session to display needed informations. That works, ok, but for my application, I need to integrate the API call to my PHP code and get the email usage without being logged in CPanel. You can imagine that I want that :
PHP:
<?php //that file is in /home/someuser/myfolder/
// here, there is some code
    // Now, I would like to get used space information about 'use[email protected]' (it HAS to be variables)
    $user = "user";
    $domain = "domain.com";
   // Instantiate the CPANEL object.
   require_once "/usr/local/cpanel/php/cpanel.php";
    // establish connection to CPanel
    $cpanel = new CPANEL();
    // get email account informations
    $response = $cpanel->uapi(
        'Email',
       'get_disk_usage',
           array (
               'user' => $user,
               'domain' => $domain,
           )
       );

// Handle the response
if ($response['cpanelresult']['result']['status']) {
    $data = $response['cpanelresult']['result']['data'];
    // Do something with the $data
}
else {
    // Report errors and do things  
}
// Disconnect from cPanel - only do this once.
$cpanel->end();
?>
How can I do ?

Thanks,

T.
 
Last edited:

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
14,241
2,217
363
cPanel Access Level
Root Administrator
Thanks for that additional clarification. For your situation, you'll want to use API tokens:


Those will allow you to run an API call as the user without needing to login to the interface. Can you check that out and see if that will work for you?
 

trucmuche

Well-Known Member
Nov 20, 2014
98
4
58
cPanel Access Level
Root Administrator
Thanks ! I created token and checked "initial privileges" and I managed to auth & run the query "https://127.0.0.1:2087/json-api/listaccts?api.version=1" using CURL&PHP.
BUT I can't get working the following query :
It returns 404. I read and read again the doc (Return email account's disk usage · cPanel & WHM Developer Portal) but can't find what's wrong...

Here is my full code :
Code:
$user = CPANEL_TOKEN_NAME; // defined using define()
$token = CPANEL_TOKEN_KEY; // defined using define()

    //$query = "https://127.0.0.1:2087/json-api/listaccts?api.version=1"; // works
    $query = 'https://127.0.0.1:2087/execute/Email/[email protected]&domain=mydomain.com';

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);

    $header[0] = "Authorization: whm $user:$token";
    curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
    curl_setopt($curl, CURLOPT_URL, $query);

    $result = curl_exec($curl);

    $http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    if ($http_status != 200) {
        echo "[!] Error: " . $http_status . " returned\n";
    } else {
        $json = json_decode($result);
        echo "<pre>";
        print_r($json);
        echo "</pre>";
    }

    curl_close($curl);
 
Last edited:

trucmuche

Well-Known Member
Nov 20, 2014
98
4
58
cPanel Access Level
Root Administrator
Anybody to explain why the query
PHP:
$query = 'https://127.0.0.1:2087/execute/Email/[email protected]&domain=mydomain.com';
returns 404 but
PHP:
$query = "https://127.0.0.1:2087/json-api/listaccts?api.version=1";
works ?
How should I call "Email/get_disk_usage" ? I can't find the information in the developer help center...

Thanks for your help !

T.
 

trucmuche

Well-Known Member
Nov 20, 2014
98
4
58
cPanel Access Level
Root Administrator
That API call is actually at the user level, so you'd want to use port 2083 with a user session, instead of port 2087 as root. Can you tweak that and see if that gets things working?
GOT IT ! :) I created the token using WHM and logged as root with the first request. Now I created a token in the user account (CPanel) and use it in my code ; it works !
Many thanks !!!!