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.

Regex to filter listpops function by domain

Discussion in 'cPanel Developers' started by rlesterphx, Apr 20, 2011.

  1. rlesterphx

    rlesterphx Member

    Joined:
    Jul 22, 2010
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    1
    I'm trying to create a function that's based off the API2 Email module's 'listpops' function (ApiEmail < ApiDocs/Api2 < TWiki) to return only email accounts for a specified domain. The cPanel account I'm pulling information from has a lot of addon domains and a lot of different email addresses.
    So for instance if I want to just list all the email accounts for addon domain 'domain1.com' I would just pass that string to my function and it would use the listpops function with the optional regex parameter to filter all the email accounts and only return the ones ending in '@domain1.com'.

    Regular expressions always give me a headache and I'm not 100% sure that I'm understanding the listpops function correctly, so I was wondering if someone could tell me if this will work, and if so what would be a good regex string to pass?

    Thanks in advance!
     
  2. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Hi rlesterphx,

    You should be able to use the 'listpops' as is and filter out the "@$domain" within your script.

    The following code uses the PHP XML-API class to make the API2 Email::listpops function call and then parses out just the pop name:

    PHP:
    <?php
    include("xmlapi.php");

    $ip "10.1.4.1";
    $auth_user 'dave';
    $auth_pass 'secret!';
    $cpuser 'dave';

    $xmlapi = new xmlapi($ip);
    $xmlapi->password_auth($auth_user$auth_pass);
    $xmlapi->set_output 'json'//using JSON ensures a list reference is returned, even if one result is found
    $xmlapi->set_port(2083);

    // for debugging result
    //$xmlapi->set_debug(1);

    // set to null for a full list, otherwise specify a known domain for $cpuser
    //$domain_to_hunt_for =  null;
    $domain_to_hunt_for 'extradave.com';

    if (
    $domain_to_hunt_for) {
        
    $args = array(
            
    'regex' => '@' $domain_to_hunt_for ,
        );
    } else {
        
    $args = array();
    }


    $result $xmlapi->api2_query($cpuser'Email''listpops'$args);

    if( 
    $domain_to_hunt_for ){
        echo 
    "This is a refined list of emails for the {$domain_to_hunt_for} domain\n";

        foreach (
    $result->data as $email_reference) {
            
    $email_address str_replace'@' $domain_to_hunt_for''$email_reference->email);
            
    $email_names[] = $email_address;
            echo 
    "\t'{$email_address}'\n";
        }
    }else{
        echo 
    "This is all the emails addressed related to the {$cpuser} account\n";

        foreach (
    $result->data as $email_reference) {
            echo 
    "\t'{$email_reference->email}'\n";
        }
    }
    ?>
    Here is the results, the first being with no domain regex passed to the call, and then for just the 'extradave.com' domain:

    Code:
    [David:~/xmlapi_dave] $ php -f listpop.php 
    This is all the emails addressed related to the dave account
    	'test@dave.com'
    	'blah@afavaf.dave.com'
    	'lilextrapop@extradave.com'
    	'anotherpop@extradave.com'
    	'dave'
    
    [David:~/xmlapi_dave] $ php -f listpop.php 
    This is a refined list of emails for the extradave.com domain
    	'lilextrapop'
    	'anotherpop'
    
    [David:~/xmlapi_dave] $
    
    That said, you could write you own Perl API2 custom module to filter the domain string out of the result hash, but unless you have a compelling reason, it probably should be handled on the client (calling script) side as it's more involved create an API2 call that mutates the response....one such reason might be if you developing for the cPanel system, as opposed to a remote system (ie, a billing or other aggregate system or UI).

    Also, as a side note, the "regex" value that one would pass in such a call is a string that will be used within a regular expression on the Perl backend...it's not the regular expression itself: ie, ".*(what|where)ever" and not "/.*(what|where)ever/"...just FYI :)

    Regards,
    -DavidN
     
  3. rlesterphx

    rlesterphx Member

    Joined:
    Jul 22, 2010
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    1
    Great, thanks so much David! That's exactly all the info I was looking for.
     
  4. maumart

    maumart Registered

    Joined:
    Aug 31, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Reseller Owner
    --SOLVED --
    good result tanks!

    You can cancel the post
     
    #4 maumart, Aug 31, 2012
    Last edited: Aug 31, 2012
Loading...

Share This Page