We help IT Professionals succeed at work.

How create email account with PHP ASAP

sanalyst
sanalyst asked
on
I want to know how I can create new POP3 email account from PHP ?

I have only normal account on web server that meaning I can't access the web server.

I have also cpanel version 11.25 I try to using automatic create account with cpanel but is not working with this link
http://www.zubrag.com/scripts/cpanel-create-email-account.php
because that code is old and not working with cpanel 11.25
Comment
Watch Question

Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
I would not expect that solution in the link to work.  Creating accounts is an admin function and usually can't be accessed by PHP thru a web page.

Author

Commented:
But cpanel have API's you can use it to do more things like create / delete email account
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
While I'm sure it does, that doesn't mean that your hosting company will enable them for you.  You need to talk to them.  In general, hosting companies restrict your access to make sure that you can't cause problems on the server.  If you read the article in the link, you would have noticed that it didn't work for a lot of people.

Author

Commented:
Hello DaveBaldwin

I found fsockopen function can connect with cpanel 11.25

Check attach code

I use this code and success connect to cpanel but I send request "GET / HTTP/1.1\r\n" that return path "/fronend/x3/index.html", but I get this result in address bar "http://www.example.com/frontend/x3/index.html" and this is wrong.

After that I send another request to create enail but the return path is wrong.

Can you modify this code to work fine ?
<?php

	$hSocket = fsockopen("ssl://www.example.com", 2083);
	if (!$hSocket) { echo "Failed to open socket connection… Bailing out!\n"; exit; }  	
	
	$sAuth = base64_encode("cpuser:cppassword");  	  
	
	$sHTTP = "GET / HTTP/1.1\r\n";
	/*$sHTTP = "POST /frontend/x3/mail/doaddpo.html?email=test2010&domain=example.com&password=test2010&quota=50\r\n";*/
	$sHTTP .= "Host: example.com\r\n";
	$sHTTP .= "Authorization: Basic $sAuth\r\n";  
	$sHTTP .= "Connection: Close\r\n\r\n";
		
	fwrite($hSocket, $sHTTP); 
	while (!feof($hSocket))
	{
    	echo fgets($hSocket, 128);
   	}  
	
	echo "Done.";
	fclose($hSocket);  
?>

Open in new window

Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
No, you're beyond me at this point.  You need to 'Request Attention' and get the Cpanel zone added to your question.

Author

Commented:
DaveBaldwin
I do that
anyway thank you
greetings sanalyst, I was looking into using cpanel API, but did not get to implement it, however, it appears that for the newer versions of cpanel, that the old url call with parameters like ->
?email=test2010&domain=example.com&password=test2010&quota=50\r\n
has been replaced with a JASON - XML call, which I do not know many details about, however, there's info about it at the cpanel site which now can uses a PHP class to have calls like  $xmlapi->api2_query( );
check out some cpanel info at -

http://forums.cpanel.net/f42/auto-create-email-php-146821.html

I think this requires the xmlapi.php which is available here -

http://forums.cpanel.net/f42/xml-api-php-class-version-1-0-a-136449.html

or the tar gz for it at - http://sdk.cpanel.net/lib/xmlapi/php/cp_xmlapi_php_v1.0.5.tar.gz

good luck

Author

Commented:
Hello Slick812
This is link
http://forums.cpanel.net/f42/auto-create-email-php-146821.html
is my question and I post it there but the code is not working with me
You gave me a web page reference for your "code is not working with me". this page has more than one code example, so I have no idea what code you tried!, AS I have said, I did NOT get to use this cpanel API, because when I contacted my web host support, I was told that they had set the individual user access to cpanel API as "READ ONLY" or something to that effect, so I was not interested any longer. I did get the xmlapi.php API function access class tar gz for it at - http://sdk.cpanel.net/lib/xmlapi/php/cp_xmlapi_php_v1.0.5.tar.gz. In this there is the xmlapi.php file. And important for you, there is the api1_example.php in the tar gz, which gives an example for what you are asking about in this question. I have NOT used the code below, I only copy it here from the api1_example.php file. I you try the code below, it should output a json string this debug information from the print function of $xmlapi->api1_query() call. I do not have experience in dealing with this (to me) complex xmlapi class api. So I do NOT know how to debug this class. . . . This seems to me something only an advanced php user would try to use. And someone who could take the time to research and read much of the documentation at -

http://docs.cpanel.net/twiki/bin/view/DeveloperResources/ApiBasics/
http://etwiki.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/XmlApi

and the other method pages for the XML and JSON function call documentation

Also, I am not sure, but I guess this may require  the cpanel access at port 2087, which not all web host provide? ? to complex for me to figure out?
include("xmlapi.php");

$ip = "127.0.0.1"; // I think this can also be your DNS like "mydomain.com"
$root_pass = "testing";// This is your password you use to access your cpanel at "www.mydomain.com:2082"

$account = "cptest";// account name as main account, or addon, subdomain etc, 
     //likely the user name you use to access your cpanel at "www.mydomain.com:2082"

$email_user = "somerandomuser";// New User Name
$email_password = "xxxxxxxxxx"; // new password
$email_domain = "somedomain.com";


$xmlapi = new xmlapi($ip); // this defaults to port "2087", if you do not have that port availible 
    // you may have to call  $xmlapi->set_port('2083');
$xmlapi->password_auth("root",$root_pass);
$xmlapi->set_output('json');

$xmlapi->set_debug(1);// for testing use the debug
print $xmlapi->api1_query($account, "Email", "addpop", array($email_user, $email_password, $email_quota, $email_domain) );

Open in new window

so sorry, I just noticed a mistake in their code, they DO NOT define the $email_quota variable, so Please add -

$email_quota = '100';

or whatever amount you want for the email quota

Author

Commented:
Hello Slick812

I will check your code

Thanks