Black Friday sale is now on! 50% off Laravel: The Modular Way. Learn more

Cpanel create emails with php

David Carr

IMAP PHP & MySQL Tutorials

  • Using the class
  • 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.

    Download source files

    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");
    	    	case 'deleteemail':
    	    		fopen("https://$cpuser:$cppass@$cpdomain:2083/frontend/$cpskin/mail/realdelpop.html?email=$emailname&domain=$emaildomain", "r");

    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.


    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 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'

    Example with all options used:

    $params = array(
    	'cpuser' => 'cpanelusername', 
    	'cppass' => 'cpanelpassword', 
    	'cpdomain' => '', 
    	'cpskin' => 'x3', 
    	'emailname' => 'demo466', 
    	'emailpass' => 'helllo123', 
    	'emaildomain' => '', 
    	'quota' => '500',  
    	'action' => 'createemail'


    Laravel Modules Book by David Carr

    Help support the blog so that I can continue creating new content!

    Subscribe to my newsletter

    Subscribe and get my books and product announcements.

    © 2009 - 2022 DC Blog. All code MIT license. All rights reserved.