hMailServer中文论坛 QQ群:80049760 SSL在线生成 https://www.sssssssss.com/ 临时邮箱 https://www.linshiyouxiang.com

hMailServer批量添加用户的方法

与hMailServer脚本相关,戳此进入。
回复
头像
Hsia
网站管理员
网站管理员
帖子: 336
注册: 2014年11月26日, 12:41
地址: 上海
联系:

hMailServer批量添加用户的方法

帖子 Hsia »

用脚本来批量添加用户,操作简单。
下载文件,要修改的地方如下:
import.vbs 修改第44行,这是登陆hmailserver的密码。

bulk.csv 这是用户的数据。格式:User,tjones,mypassword,jones.com,Tommy,Jones
参数:user:指用户。依次是用户、用户名、密码、域名、姓名。姓和名是要隔开的。
例如:user,abc,123456,xxx.com,zhang,san
设置空间大小修改下面这个0 ,此时的0表示无限制。1024=1M

代码: 全选

obNewAccount.Maxsize = 0 'sets mailbox size, 0=unlimited
然后运行脚本。
重要说明:下载文件后,不管你放在哪里,这两个文件必须放在同一目录。
批量添加用户.rar
(1.69 KiB) 下载 942 次


----------------------------------------------------分割线------------------------------------------------------------------


我把完整的import.vbs脚本贴出来:

代码: 全选

'#####the script#####
'# The EntryType can be one of two options, User or Alias. If the EntryType is User, then:
'#
'# EntryType = User
'# Field1 = Uername
'# Field2 = Password
'# Field3 = DqmainName
'#
'# If the EntryType is Alias, then:
'# EntryType = Alias
'# Field1 = AliasName
'# Field2 = ForwardingEmail
'# Field3 = DomainName
'#
'# To add USERS, for example, your CSV file should have the structured information (type, strUsername, strPassword, strDomain, strPersonFirstName, strPersonLastName), like below:
'# User,tjones,mypassword,jones.com,Tommy,Jones
'#
'# To add ALIAS, for example, your CSV file should have the structured information (type, strAlias, strAliasUsername, strDomain), like below:
'# Alias,tommy,tjones@jones.com,jones.com
'#
'# This would create a user names tjones@jones.com in the jones.com domain and an alias tommy@jones.com which will forward all e-mail to tjones@jones.com in the jones.com domain.
'####################

Option Explicit
Dim Elog
Dim obBaseApp
Dim objFSO
Dim objTextFile
Dim strNewAlias,i

Dim failed
Dim added
Dim domainsAdded

failed = 0
added = 0
domainsAdded = 0

'#####################################################################
'# Custom Variables regarding HMAIL admin password and file to be read
'#####################################################################
Dim hAdminpwd
Dim CSVFile
hAdminpwd = "888888"
CSVFile = "bulk.csv"

Set Elog = CreateObject("hMailServer.EventLog")
Set obBaseApp = CreateObject("hMailServer.Application") 

Call obBaseApp.Authenticate("Administrator",hAdminpwd) '*** N.B. 1. set your administrator password in this line 

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(CSVFile, ForReading) 'N.B. 2. Set your CSV path/filename in this line
On Error resume next
Do While objTextFile.AtEndOfStream <> True
    strNewAlias = split(objTextFile.Readline, ",")

    Select Case strNewAlias(0)
       Case "User"
          AddUser strNewAlias(1), strNewAlias(2), strNewAlias(3),strNewAlias(4),strNewAlias(5)
       Case "Alias"
          AddAlias strNewAlias(1), strNewAlias(2), strNewAlias(3)
    End Select
    If err.Number <> 0 Then    'error handling:
     Elog.Write("Failed add (probably duplicate)" & Chr(34) & vbTab & Chr(34) & strNewAlias(0) _
     & "," & strNewAlias(1) & "," & strNewAlias(2) & "," & strNewAlias(3))
     failed = failed + 1
     err.Clear
    Else
     added = added + 1
    End If
Loop
On Error goto 0
Elog.Write("Domains Added Sucessfully    = " & domainsAdded)
Elog.Write("Accounts Added Sucessfully   = " & added)
Elog.Write("Failed or Duplicate Accounts = " & failed)
Wscript.Echo ("Domains Added Sucessfully    = " & domainsAdded & VbNewLine _
            & "Accounts Added Sucessfully   = " & added & VbNewLine _
            & "Failed or Duplicate Accounts  = " & failed & VbNewLine & VbNewLine _
            & "See hmailserver_events.log for list of duplicates (if any)")

'###################
'# Usefull functions
'###################
Sub AddAlias(strAlias,strEmailAddress,strDomain)

   AddDomain strDomain

   Dim obDomain 
   Dim obAliases 
   Dim obNewAlias

   Set obDomain = obBaseApp.Domains.ItemByName(strDomain) 
   Set obAliases = obDomain.Aliases
   Set obNewAlias = obAliases.Add() 
   
   obNewAlias.Name = strAlias & "@" & strDomain 'username
   obNewAlias.Value = strEmailAddress 'password
   obNewAlias.Active = 1 'activates user
   obNewAlias.Save() 'saves account
   
   Set obNewAlias = Nothing
   Set obAliases = Nothing
   Set obDomain = Nothing
   
End Sub

Sub AddUser(strUsername, strPassword, strDomain, strPersonFirstName, strPersonLastName)
   
   AddDomain strDomain
   
   Dim obDomain 
   Dim obAccounts 
   Dim obNewAccount

   Set obDomain = obBaseApp.Domains.ItemByName(strDomain) 
   Set obAccounts = obDomain.Accounts
   Set obNewAccount = obAccounts.Add() 
   
   obNewAccount.Address = strUsername & "@" & strDomain 'username
   obNewAccount.Password = strPassword 'password
   obNewAccount.PersonFirstName = strPersonFirstName
   obNewAccount.PersonLastName = strPersonLastName
   obNewAccount.Active = 1 'activates user
   obNewAccount.Maxsize = 0 'sets mailbox size, 0=unlimited
   obNewAccount.Save() 'saves account
  
   Set obNewAccount = Nothing
   Set obDomain = Nothing   
   Set obAccounts = Nothing
  
End Sub

Sub AddDomain(strDomain)
   Dim obNewDomain
   Set obNewDomain = obBaseApp.Domains.Add()
   obNewDomain.Name = strDomain
   obNewDomain.Active = True

   On Error resume next
   obNewDomain.Save()
   If err.Number = 0 Then
    domainsAdded = domainsAdded + 1
   End If 
   err.Clear
 
   obBaseApp.Domains.Refresh()
End Sub
bulk.csv 的格式是:User,tjones,mypassword,jones.com,Tommy,Jones
参数:user:指用户。依次是用户、用户名、密码、域名、姓名。姓和名是要隔开的。
jasonshaw
Level 3
Level 3
帖子: 34
注册: 2015年5月6日, 11:16

Re: hMailServer批量添加用户的方法

帖子 jasonshaw »

这个很好,学习试试怎么弄
errai
Level 1
Level 1
帖子: 3
注册: 2015年11月24日, 19:50

Re: hMailServer批量添加用户的方法

帖子 errai »

按帖子的提示设置了csv文件和修改了import.vbs的hmail管理密码,导入过程顺利,提示已经导入,结果到hmail管理程序进去一看,啥都么有!!!

LOGS里有如下几行:

5776 "2015-11-25 21:16:02.444" "Domains Added Sucessfully = 0"
5776 "2015-11-25 21:16:02.446" "Accounts Added Sucessfully = 88"
5776 "2015-11-25 21:16:02.447" "Failed or Duplicate Accounts = 0"

提示已经导入了88个账户,结果都么有,晕倒,也没错误日志,不知道从何查起了!
guiltysong
Level 1
Level 1
帖子: 1
注册: 2016年12月2日, 11:09

Re: hMailServer批量添加用户的方法

帖子 guiltysong »

感谢,导入成功了!
tutututuqd
Level 1
Level 1
帖子: 1
注册: 2017年6月29日, 13:23

Re: hMailServer批量添加用户的方法

帖子 tutututuqd »

我也出现了同样的错误,添加成功,但是看不到新增的账号。这是什么原因呢?
AFEI
Level 1
Level 1
帖子: 2
注册: 2016年6月14日, 10:39

Re: hMailServer批量添加用户的方法

帖子 AFEI »

我也是提示导入成功,但是数据库里没对应的账号信息!
jujb.cn
Level 1
Level 1
帖子: 1
注册: 2017年9月20日, 19:06

Re: hMailServer批量添加用户的方法

帖子 jujb.cn »

直接在提供下载的bulk上制作用户名单;重启后即可见已成功导入。向为此付出劳动的人们,致敬!
yonglemcse
Level 1
Level 1
帖子: 1
注册: 2018年5月4日, 16:42

Re: hMailServer批量添加用户的方法

帖子 yonglemcse »

域名空白没有新建时,即可批量导入,如果已经新建了域名,导入会出现失败!
回复