Taking too long? Close loading screen.
Opening Up A New Dimension

Creating email accounts & forwarders on webmail using PHP & CPanel API

Posted by: Herald Lynx Comments: 0 0 Post Date: May 26, 2016

Recently one of our clients asked: “Can I create email accounts within a php script?” Taking in consideration high possibilities of cPanel API we have decided to search for related requests on the Internet and have found out that many people are interested in this question.

What is this needed for? For example, we have an awesome domain and you want to become a mail service provider, but creating mailboxes manually is not going to work. You can create addon domains, redirects and other things in the same way.

We need following for the script:

1. Request form (file index.php can be found in the attachments).
2. cPanel XML API Client Class (file xmlapi.php can be found in the attachments).

Let’s check script of the form in detail:

$ip = "127.0.0.1";                // should be server IP address or 127.0.0.1 if local server
$account = "username";            // cPanel user account name
$passwd ="password";            // cPanel user password
$port =2083;                    // cpanel secure authentication port
$email_domain = 'domain.com';   // email domain (usually same as cPanel domain)
$email_quota = 50;                // default amount of space in megabytes

Creating XMLAPI request:

$xmlapi = new xmlapi($ip);

//set port number. cpanel client class allow you to access WHM as well using WHM port.
$xmlapi->set_port($port);

// authorization with password. Not as secure as hash.
$xmlapi->password_auth($account, $passwd);

// cpanel email addpop function Parameters
$call = array(domain=>$email_domain, email=>$email_user,password=>$email_pass, quota=>$email_quota);

// cpanel email fwdopt function Parameters
$call_f  = array(domain=>$email_domain, email=>$email_user,
fwdopt=>”fwd”, fwdemail=>$dest_email);

//output to error file  set to 1 to see
$xmlapi->set_debug(0);
error_log.

// making call to cpanel api
$result = $xmlapi->api2_query($account, “Email”, “addpop”,
$call);

//create a forward
$result_forward = $xmlapi->api2_query($account, “Email”, “addforward”,
$call_f);

And of course creating HTML form:

<html>
<head>
<title>cPanel Email Account Creator</title>
</head>

<body>
<?php echo ‘<div style=”color:red”>’.$msg.'</div>’; ?>

<h1>cPanel Email Account Creator</h1>

<form name=”frmEmail” method=”post”>
<table width=”400″ border=”0″>
<tr>
<td>Username:</td>
<td><input name=”user” size=”20″ type=”text”/></td>
</tr>
<tr>
<td>Password:</td>
<td><input name=”pass” size=”20″ type=”password”/></td>
</tr>
<tr>
<td>Verify Password:</td>
<td><input name=”vpass” size=”20″type=”password” /></td>
</tr>
<tr>
<td>Forwarde:</td>
<td><input name=”forward” size=”20″ type=”text”/></td>
</tr>
<tr>
<td colspan=”2″ align=”center”><hr /><input name=”submit” type=”submit” value=”Create Email” /></td>
</tr>
</table>
</form>
</body>
</html>

PHP_Account_Creator.zip Mediafire Link

Related links:

Creating multiple email account in cPanel with PHP and XMLAPI | Home Based Coder | Filipino Web Designer + Web Developer
Create Email Account using API
XMLAPI php class
Auto create email with PHP
cPanel XML and JSON APIs
xpl-api and cPanel APIs via PHP

Share this post

Leave a Reply