How to get all email accounts by API?

Yuriy Revido

Member
Sep 1, 2023
9
3
3
Bali
cPanel Access Level
Reseller Owner
Hi there!
Is there a way to get all the email accounts under the WHM level by using a single API call?

It says everywhere that this would require some scripting to automate the process and aggregate the results by using listaccts first and then iterating through by Email::list_pops endpoints

We'd like to get all email accounts with a single API fired.

Thanks in advance.
 
Last edited by a moderator:

cPanelThomas

Developer
Feb 16, 2023
32
23
83
cPanel
cPanel Access Level
Root Administrator
Well, it's not *really* that difficult in my estimation.

Basically the process is:
* Get the accounts via `listaccts` API call
* Iterate over accounts and run `list_pops_for` API call on each of them (or use WHMAPI batch to run it for all of them in one shot -- that'd probably be the best way).

Thankfully, whmapi.pl does not impose any RFC based restrictions on URL length, so even a GET with 1000 accounts specified in the batch call should still work properly.
That said, I'd probably submit that one as a POST just to avoid sending usernames over the wire unencrypted (as all URLs go across that way).

An example of the output when using the CLI looks like:
Code:
# bin/whmapi1 --output=jsonpretty batch command='list_pops_for?user=cptest' command='list_pops_for?user=reselleur'
{
   "data" : {
      "result" : [
         {
            "data" : {
               "pops" : [
                  "[email protected]"
               ]
            },
            "metadata" : {
               "command" : "list_pops_for",
               "reason" : "OK",
               "result" : 1,
               "version" : 1
            }
         },
         {
            "data" : {
               "pops" : []
            },
            "metadata" : {
               "command" : "list_pops_for",
               "reason" : "OK",
               "result" : 1,
               "version" : 1
            }
         }
      ]
   },
   "metadata" : {
      "command" : "batch",
      "reason" : "OK",
      "result" : 1,
      "version" : 1
   }
}
Of course for an HTTP request, you'd want to URI encode the values passed in as command if doing this as a GET, but that should go without saying (and our docs for batch also mention this).
 
  • Like
Reactions: cPRex