如何以编程方式在 hMailServer 上创建用户?

How to create user on hMailServer programatically?

我们正在编写一个专门的 PHP 电子邮件客户端,并希望该客户端的管理员能够在 hMailServer 上创建用户帐户。

我尝试了 imap_createmailbox(...),但它只是在用户的文件夹结构中创建了一个目录,而不是我们想要的 "create a mailbox for a new user"。

hMailServer 是否有某种接口,以便我可以启用我们的 PHP 电子邮件客户端通过代码创建 hMailServer 帐户?

是的,在 hmailserver 中有两个创建帐户的界面。

一、通过数据库,可以选择账号密码哈希类型(0,1,2,3)和签名等经典信息。出于同步原因,我不推荐这种方法,hmail-server 需要缓存时间来考虑数据库更新。

二,我推荐使用API COM,它提供了所有通用语言的所有可能方法。 您必须在 windows 服务器中启用 D-COM。 APIguide

hmailserver 版本:hMailServer 5.6.4 - Build 2283 在 C# 中通过 hmailserver 创建新电子邮件帐户的完整解决方案 如果您已经成功配置hmailserver,那么您可以使用以下步骤在c#

中通过hmailserver创建新帐户
  1. hmailserver 连接

       private Domain HMailServerConnection() {
            var objGlobal = new ApplicationClass();
               objGlobal.Authenticate(ConfigurationManager.AppSettings["HMailUsername"], ConfigurationManager.AppSettings["HMailPassword"]);
            return objGlobal.Domains.get_ItemByName(ConfigurationManager.AppSettings["hMailDomain"]);
        }
    
  2. 创建新电子邮件帐户的功能

    public string AddNewAccount(string email,string password)
    {
    try
    {
        Domain domain = HMailServerConnection();
        Accounts accounts = domain.Accounts;
        Account mailbox = accounts.Add();
        mailbox.Address = email;
        mailbox.Password = password;
        mailbox.Save();
        return "success";
    }
    catch(Exception ex)
    {
        return "error";
    }
    }
    
  3. App.config 或 web.config

    中的应用程序设置
      <appSettings>
        <add key="hMailDomain" value="domainname"/>
        <add key="HMailUsername" value="Username"/>
        <add key="HMailPassword" value="password"/>
      </appSettings>
    

查看 link

它的工作我测试了它。

<?php   
header('Content-Type: text/html; charset=utf-8');
$obBaseApp = new COM("hMailServer.Application", NULL, CP_UTF8);
$obBaseApp->Connect();

$hmail_config['rooturl']            = "http://localhost/";      
$obAccount = $obBaseApp->Authenticate("Administrator", "hMailserver Administrator 
password");

if (!isset($obAccount)) {
      echo "<b>Not authenticated or Administrator's password wrong</b>";
} else {

try {
    echo "<b>Logon COM [OK], now we can add accounts</b>";

    $obDomain   = $obBaseApp->Domains->ItemByDBID(1); 
    $obAccount = $obDomain->Accounts->ItemByDBID(1);  // account id

      $domainname = $obDomain->Name;
       $obAccounts = $obDomain->Accounts();       
       $obAccount = $obDomain->Accounts->Add();
       $newalias = "powerranger";
       $firstname = "Arnold";
       $lastname = "Schwarzenegger";
       $my_domainname ="@mydomain.com";

         $obAccount->PersonFirstName = $firstname;
         $obAccount->PersonLastName = $lastname;
         $obAccount->MaxSize = 102; // 102 MB set inbox space
         $obAccount->Address = $newalias .$my_domainname; 
         $obAccount->Password = "secret"; // provide this in Thunderbird/Outlook ect.
         $obAccount->Active = true; // set account to active
         $obAccount->Save(); // save, finish.

     /* If we reaching this point, everything works as expected */
     echo "<br/><h3> Account was successfully created, now you can login with".
                      "an POP3 or IMAP-Client </h3>";

} 
/* OK, if something went wrong, give us the exact error details */  
catch(Exception $e) {
    echo "<h4>COM-ERROR: <br />".$e->getMessage()."</h4><br />";        
}

}