Cpanel create emails with php

David Carr

2 min read - 3rd Mar, 2015

Table of Contents

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: https://github.com/daveismynamecom/cpanelemails/tree/master

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);

 

0 comments
Add a comment

Copyright © 2006 - 2024 DC Blog - All rights reserved.