Create emails accounts using PHP
Asked Answered
A

2

5

I am in the process of trying to create emails using PHP.

This is my code so far it is very basic until I can get a working script. This is the closest I have got but it says it has added the email although in cpanel the email doesn't exist so it obviously isn't :)

Please note the following information within this code has been edited for security reasons (e.g. not a real password, username or domain).

This is the code I found and have been trying to work out..

<?php

// cPanel info
$cpuser = 'someusername'; // cPanel username
$cppass = 'somepassword'; // cPanel password
$cpdomain = 'somesite.com'; // cPanel domain or IP
$cpskin = 'someskin';  // cPanel skin. Mostly x or x2. 
// See following URL to know how to determine your cPanel skin
// http://www.zubrag.com/articles/determine-cpanel-skin.php

// Default email info for new email accounts
// These will only be used if not passed via URL
$epass = 'hispassword'; // email password
$edomain = 'somesite.com'; // email domain (usually same as cPanel domain above)
$equota = 20; // amount of space in megabytes


function getVar($name, $def = '') {
  if (isset($_REQUEST[$name]))
    return $_REQUEST[$name];
  else 
    return $def;
}

// check if overrides passed
$euser = getVar('user', '');
$epass = getVar('pass', $epass);
$edomain = getVar('domain', $edomain);
$equota = getVar('quota', $equota);

$msg = 'check';

if (!empty($euser))
while(true) {

  // Create email account
  $f = fopen ("http://$cpuser:$cppass@$cpdomain:2082/frontend/$cpskin/mail/doaddpop.html?email=$euser&domain=$edomain&password=$epass&quota=$equota", "r");
  if (!$f) {
    $msg = 'Cannot create email account. Possible reasons: "fopen" function allowed on your server, PHP is running in SAFE mode';
    break;
  }

  $msg = "<h2>Email account {$euser}@{$edomain} created.</h2>";

  // Check result
  while (!feof ($f)) {
    $line = fgets ($f, 1024);
    if (ereg ("already exists", $line, $out)) {
      $msg = "<h2>Email account {$euser}@{$edomain} already exists.</h2>";
      break;
    }
  }
  @fclose($f);

  break;

}

?>
<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" value="<?php echo htmlentities($euser); ?>" /></td></tr>
<tr><td>Password:</td><td><input name="pass" size="20" type="password" /></td></tr>
<tr><td colspan="2" align="center"><hr /><input name="submit" type="submit" value="Create Email Account" /></td></tr>
</table>
</form>
</body>
</html>

Thank you in advance :)

Andrew

Atheling answered 25/7, 2013 at 22:50 Comment(6)
What is the question actually?Goodyear
Is there a better way of doing this that actually works? - without tearing apart some open source client to achieve this.Atheling
does this have to go through cpanel? (may help: #6422700)Upanchor
It doesn't have to, no. The reason I suggested cpanel is because I don't use any alternative way of adding them. However with this said in relation to the linked post, the code I am having problems with is the suggested solution for that thread.Atheling
Got the following script from: #17094968 This script works :)Atheling
u r question is related to #17094968 refer this postEijkman
S
4

I think this is what you're looking for:

$socket = fsockopen($cpdomain,2082);
$cuser = "YourUserName";
$cpassword = "YourPassword";
$authstr = base64_encode("".$cpuser.":".$cppass."");
$in = "GET /frontend/$cpskin/mail/doaddpop.html?email=$euser&$edomain&password=$epass&quota=$equota
HTTP/1.0\r\nAuthorization: Basic $authstr \r\n";
fputs($socket,$in);
fclose( $socket );
Seigler answered 25/7, 2013 at 23:41 Comment(0)
S
5

There is an xml api for cpanel, and some php classes to make the requests. Many people have server configurations that make fopen problematic, but the xmlapi.php file works on many different web hosts.

include("xmlapi.php");        //XMLAPI cpanel client class

$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 unsecure port# 2082
$email_domain ="example.com";
$email_user ="john";
$email_pass ="johnspassword";
$email_quota = 0;             // 0 is no quota, or set a number in mb

$xmlapi = new xmlapi($ip);
$xmlapi->set_port($port);     //set port number.
$xmlapi->password_auth($account, $passwd);
$xmlapi->set_debug(0);        //output to error file  set to 1 to see error_log.

$call = array(domain=>$email_domain, email=>$email_user, password=>$email_pass, quota=>$email_quota);

$result = $xmlapi->api2_query($account, "Email", "addpop", $call );

print_r($result);            //show the result of your query

The full list of email functions in the api is available here. https://documentation.cpanel.net/display/SDK/cPanel+API+2+-+Email

Schooling answered 24/10, 2014 at 17:40 Comment(0)
S
4

I think this is what you're looking for:

$socket = fsockopen($cpdomain,2082);
$cuser = "YourUserName";
$cpassword = "YourPassword";
$authstr = base64_encode("".$cpuser.":".$cppass."");
$in = "GET /frontend/$cpskin/mail/doaddpop.html?email=$euser&$edomain&password=$epass&quota=$equota
HTTP/1.0\r\nAuthorization: Basic $authstr \r\n";
fputs($socket,$in);
fclose( $socket );
Seigler answered 25/7, 2013 at 23:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.