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.

How to make new email accounts for cPanel user, from PHP script

Discussion in 'cPanel Developers' started by cPax, Jul 28, 2014.

  1. cPax

    cPax Registered

    Joined:
    Jul 28, 2014
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Hello,

    I need to add new email accounts to a cPanel user, from a PHP script called through Apache, like so:
    http://example.com/add-email-account.php

    I also need to add a forwarder for each new email account, from a PHP script called through Apache, like so:
    http://example.com/add-email-forwarder.php

    I thought LiveAPI would allow this, but apparently it doesn't.

    Can you show me how to do this?

    Thank you.
     
  2. cPax

    cPax Registered

    Joined:
    Jul 28, 2014
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Root Administrator
    Hello,

    Thank you for the reference to the PHP client class. However, it doesn't appear to support addforward or delpop.

    For a remote application, I got addpop addforward and delpop to work by accessing the cPanel API-2 functions through the WHM API with hash authentication.

    1) Note on addforward:

    addforward duplicates a pre-existing forward without warning, so if you call addforward twice on the same email account, your forwards file look like this (for example.com):

    # cat /etc/valiases/example.com
    user@example.com : user@gmail.com, user@gmail.com

    2) Note on delpop:

    delpop takes many seconds to return, which causes the web server to timeout. The email account targeted by delpop for deletion may or may not be deleted, but either way you get no confirmation because the web server terminates the connection.

    3) There is no delforward function.

    4) What is the fastest way to determine whether or not a given email account already exists on the server?

    Thank you.
     
  3. N1ghteyes

    N1ghteyes Active Member

    Joined:
    Mar 3, 2014
    Messages:
    33
    Likes Received:
    2
    Trophy Points:
    8
    Location:
    Reading, UK
    cPanel Access Level:
    Root Administrator
    Twitter:
    https://documentation.cpanel.net/display/SDK/UAPI+-+Email

    The UAPI might well do what you need,

    if i understand correctly, then specifically:
    https://documentation.cpanel.net/display/SDK/UAPI+-+Email#UAPI-Email-add_forwarder
    https://documentation.cpanel.net/display/SDK/UAPI+-+Email#UAPI-Email-delete_forwarder
    https://documentation.cpanel.net/display/SDK/UAPI+-+Email#UAPI-Email-add_pop
    https://documentation.cpanel.net/display/SDK/UAPI+-+Email#UAPI-Email-delete_pop

    and you could use these to check if an email exists as a pop or forward:
    https://documentation.cpanel.net/display/SDK/UAPI+-+Email#UAPI-Email-list_forwarders
    https://documentation.cpanel.net/display/SDK/UAPI+-+Email#UAPI-Email-list_pops

    I have also written a simple UAPI class (pretty much just handles connections and magic function calls) which might be of some help here:

    https://github.com/N1ghteyes/cpanel-UAPI-php-class

    There is an example for mysql databases in there, but it is the same principle. Set the scope to Email and then just call any UAPI functions like you would class methods.

    e.g.

    include "../cpaneluapi.class.php"; //include the class file
    $uapi = new cpanelUAPI('cPuser', 'cPpass', 'cPanel.example.com'); //instantiate the object
    $uapi->scope = 'Email'; //use the email module
    //create an email address
    $uapi->add_pop(array('email' => 'email@email.com', 'password' => 'password123', 'quota' => 0, 'domain' => 'email.com'));

    simple as that :). The class uses PHP magic functions inplace of methods, see:

    PHP: Overloading - Manual
     
    #4 N1ghteyes, Aug 3, 2014
    Last edited: Aug 3, 2014
  4. Angela S

    Angela S Registered

    Joined:
    Oct 27, 2015
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Muncie, IN
    cPanel Access Level:
    Website Owner
    I am using your cpanelUAPI class to create an email account. It works except that it seems that add_pop is being called twice. The email address is created, but I get the error message of

    'The account test@example.com already exists!'. The following is my code.
    Code:
    $uapi = new cpanelUAPI('user', 'password, 'server.com');
         
    $uapi->scope = 'Email';
            $data = $uapi->add_pop(array('email' => '[EMAIL]test@example.com[/EMAIL]', 'password' => 'mypwd', 'quota' => 0, 'domain' => 'example.com'));
         
    
    Is this a known problem with the cpanelUAPI class? Thank you for any help.
     
  5. N1ghteyes

    N1ghteyes Active Member

    Joined:
    Mar 3, 2014
    Messages:
    33
    Likes Received:
    2
    Trophy Points:
    8
    Location:
    Reading, UK
    cPanel Access Level:
    Root Administrator
    Twitter:
    Hi Angela

    It is a known bug, theres a fix waiting to be pushed to git, but in the mean time, change line 99 from $content = $this->curl_exec_follow($ch); to $content = $this->curl_exec_follow($ch, 0);

    that should do it
     
Loading...

Share This Page