【问题标题】:Create emails accounts using PHP使用 PHP 创建电子邮件帐户
【发布时间】:2013-07-26 01:26:45
【问题描述】:

我正在尝试使用 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:$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>

提前谢谢你:)

安德鲁

【问题讨论】:

  • 问题到底是什么?
  • 有没有更好的方法来实际工作? - 无需拆开一些开源客户端来实现这一点。
  • 这必须通过cpanel吗? (可能有帮助:stackoverflow.com/questions/6422200/…
  • 没必要,不。我建议使用 cpanel 的原因是因为我不使用任何替代方式来添加它们。但是,与链接帖子相关的内容中,我遇到问题的代码是该线程的建议解决方案。
  • stackoverflow.com/questions/17094468/… 获得以下脚本:)

标签: php cpanel


【解决方案1】:

我想这就是你要找的东西:

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

【讨论】:

    【解决方案2】:

    有一个用于 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-10
      • 2013-06-10
      • 2016-08-27
      • 2022-11-07
      • 2011-08-05
      • 2019-10-16
      • 1970-01-01
      相关资源
      最近更新 更多