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

cPax

Registered
Jul 28, 2014
2
0
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.
 

cPax

Registered
Jul 28, 2014
2
0
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
[email protected] : [email protected], [email protected]

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.
 

N1ghteyes

Active Member
Mar 3, 2014
33
2
58
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 protected]', 'password' => 'password123', 'quota' => 0, 'domain' => 'email.com'));

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

PHP: Overloading - Manual
 
Last edited:

Angela S

Registered
Oct 27, 2015
1
0
1
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 [email protected] 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][email protected][/EMAIL]', 'password' => 'mypwd', 'quota' => 0, 'domain' => 'example.com'));
Is this a known problem with the cpanelUAPI class? Thank you for any help.
 

N1ghteyes

Active Member
Mar 3, 2014
33
2
58
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