List all email accounts and forwarders on the server

May 29, 2008
7
0
51
I want to be able to list all of the email accounts and forwarders setup for every domain on the server.

I don't want to have to go through each and every account and look at the email accounts for that account. Is there a way to list all accounts through the API or through a script?

Reason is - I need a human readable backup of all email accounts on the server.

Thanks,
Jeremy
 

cPanelDavidG

Technical Product Specialist
Nov 29, 2006
11,216
12
313
Houston, TX
cPanel Access Level
Root Administrator
I want to be able to list all of the email accounts and forwarders setup for every domain on the server.

I don't want to have to go through each and every account and look at the email accounts for that account. Is there a way to list all accounts through the API or through a script?

Reason is - I need a human readable backup of all email accounts on the server.

Thanks,
Jeremy
You can do this for a single account using API2 calls from the XML API. The API2 documentation for listing email acccounts is available at:

http://www.cpanel.net/plugins/api2/Cpanel::Email::listpopswithdisk.pod.html

For listing email forwarders, visit:

http://www.cpanel.net/plugins/api2/Cpanel::Email::listforwards.pod.html

I believe if you omit the domain field, it will list all forwarders on an account. I haven't re-tested that in about a year though.

As for getting a list of accounts on the server, that can be done via the XML API:

http://cpanel.net/plugins/xmlapi/listaccts.html

You wont need to worry too much about understanding how to implement the XML API and calling API2 functions from this API if you use a PHP class one of my friends over in QA has already assembled for making it easier to use our APIs:

http://forums.cpanel.net/showthread.php?t=111897

This was a bit of a brief overview of everything so please let me know if you need clarification or assistance with anything.
 
May 29, 2008
7
0
51
Hi David,

I've never messed around with the API at all - I am very new to it. The documentation says that in order to list all email accounts for an account I need to run something similar to this:

"<?cp Email::listpopswithdisk(% % % email,diskused,diskquota) domain=exampledomain.com ?>"

I don't understand where I need to put this in order to get the list. I know how to get a list of all domains - but I don't how to use this.

Thanks,
Jeremy
 

cPanelDavidG

Technical Product Specialist
Nov 29, 2006
11,216
12
313
Houston, TX
cPanel Access Level
Root Administrator
Hi David,

I've never messed around with the API at all - I am very new to it. The documentation says that in order to list all email accounts for an account I need to run something similar to this:

"<?cp Email::listpopswithdisk(% % % email,diskused,diskquota) domain=exampledomain.com ?>"

I don't understand where I need to put this in order to get the list. I know how to get a list of all domains - but I don't how to use this.

Thanks,
Jeremy
In the zip file available at http://forums.cpanel.net/showthread.php?t=111897 you will find an api2_example_withargs.php file which is an example of how to call an API2 function that has arguments via this PHP class. You would just switch the getdiskusage function (from the example) with listpopswithdisk (the function you want to use) and omit the login parameter (by removing , login=>$email_account) and you have your function call.
 

elkram

Active Member
Nov 21, 2004
31
0
156
I don't know if what you want is to be able to output all of that info for all domains into one file, but at the very least, isn't Configserver Mail Manage a big step in the right direction?
 
Last edited:
May 29, 2008
7
0
51
I have modified the "api2_example_withargs.php" file but it does not seem to be listing the correct information.

I have picked out below the "allamericancitylandscaping.com" account. I know that they have 1 email account setup - "[email protected]". However it is not listed in the following information.

Here is what is listed when I run the script:
RAW API2 CALL:
<cpanelaction><module>Email</module><func>listpopswithdisk</func><args><domain>allamericancitylandscaping.com</domain></args></cpanelaction>



QUERY:
https://99.198.100.87:2087/xml-api/...andscaping.com</domain></args></cpanelaction>

RAW XML:

<cpanelresult>
<apiversion>2</apiversion>
<func>listpopswithdisk</func>
<module>Email</module>
</cpanelresult>


object(SimpleXMLElement)#1 (3) {
["apiversion"]=>
string(1) "2"
["func"]=>
string(16) "listpopswithdisk"
["module"]=>
string(5) "Email"
}
SIMPLEXML OBJ:
I have also included the modified api2_example_withargs.php. It reads in a CSV file that has all account names and domains listed. I have included the relevant entry below as well as the script.

CSV FILE:
allamericancitylandscaping.com,allameri
PHP File:
PHP:
<?php
# cpanel12 - api2_example_withargs.php                Copyright(c) 1997-2009 cPanel, Inc.
#                                                           All Rights Reserved.
# [email protected]                                         http://cpanel.net

include("xmlapi.php.inc");

$ip = "127.0.0.1";
$root_pass = "PASSWORD";

$accounts_list = file("/home/scripts/cp_xmlapi_php/account_list.csv");
$domains_list = file("/home/scripts/cp_xmlapi_php/domain_list.csv");
$domains_accounts = file("/home/scripts/cp_xmlapi_php/domains_list.csv");

foreach($domains_accounts as $acc_user)
{
$accounts_user = explode(",",$acc_user);
$account = $accounts_user[1];
//$email_account = "someemail";
$email_domain = $accounts_user[0];

$xmlapi = new xmlapi($ip);
$xmlapi->password_auth("root",$root_pass);

$xmlapi->set_debug(1);
//print $xmlapi->api2_query($account, "Email", "listmaildomains", $account);
print $xmlapi->api2_query($account, "Email", "listpopswithdisk", array(domain=>$email_domain) );
}

?>
 
May 29, 2008
7
0
51
If anyone is interested, this is the end result - I didn't take time to format the output or anything.




PHP:
<?php
# cpanel12 - api2_example_withargs.php                Copyright(c) 1997-2009 cPanel, Inc.
#                                                           All Rights Reserved.
# [email protected]                                         http://cpanel.net

include("xmlapi.php.inc");

$ip = "0.0.0.0";
$root_pass = "PASSWORD";

$domains_accounts = file("/home/scripts/cp_xmlapi_php/domains_list.csv");

foreach($domains_accounts as $domain_user)
{
$domain_user = str_replace("\r\n", "", $domain_user);
$domain_user = explode(",",$domain_user);
$account = $domain_user[1];
$email_domain = $domain_user[0];

$xmlapi = new xmlapi($ip);
$xmlapi->password_auth("root",$root_pass);

$xmlapi->set_debug(1);
echo "Domain: $email_domain \n";
echo "Email Accounts: ";
print $xmlapi->api2_query($account, "Email", "listpopswithdisk", array(domain=>$email_domain) );
echo "Domain Forwarders: \n";
print $xmlapi->api2_query($account, "Email", "listforwards", array(domain=>$email_domain) );

}

?>
 
Last edited: