This script adds Users and/or Aliases from a CSV file to your hMailServer installation.
This seems to be a frequent requirement so I have copied and updated an old hms 4 script from original
topic http://www.hmailserver.com/forum/viewto ... 13#p149913
The modifications include authorisation for hMailServer 5.x.x and also error trapping so that script will continue processing input file when duplicates are found. A popup summary of duplicates (already in HMS) and counts is produced. Otherwise it's same as original.
** N.B. 1 ** You need to set your password into the Authentication step on line 20 of following code
** N.B. 2 ** You need to set your CSV filename on line 22 of following code with correct path of where it is.
Code: Select all
Option Explicit
On Error resume next
Dim obBaseApp
Dim objFSO
Dim objTextFile
Dim strNewAlias,i
Dim scrreport
Dim failed
Dim added
failed = 0
added = 0
Const ForReading = 1
Set obBaseApp = CreateObject("hMailServer.Application")
Call obBaseApp.Authenticate("Administrator","password_here") '*** N.B. 1. set your administrator password in this line
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("AddAccounts.txt", ForReading) 'N.B. 2. Set your CSV path/filename in this line
Do While objTextFile.AtEndOfStream <> True
strNewAlias = split(objTextFile.Readline, ",")
Select Case strNewAlias(0)
Case "User"
AddUser strNewAlias(1), strNewAlias(2), strNewAlias(3)
Case "Alias"
AddAlias strNewAlias(1), strNewAlias(2), strNewAlias(3)
End Select
If err.Number <> 0 Then 'error handling:
scrreport = scrreport & "Failed add (probably duplicate) : " & strNewAlias(0) & "," & strNewAlias(1) & "," & strNewAlias(2) & "," & strNewAlias(3) & VBNewLine
failed = failed + 1
err.Clear
Else
added = added + 1
End If
i = i + 1
Loop
scrreport = scrreport & " " & VBNewLine
scrreport = scrreport & "Added = " & added & VBNewLine
scrreport = scrreport & "Failed or Duplicates = " & failed & VBNewLine
wscript.echo scrreport
Sub AddAlias(strAlias,strEmailAddress,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)
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.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
The CSV file needs to be in the following format:
The EntryType can be one of two options, User or Alias. If the EntryType is User, then:EntryType,Field1,Field2,Field3
EntryType = User
Field1 = Uername
Field2 = Password
Field3 = DqmainName
If the EntryType is Alias, then:
EntryType = Alias
Field1 = AliasName
Field2 = ForwardingEmail
Field3 = DomainName
So, for example, if you had the following CSV file:
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.User,tjones,mypassword,jones.com
Alias,tommy,tjones@jones.com,jones.com
Good Luck !