2013-07-25 101 views
3

我正在嘗試使用PHP創建電子郵件。使用PHP創建電子郵件帳戶

這是我的代碼到目前爲止,它是非常基本的,直到我可以得到一個工作腳本。這是我得到的最接近,但它表示,它已經增加了電子郵件雖然在的cPanel電子郵件不存在,因此它顯然沒有:)

請注意,該代碼中的下列信息已出於安全原因,編輯(例如,不是真實的密碼,用戶名或域名)。

這是我發現的,並一直在努力制定出代碼..

<?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:[email protected]$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> 

預先感謝您:)

安德魯

+0

實際上是什麼問題? –

+0

有沒有更好的方法來做到這一點,實際工作? - 無需拆開一些開源客戶端即可實現此功能。 – Andrew

+0

這是否必須通過cpanel? (可能有幫助:http://stackoverflow.com/questions/6422200/how-to-create-an-email-account-in-cpanel-via-php) – 2013-07-25 23:15:16

回答

1

我認爲這是你在找什麼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); 
4

有一個cPanel的xml api,以及一些php類來提出請求。許多人有服務器配置,使fopen有問題,但xmlapi.php文件適用於許多不同的Web主機。

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 

api中的電子郵件功能的完整列表可以在這裏找到。 https://documentation.cpanel.net/display/SDK/cPanel+API+2+-+Email

相關問題