Robin Rodricks
Robin Rodricks

Reputation: 114126

How to access list of email accounts with cPanel API?

cPanel is a web server management application that we have installed on our servers. It has a XML/JSON-based PHP API that we want to use to access data such as a list of all email accounts. This system is huge, I could'nt find an answer to this anywhere. Does anyone know how to list email accounts with the cPanel API?

Edit: The function I need is listpopswithdisk (docs here) which lists email accounts under a given domain, but no idea how to call this function.

Upvotes: 5

Views: 5944

Answers (2)

blamb
blamb

Reputation: 4299

cpanel UAPI listpops should do the trick

UAPI Functions - Email::list_pops

Since you tagged PHP, heres the PHP example

$cpanel = new CPANEL(); // Connect to cPanel - only do this once.

// List all email addresses that contain "user".
$emails = $cpanel->uapi(
    'Email', 'list_pops',
    array(
        'regex'      => 'user',
        )
);

reference https://documentation.cpanel.net/display/SDK/UAPI+Functions+-+Email%3A%3Alist_pops

Also look into Afterlogic's WebMail Lite API, it has alot of bang-up functionality for that, including both for PHP and JS, and a REST API.

THe REST API states

GET /account/list
Returns list of users.

Required parameters:

* string token - token

Optional parameters:

* int page - page number of the list. Default value: 1

* int usersPerPage - number of users per page. Default value: 100

* string orderBy - sorting field. Accepted values: email / name / last login

* string searchDesc - search string used for looking up specific account

* string domain - domain

Return: array

Sample request:

http://yourdomain/rest.php/account/list?token=yourToken

curl -X GET -d "token=yourToken" http://yourdomain/rest.php/account/list

Sample response:

"result":
[
  {
     "Id": 32,
     "Email": "[email protected]",
     "FriendlyName": "Name"
  },
  {
     "Id": 33,
     "Email": "[email protected]",
     "FriendlyName": "OtherName"
  }
]

http://www.afterlogic.org/docs/webmail-lite/integration-and-development/rest-api#get-/account/list

Upvotes: 1

Sajjad Hossain
Sajjad Hossain

Reputation: 193

I think anyone can get his/her websites cPanel email accounts by using this normal php script. I am using this script for my personal work and it's working fine.

<?php
$domain = 'domain';
$username = 'username';
$quota = 'default_quota';
$mails = "/home/".$username."/.cpanel/email_accounts.yaml";
$mail_info = file_get_contents($mails);
$get_domain_mails = explode('account_count:',$mail_info);
foreach ($get_domain_mails as $accounts_email)
{
$acc = explode(' ',$accounts_email);
$m = $acc[1];
$clean = str_replace($m,"",$accounts_email);
$get_data = str_replace("accounts:","",$clean);

$exp_ag = explode("'",$get_data);
foreach ($exp_ag as $brk)
{
$ex = explode("diskquota",$brk);
foreach ($ex as $na)
{
$aex = explode('disk_mtime',$na);
$aarx = explode("diskused",$aex[0]);
foreach ($aarx as $tax)
{
$rexp = explode(":",$tax);
$reaexp = str_replace(" ","",$rexp[1]);
if ($reaexp!="")
{
$lex = explode($quota,$reaexp);
$naex = explode("\n",$lex[0]);
echo $naex[1]."\n";
}
}
}
}
}
?>

Upvotes: 3

Related Questions