How to access list of email accounts with cPanel API?

家住魔仙堡 提交于 2019-12-11 03:22:41

问题


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.


回答1:


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";
}
}
}
}
}
?>



回答2:


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": "yourName@yourdomain.com",
     "FriendlyName": "Name"
  },
  {
     "Id": 33,
     "Email": "yourOtherName@yourotherdomain.com",
     "FriendlyName": "OtherName"
  }
]

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



来源:https://stackoverflow.com/questions/25528511/how-to-access-list-of-email-accounts-with-cpanel-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!