IMAP, PHP & MySQL, Tutorials

Cpanel create emails with php

Here’s a easy way to create new email addresses for Cpanel using PHP. I recently needed the ability to create a new email address for a project everytime I created a new project it needed an email address to go with it.

I came up with a reusable class that can create and delete email addresses:

class Cpanel {

    static public function action($params = array()){

    	$cpuser      = (isset($params['cpuser']))      ? $params['cpuser']       : '';
    	$cppass      = (isset($params['cppass']))      ? $params['cppass']       : '';
    	$cpdomain    = (isset($params['cpdomain']))    ? $params['cpdomain']     : '';
    	$cpskin      = (isset($params['cpskin']))      ? $params['cpskin']       : 'x3';
    	$emailname   = (isset($params['emailname']))   ? $params['emailname']    : '';
    	$emaildomain = (isset($params['emaildomain'])) ? $params['emaildomain']  : '';
    	$emailpass   = (isset($params['emailpass']))   ? $params['emailpass']    : '';
    	$quota       = (isset($params['quota']))       ? $params['quota']        : '250';
    	$action      = (isset($params['action']))      ? $params['action']       : '';

	    switch ($action) {
	    	case 'createemail':
	    		fopen("https://$cpuser:$cppass@$cpdomain:2083/frontend/$cpskin/mail/doaddpop.html?email=$emailname&domain=$emaildomain&password=$emailpass&quota=$quota", "r");
	    		break;
	    	case 'deleteemail':
	    		fopen("https://$cpuser:$cppass@$cpdomain:2083/frontend/$cpskin/mail/realdelpop.html?email=$emailname&domain=$emaildomain", "r");
	    		break;
	    }

	    
    }

}

The class accepts an array to be passed this is a flexible way of passing data to the class, it means not all options need to be used for instance the cpanel username and password can be set in the class directly then you only need to pass in the options you want to set.

Using the class

Include the class into your file.

require('cpanel.php');

Next call the class and the action method: Cpanel::action() inside the parenthesis pass an array of options.

To create a new email address:

Cpanel::action(array('emailname' => 'demo466', 'emailpass' => 'helllo123', 'action' => 'createemail'));

That would create a new email address of demo466@domain.com with the password of hello123.

To delete an existing email address:

Cpanel::action(array('emailname' => 'demo466', 'action' => 'deleteemail'));

The array can be defined first then passed to the method:

$params = array(
	'emailname' => 'demo466', 
	'emailpass' => 'helllo123', 
	'action' => 'createemail'
)
Cpanel::action($params);

Example with all options used:

$params = array(
	'cpuser' => 'cpanelusername', 
	'cppass' => 'cpanelpassword', 
	'cpdomain' => 'domain.com', 
	'cpskin' => 'x3', 
	'emailname' => 'demo466', 
	'emailpass' => 'helllo123', 
	'emaildomain' => 'domain.com', 
	'quota' => '500',  
	'action' => 'createemail'
)
Cpanel::action($params);

 

David Carr

David Carr

For the past 12 years, I’ve been developing applications for the web using mostly PHP. I do this for a living and love what I do as every day there is something new and exciting to learn.

In my spare time, the web development community is a big part of my life. Whether managing online programming groups and blogs or attending a conference, I find keeping involved helps me stay up to date. This is also my chance to give back to the community that helped me get started, a place I am proud to be apart of.

Besides programming I love spending time with friends and family and can often be found together going out catching the latest movie, staying in playing games on the sofa or planning a trip to someplace I’ve never been before.