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.

php: does email account exist?

Discussion in 'E-mail Discussions' started by davidn, May 5, 2009.

  1. davidn

    davidn Member

    Joined:
    May 5, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    I need to query cpanel's email from php to enable end users to find out if an email username is available so they can choose one before deciding whether to commit.

    I've searched the web for two weeks and found no code for this at all, not even a list of the email functions Cpanel does support.

    (All I have found is doaddpop and realdelpop; I'm using the first and it works)

    ____________

    2. And I want them to use Roundcube - what code do I need to take them straight there?

    Can you help please?

    Many thanks.

    David.
     
  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
    Documentation for API1 and API2 (used for the cPanel interface) is still a work in progress. However, listpopswithdisk is the API call you want to use to get a list of email accounts:

    http://twiki.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/Api2ListPopsWithDisk

    There is a input parameter called regex you can use (not listed in that documentation yet) where you can use a Perl Regular Expresison to search for a specific email address. You could perhaps use this to determine if an email address exists (as 1 or more results will be returned).


    Keep in mind, by bringing your users directly into Roundcube, you are not allowing them to change their own email password, set server-side mail filtering, create vacation auto-responders etc.

    Here's a thread on how to do this: http://forums.cpanel.net/showthread.php?p=508341
     
    #2 cPanelDavidG, May 5, 2009
    Last edited: May 5, 2009
  3. davidn

    davidn Member

    Joined:
    May 5, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    regex and listpopswithdisk: Does email account exist?

    The direct access to roundcube looks great. Thanks.

    I can't find any info on regex as far as checking to see if email accounts exist.

    and listpopswithdisk gives errors; with the URL:

    https://domainname.com:2087/xml-api/cpanel?user=bob&xmlin=<cpanelaction><module>email</module><func>listpopswithdisk</func><args><domain>domainname.com</domain></args></cpanelaction>

    Errors:
    "This XML file does not appear to have any style information associated with it. The document tree is shown below:


    <cpanelresult>
    <apiversion>1</apiversion>
    <type>text</type>

    <data>
    <result>0</result>
    <reason>No Valid Command Given.</reason>
    </data>
    </cpanelresult>"

    The only code for listpopswithdisk gives the following errors:

    Warning: main(xmlapi.php.inc) [function.main]: failed to open stream: No such file or directory in /home/eutruth/public_html/f1/cplistaccounts.php on line 6

    Warning: main() [function.include]: Failed opening 'xmlapi.php.inc' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/eutruth/public_html/f1/cplistaccounts.php on line 6

    Warning: file(/home/scripts/cp_xmlapi_php/domains_list.csv) [function.file]: failed to open stream: No such file or directory in /home/eutruth/public_html/f1/cplistaccounts.php on line 11

    Warning: Invalid argument supplied for foreach() in /home/eutruth/public_html/f1/cplistaccounts.php on line 13
    "


    May I please ask if you have or know of any code which works with either of these?

    David.
     
  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
    Like I said, this is not yet documented. You can use the regex parameter to see if an email account exists. You would know if the account exists since the API call will return more than 0 accounts (rather than 0 accounts if there was no match).

    The regex parameter takes in a Perl Regular Expression. All email accounts belonging to this user are then compared against this regular expression. Any matches are returned by the API.

    What version of cPanel/WHM are you using? Please include branch (CURRENT, STABLE, RELEASE?) and build number.

    If you need a PHP 5 class that interacts with our APIs, I recommend looking at this thread: http://forums.cpanel.net/showthread.php?t=111897
     
  5. davidn

    davidn Member

    Joined:
    May 5, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    Cpanel version:

    Cpanel version 11.24.4-RELEASE build 35075
     
  6. 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
    The problem was that you were using email as the module when the module is actually Email. These XML API URLs are case-sensitive.
     
  7. davidn

    davidn Member

    Joined:
    May 5, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    Still no check on existing email accounts works

    Many thanks, but module "Email" as opposed to "email" still gives the same cPanel result:

    <result>0</result>

    https://eutruth.org.uk:2087/xml-api/cpanel?user=myself&mypassword=<cpanelaction><module>Email</module><func>listpopswithdisk</func><args><domain>eutruth.org.uk</domain></args></cpanelaction>

    (And you have to use WHM to get even this.)

    So I still have no way of listing my email accounts under cPanel, either with regex or listpopswithdisk.

    Does anyone know how to check if an email account exists under cPanel?

    I'd much appreciate any help.

    David.
     
  8. 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
    That function should work even over the cPanel ports. I'm not sure where you're getting the "mypassword" syntax from. What you have as "mypassword" should be "xmlin" just like in the sample in our documentation:

    http://twiki.cpanel.net/twiki/bin/v...onIntegration/CallingAPIFunctions#Normal_Mode
     
  9. davidn

    davidn Member

    Joined:
    May 5, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    Partial success

    Thanks David, from the URL this now works:

    https://mysite.com:2087/xml-api/cpanel?user=username&xmlin=<cpanelaction><module>Email</module><func>listpopswithdisk</func><args><domain>eutruth.org.uk</domain></args></cpanelaction>

    But cPanel won't allow it from within php:

    <?php
    // set the variable I'm searching for - an email account
    $email = "davidn";

    $data = file_get_contents("https://eutruth.org.uk:2087/xml-api/cpanel?user=username&xmlin=<cpanelaction><module>Email</module><func>listpopswithdisk</func><args><domain>mysite.com</domain></args></cpanelaction>");

    if(preg_match("/$email/", $data)){
    print("
    $email exists
    ");
    }
    ?>

    Error message:
    Warning: file_get_contents(https://mysite.com:2087/xml-api/cpanel?user=username&xmlin=<cpanelaction><module>Email</module><func>listpopswithdisk</func><args><domain>mysite.com</domain></args></cpanelaction>) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 401 Access Denied Still Working in /home/username/public_html/f1/checkdn.php on line 2

    How do I get round this?

    Thanks,

    David N.
     
  10. 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
    To get around this error, have your PHP script authenticate to the cPanel/WHM server. There are many threads on how to do this.

    If you don't want to do that yourself, feel welcome to make use of our pre-assembled PHP 5 class available at: http://forums.cpanel.net/showthread.php?t=111897
     
  11. davidn

    davidn Member

    Joined:
    May 5, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    Still fails with authentication

    It doesn't matter whether I'm authenticated to cPanel or to WHM.

    I still get the same error message.

    Can you please show me some code that works, or perhaps a different function that cPanel accepts?

    David N.
     
  12. 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
    Whether you are authenticated to cPanel/WHM or not is irrelevant to the PHP script being authenticated or not.

    Here's some sample code that includes the PHP script authenticating into the WHM interface:

    http://forums.cpanel.net/showpost.php?p=322538&postcount=6
     
    #12 cPanelDavidG, May 11, 2009
    Last edited: May 11, 2009
Loading...

Share This Page