The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

List all email accounts and forwarders on the server

Discussion in 'E-mail Discussions' started by jeremy@dailydna, Apr 9, 2009.

  1. jeremy@dailydna

    Joined:
    May 29, 2008
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    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
     
  2. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    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.
     
  3. jeremy@dailydna

    Joined:
    May 29, 2008
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    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
     
  4. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    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.
     
  5. elkram

    elkram Active Member

    Joined:
    Nov 21, 2004
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    6
    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?
     
    #5 elkram, Apr 10, 2009
    Last edited: Apr 10, 2009
  6. jeremy@dailydna

    Joined:
    May 29, 2008
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    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 - "info@allamericancitylandscaping.com". However it is not listed in the following information.

    Here is what is listed when I run the script:
    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:
    PHP File:
    PHP:
    <?php
    # cpanel12 - api2_example_withargs.php                Copyright(c) 1997-2009 cPanel, Inc.
    #                                                           All Rights Reserved.
    # copyright@cpanel.net                                         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) );
    }

    ?>
     
  7. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Looking at your URL, it seems there's a Windows newline after the username. That could be causing the complications.
     
    #7 cPanelDavidG, Apr 10, 2009
    Last edited: Apr 10, 2009
  8. jeremy@dailydna

    Joined:
    May 29, 2008
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    That was it - after I stripped the "\r\n" from the line it printed all the email accounts out fine.
     
  9. jeremy@dailydna

    Joined:
    May 29, 2008
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    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.
    # copyright@cpanel.net                                         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) );

    }

    ?>
     
    #9 jeremy@dailydna, Apr 10, 2009
    Last edited: Apr 10, 2009
Loading...

Share This Page