Add Users and Aliases from CSV

This section contains scripts that hMailServer has contributed with. hMailServer 4 is needed to use these.
Post Reply
JasonMcFeetors
New user
New user
Posts: 4
Joined: 2005-06-23 20:46

Add Users and Aliases from CSV

Post by JasonMcFeetors » 2005-09-21 18:34

While I was setting up my new mail server, I wrote this script to create the users and aliases. You simply put all of the users and the aliases into a CSV file and run the script. Once it finishes, all of your users and aliases should be created for you.

Code: Select all

Option Explicit

Dim obBaseApp
Dim objFSO
Dim objTextFile
Dim strNewAlias,i

Const ForReading = 1
 
Set obBaseApp = CreateObject("hMailServer.Application") 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("Objects.csv", ForReading) 'Change the name of Objects.csv to the name of your CSV file that you put in the same directory as the script.

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
    
    i = i + 1
Loop

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
Make sure that the CSV file you create is in the same directory as the sccript otherwise the script will fail. Also, the CSV file needs to be in the following format:

Code: Select all

EntryType,Field1,Field2
The EntryType can be one of two options, User or Alias. If the EntryType is User, then:

Field1 - Username
Field2 - Password
Field3 - DomainName

If the EntryType is Alias, then:

Field1 - AliasName
Field2 - ForwardingEmail
Field3 - DomainName

So, for example, if you had the following CSV file:

Code: Select all

User,tjones,mypassword,jones.com
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.

Hope you find this to be helpful.

lusitano
New user
New user
Posts: 9
Joined: 2005-12-16 16:31

Post by lusitano » 2005-12-20 15:40

Hello,

How to use this script??

Thank you :oops:

hodown
New user
New user
Posts: 10
Joined: 2006-01-19 01:39
Contact:

Post by hodown » 2006-01-19 01:44

to use the script copy the code and save it as import.vbs. You then need to create the Objects.csv as explained above.

does anyone have a version of this script that adds domains in on the fly also? This script doesn't cater for this.

User avatar
pjcarmen
New user
New user
Posts: 9
Joined: 2004-11-16 23:10
Location: USA

Post by pjcarmen » 2006-01-27 17:51

Great work, thanks! You saved quite a bit of time today. I had to add 60 users as well as an alias for each. Granted it's not that many users, but I tend to "fat finger" :shock: things, so this was a huge help.

It worked perfectly, once I realized that "alias" had to be "Alias", I guess it expects capitalization.

Any way to add to or modify this to help to create distribution lists?

-PJC

Jimi_l
Normal user
Normal user
Posts: 88
Joined: 2005-12-31 12:57
Location: USA

Post by Jimi_l » 2006-02-19 01:19

What if it needed to be modified to import a CSV into the distribution list?

Apparently there are also three fields in that DB. The first is a numerical id (1,2,3, etc) the second field all have a 5 and the third is the actual E-mail address that would be imported from a CSV text file.

If this could work it would really save my bacon.

Jim

hodown
New user
New user
Posts: 10
Joined: 2006-01-19 01:39
Contact:

Post by hodown » 2006-04-06 21:29

hodown wrote:to use the script copy the code and save it as import.vbs. You then need to create the Objects.csv as explained above.

does anyone have a version of this script that adds domains in on the fly also? This script doesn't cater for this.
This script adds domains to hmailserver. Usage is: scriptname.vbs domainname.com

Therefore if you copied the following code into a file and called it 'scriptname.vbs', executed the script from the command line as above usage it would add a domain called 'domainname.com' to hmailserver:

Code: Select all

Set oArgs = WScript.Arguments 
If oArgs.count <> 1 Then 
   WScript.Quit 
End If 

Dim obBaseApp 
Set obBaseApp = CreateObject("hMailServer.Application") 

Dim obNewDomain 
Set obNewDomain = obBaseApp.Domains.Add() 

obNewDomain.Name = oArgs(0) 
obNewDomain.Active = True 
obNewDomain.Save() 

obBaseApp.Domains.Refresh()

mputnam
New user
New user
Posts: 2
Joined: 2006-07-19 14:27

csv for distribution lists

Post by mputnam » 2006-07-19 14:39

Jimi_l wrote:What if it needed to be modified to import a CSV into the distribution list?

Apparently there are also three fields in that DB. The first is a numerical id (1,2,3, etc) the second field all have a 5 and the third is the actual E-mail address that would be imported from a CSV text file.

If this could work it would really save my bacon.

Jim

Hello All,
I understand in theory what the script is doing, but being a novice in this sort of thing, am a bit afraid to attempt to modify and possibly "muck up" the dbase. Can someone help me with the changing the script to upload a csv file to the dbase, for a distribution list. I have a hefty list to move over and would rather not manage with manual inserts to the dbase. :wink:
Thanx in advance
Marie

CraigHarris
Senior user
Senior user
Posts: 886
Joined: 2005-11-28 11:43

Re: csv for distribution lists

Post by CraigHarris » 2006-07-19 23:30

Hello All,
I understand in theory what the script is doing, but being a novice in this sort of thing, am a bit afraid to attempt to modify and possibly "muck up" the dbase. Can someone help me with the changing the script to upload a csv file to the dbase, for a distribution list. I have a hefty list to move over and would rather not manage with manual inserts to the dbase. :wink:
Thanx in advance
Try

Code: Select all

<?php
  //Note: This code has not been properly tested and is provided without warranties of any kind - use at your own risk.
  //Assumes perfect settings & file format as there is no proper error checking.

  $Filename = 'W:\Emails.csv';
  $FileType = 'csv'; // 'csv' expects comma's between fields, and new lines between records | ',' means comma seperated addresses with no new lines.
  $EmailField = 0; //0=first field, 1=2nd field ... (only applies if $FileType is csv)
  $DistributionList = 'List@Example.com';

  $DomainName = substr($DistributionList, strpos($DistributionList, '@')+1);

  $hMail = new COM('hMailServer.Application');
  $hMail->Connect();
  $Domain = $hMail->Domains->ItemByName($DomainName);
  $DistLists = $Domain->DistributionLists();
  $DistList = $DistLists->ItemByAddress($DistributionList);
  $Recipients = $DistList->Recipients();

  function AddEmailToList($EmailAddress) {
    global $Recipients;
    $Recipient = $Recipients->Add();
    $Recipient->RecipientAddress = trim($EmailAddress, ' ,');
    $Recipient->Save();
  }

  $LinesRead = 0;
  $EmailCount = 0;

  $fh = fopen($Filename, 'r');
  if(!$fh) {
    die("Cannot open '{$Filename}'");
  }
  if($FileType == ',') {
    $Addresses = explode(',', file_get_contents($Filename));
    while(list(,$Address) = each($Addresses)) {
      $EmailCount++;
      AddEmailToList($Address);
    }
  } elseif($FileType == 'csv') {
    while(($Line = fgetcsv($fh, 1200, ',')) !== false) {
      $LinesRead++;
      if($Email = strpos($Line[$EmailField], '@') !== false) {
        $EmailCount++;
        AddEmailToList($Line[$EmailField]);
      }
    }
    $ListMembers = $Recipients->Count();
  } else {
    die('Upsupported file type');
  }
  echo <<< EOR
<html>
  <head>
    <title>hMail Distribution List: load report</title>
  </head>
  <body>
    <ul>
      <li>Lines: {$LinesRead}</li>
      <li>Email Addresses: {$EmailCount}</li>
      <li>List members: {$ListMembers}</li>
    </ul>
  </body>
</html>
EOR;
?>
This only makes changes to the specified distribution list - nothing else will be affected.
Windows Server 2003 Std ::: hMailServer 4.3 B248 ::: 99% of email rejected as spam ;)

DJP
Normal user
Normal user
Posts: 52
Joined: 2006-06-28 16:26

Post by DJP » 2006-08-07 04:02

Just by copied the previous vbs and make transformed it to handle creation of distribution list. I made this very quickly so there probably are errors when distribution list already exist, etc... but worked for me since i was in a hurry to add about 200 mails in 5 distributions list and worked well :P

File format is :
Domain,yourdomain.com,distributionlistname
Mail,myuser1@domain.com
Mail,myuser2@domain.com
Mail,myuser3@domain.com
Domain,yourotherdomain.com,distributionlistname2
Mail,myuser1@domain.com
Mail,myuser2@domain.com
Mail,myuser3@domain.com

Code: Select all

Option Explicit

Dim obBaseApp
Dim objFSO
Dim objTextFile
Dim strNewAlias,i
Dim strDomain, strList

Const ForReading = 1
 
Set obBaseApp = CreateObject("hMailServer.Application")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("mylist.csv", ForReading) 'Change the name of Objects.csv to the name of your CSV file that you put in the same directory as the script.

Do While objTextFile.AtEndOfStream <> True
    strNewAlias = split(objTextFile.Readline, ",")

    Select Case strNewAlias(0)
       Case "Domain"
          strDomain = strNewAlias(1)
          strList = strNewAlias(2)
          AddListe strList, strDomain
       Case "Mail"
          AddMail strNewAlias(1), strList, strDomain
    End Select
   
    i = i + 1
Loop

Sub AddListe(strList, strDomain)
   Dim obDomain
   Dim obDistributionList
   
   Set obDomain = obBaseApp.Domains.ItemByName(strDomain)
   Set obDistributionList = obDomain.DistributionLists.Add()
   
   obDistributionList.Address = strList & "@" & strDomain 
   obDistributionList.Active = True
   obDistributionList.Save()
   
End Sub

Sub AddMail(strEmailAddress, strList, strDomain)
   Dim obDomain
   Dim obDistributionLists
   Dim obDistributionList
   Dim obNewMail
   Dim obRecipients

   Set obDomain = obBaseApp.Domains.ItemByName(strDomain)
   Set obDistributionLists = obDomain.DistributionLists
   Set obDistributionList = obDistributionLists.ItemByAddress(strList & "@" & strDomain)
   Set obRecipients = obDistributionList.Recipients
   Set obNewMail = obRecipients.Add()
   
   obNewMail.RecipientAddress = strEmailAddress
   obNewMail.Save() 'saves account
   
   Set obNewMail = Nothing
   Set obDomain = Nothing
   Set obDistributionLists = Nothing   
   Set obDistributionList = Nothing
   Set obRecipients = Nothing
   
End Sub

mputnam
New user
New user
Posts: 2
Joined: 2006-07-19 14:27

Post by mputnam » 2006-08-23 19:28

DJP wrote:Just by copied the previous vbs and make transformed it to handle creation of distribution list. I made this very quickly so there probably are errors when distribution list already exist, etc... but worked for me since i was in a hurry to add about 200 mails in 5 distributions list and worked well :P

File format is :
Domain,yourdomain.com,distributionlistname
Mail,myuser1@domain.com
Mail,myuser2@domain.com
Mail,myuser3@domain.com
Domain,yourotherdomain.com,distributionlistname2
Mail,myuser1@domain.com
Mail,myuser2@domain.com
Mail,myuser3@domain.com

Code: Select all

Option Explicit

Dim obBaseApp
Dim objFSO
Dim objTextFile
Dim strNewAlias,i
Dim strDomain, strList

Const ForReading = 1
 
Set obBaseApp = CreateObject("hMailServer.Application")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("mylist.csv", ForReading) 'Change the name of Objects.csv to the name of your CSV file that you put in the same directory as the script.

Do While objTextFile.AtEndOfStream <> True
    strNewAlias = split(objTextFile.Readline, ",")

    Select Case strNewAlias(0)
       Case "Domain"
          strDomain = strNewAlias(1)
          strList = strNewAlias(2)
          AddListe strList, strDomain
       Case "Mail"
          AddMail strNewAlias(1), strList, strDomain
    End Select
   
    i = i + 1
Loop

Sub AddListe(strList, strDomain)
   Dim obDomain
   Dim obDistributionList
   
   Set obDomain = obBaseApp.Domains.ItemByName(strDomain)
   Set obDistributionList = obDomain.DistributionLists.Add()
   
   obDistributionList.Address = strList & "@" & strDomain 
   obDistributionList.Active = True
   obDistributionList.Save()
   
End Sub

Sub AddMail(strEmailAddress, strList, strDomain)
   Dim obDomain
   Dim obDistributionLists
   Dim obDistributionList
   Dim obNewMail
   Dim obRecipients

   Set obDomain = obBaseApp.Domains.ItemByName(strDomain)
   Set obDistributionLists = obDomain.DistributionLists
   Set obDistributionList = obDistributionLists.ItemByAddress(strList & "@" & strDomain)
   Set obRecipients = obDistributionList.Recipients
   Set obNewMail = obRecipients.Add()
   
   obNewMail.RecipientAddress = strEmailAddress
   obNewMail.Save() 'saves account
   
   Set obNewMail = Nothing
   Set obDomain = Nothing
   Set obDistributionLists = Nothing   
   Set obDistributionList = Nothing
   Set obRecipients = Nothing
   
End Sub
This could be moved to HOW TO
Adding Distribution Lists for Dummies
1) Copy and paste the code into notepad. name the file Dlist.vbs. Place this file on your mailserver in the directory /hmailserver/dbscripts

2) create your file format as shown;
REMEMBER: the first word of each line is critical to the script. Domain (first letter uppercase) will create a new distribution list for that domain. Mail (first letter uppercase) will create a new member address in that domain. You can create multiple distribution lists in one file.

3) After creating the distribution list open it in Hmail and configure the options that allow it to function.

craig@ccilegal.co.uk
New user
New user
Posts: 1
Joined: 2006-09-06 15:01

Add addresses to Routes - script

Post by craig@ccilegal.co.uk » 2006-09-06 15:08

Hi
Anyone got a script to add addresses to the Routes "deliver to these addresses below"
Craig
PS thanks for the import.vbs script

SFM
New user
New user
Posts: 13
Joined: 2006-12-01 17:04

Script broken in new version?

Post by SFM » 2006-12-01 20:10

I am using hmailserver 4.3 and this script dosn't seem to work.

I saw in the forum where another script had to be updated to work in 4.3.

User avatar
martin
Developer
Developer
Posts: 6834
Joined: 2003-11-21 01:09
Location: Sweden
Contact:

Post by martin » 2006-12-01 20:12

Correct. After the line:
Set obBaseApp = CreateObject("hMailServer.Application")
You need to add:
Call obBaseApp.Authenticate("Administrator","your-hmailserver-password")

Otherwise the script won't have access to do what it's supposed to do.

SFM
New user
New user
Posts: 13
Joined: 2006-12-01 17:04

Post by SFM » 2006-12-01 20:15

Martin,

Thanks for the reply and developing a great mail server.

barcajunior
New user
New user
Posts: 1
Joined: 2008-01-05 17:10
Contact:

Post by barcajunior » 2008-01-05 17:14

It did save many time for me.Thank you very much.

powerful green laser pointers
Last edited by barcajunior on 2009-11-18 15:08, edited 2 times in total.

Red_Scorpion
New user
New user
Posts: 6
Joined: 2008-01-09 18:33

Post by Red_Scorpion » 2008-01-10 15:46

Hello Everybody,
I want to add 1500 person's email to distribition list.when I run Script I got this error,

script : c:\programfiles\hMailserver\DBScripts\Dlist.vbs
line :35
Char :4
Error :Subscript out of range
code :800A0009
Source :microsoft VBSript runtime error

how can I do? I need help quickly...
Bilgehan

Red_Scorpion
New user
New user
Posts: 6
Joined: 2008-01-09 18:33

Post by Red_Scorpion » 2008-01-10 17:36

HHOOOAAAREEEEEYYYYY....
it is working...
I'm so happy,
so many thanks everyone...
Red_Scorpion

plobby
Normal user
Normal user
Posts: 115
Joined: 2008-01-29 07:04

Re: Add Users and Aliases from CSV

Post by plobby » 2008-09-18 06:06

Can anyone help me out with getting this working with the current hmailserver?

Basically I need a line in there or something so it can authenticate...

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: Add Users and Aliases from CSV

Post by ^DooM^ » 2008-09-18 10:05

This should do what you need. Find the first line, add the second line.

Code: Select all

Set obBaseApp = CreateObject("hMailServer.Application")
Call obBaseApp.Authenticate ("Administrator", "YourHmailPassword")
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

plobby
Normal user
Normal user
Posts: 115
Joined: 2008-01-29 07:04

Re: Add Users and Aliases from CSV

Post by plobby » 2008-09-18 14:18

Thank you very much Doom -- I will try this later.

plobby
Normal user
Normal user
Posts: 115
Joined: 2008-01-29 07:04

Re: Add Users and Aliases from CSV

Post by plobby » 2008-09-19 02:07

I keep getting the same error -- would it make a difference if I am running an external MySQL database?

Script: C:\Program Files\hMailServer\DBScripts\Dlist.vbs

Line: 35

Char: 4

Error: "You do not have access to this property / method. Ensure that hMailServer.Application.Authentication() is called with proper login credentials."

Code: 800403E9

Source: hMailServer COM Library

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re:

Post by ^DooM^ » 2008-09-19 02:29

martin wrote:Correct. After the line:
Set obBaseApp = CreateObject("hMailServer.Application")
You need to add:
Call obBaseApp.Authenticate("Administrator","your-hmailserver-password")

Otherwise the script won't have access to do what it's supposed to do.
Martin replied above with the correct code and it looks the same as mine. Are you sure the password is correct?
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

plobby
Normal user
Normal user
Posts: 115
Joined: 2008-01-29 07:04

Re: Add Users and Aliases from CSV

Post by plobby » 2008-09-19 03:01

I somehow ran over that and saw it right after you posted your reply -- Yeah I am positive it is the right password. Am I connecting to hmailserver or the mysql database for hmail?

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: Add Users and Aliases from CSV

Post by ^DooM^ » 2008-09-19 09:10

You are connected to the COM API not the database.
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

plobby
Normal user
Normal user
Posts: 115
Joined: 2008-01-29 07:04

Re: Add Users and Aliases from CSV

Post by plobby » 2008-09-19 14:57

^DooM^ wrote:You are connected to the COM API not the database.
Sorry for all the questions, I just dont feel like entering the mailing list 1 by 1...we have about 200 people on it.

One more, is there a way to test out the password besides through a script or change it? I am positive it is the right password but just to try something different.

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: Add Users and Aliases from CSV

Post by ^DooM^ » 2008-09-19 19:09

Easy way to test it is to change the hmail admin password temporarily.

Goto the hMailServer.ini file and find AdministratorPassword. Copy and paste the string of random letters and numbers into another text file. Replace that string with this one

cc03e747a6afbbcbf8be7668acfebee5

Restart the hMail service from windows.

Test your script using the following password test123

If it works with this new password then you have misremembered your old password that you were using in the script. Replace the old code, save it and restart the service again to go back to the original password.

Read this to change the admin password to something more secure if you have forgotten your original password.

http://www.hmailserver.com/documentatio ... r_password
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

plobby
Normal user
Normal user
Posts: 115
Joined: 2008-01-29 07:04

Re: Add Users and Aliases from CSV

Post by plobby » 2008-09-20 01:51

Thanks for your help yet again, and patience. I guess I wasn't so positive on it being the correct password.

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: Add Users and Aliases from CSV

Post by ^DooM^ » 2008-09-20 02:12

Did you sort it out then mate?
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

plobby
Normal user
Normal user
Posts: 115
Joined: 2008-01-29 07:04

Re: Add Users and Aliases from CSV

Post by plobby » 2008-09-20 02:20

Yes I did, thanks a lot!

desiredforsome
New user
New user
Posts: 7
Joined: 2009-02-06 04:43

Re: Add Users and Aliases from CSV

Post by desiredforsome » 2009-02-06 04:47

Hey i have 5.0 Does this script work wth that, When i run it it gives me.

Error

line:52
Char:4
Error: You do not have access to this property / method. Ensure that hmailServer.Application.Authticate() is called with proper login cridentals.

What do i do. Im trying to basicaly make a free user signup for email onlien, ANd i figured i can just have them fill out a form that converts it to .csv and then every 5 seconds this script runs on that .csv

Thanks

gaganuiit
New user
New user
Posts: 28
Joined: 2009-06-20 12:34

Re: Add Users and Aliases from CSV

Post by gaganuiit » 2009-07-03 15:49

Well i have succeeded with the script.
But i would really want to have one little enhancement be done.
When i am trying to add more members to an existing distribution list, i am getting this error

Failed to save object. Another object with the same name already exists in this domain.
This is because the same distribution list already exists.
Suggestions please.
Dlisterror.jpg

Melibokus73
Normal user
Normal user
Posts: 87
Joined: 2009-06-26 20:13

Re: Add Users and Aliases from CSV

Post by Melibokus73 » 2009-07-24 10:58

Hi ...

Does this original "ADD-Users"script also work with 5.x (5.2) :?:

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: Add Users and Aliases from CSV

Post by ^DooM^ » 2009-07-24 11:23

Try it ;)
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

Melibokus73
Normal user
Normal user
Posts: 87
Joined: 2009-06-26 20:13

Re: Add Users and Aliases from CSV

Post by Melibokus73 » 2009-08-19 19:43

^DooM^ wrote:Try it ;)
It seems to run ... 8)


(after adding the Line with the Password) .... :!:

jujb.cn
New user
New user
Posts: 5
Joined: 2013-05-18 05:35

Re: Add Users and Aliases from CSV

Post by jujb.cn » 2013-05-23 11:14

JasonMcFeetors wrote:While I was setting up my new mail server, I wrote this script to create the users and aliases. You simply put all of the users and the aliases into a CSV file and run the script. Once it finishes, all of your users and aliases should be created for you.

Code: Select all

Option Explicit

Dim obBaseApp
Dim objFSO
Dim objTextFile
Dim strNewAlias,i

Const ForReading = 1
 
Set obBaseApp = CreateObject("hMailServer.Application") 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("Objects.csv", ForReading) 'Change the name of Objects.csv to the name of your CSV file that you put in the same directory as the script.

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
    
    i = i + 1
Loop

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
Make sure that the CSV file you create is in the same directory as the sccript otherwise the script will fail. Also, the CSV file needs to be in the following format:

Code: Select all

EntryType,Field1,Field2
The EntryType can be one of two options, User or Alias. If the EntryType is User, then:

Field1 - Username
Field2 - Password
Field3 - DomainName

If the EntryType is Alias, then:

Field1 - AliasName
Field2 - ForwardingEmail
Field3 - DomainName

So, for example, if you had the following CSV file:

Code: Select all

User,tjones,mypassword,jones.com
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.

Hope you find this to be helpful.

Using the code above, I had made a file of import.vbs, after excuting the import.vbs, screen tip is following:

Script: E:\hMailServer\hMailServer\import.vbs
Line: 52
Char: 4
Error: You do not have access to this property / method. Ensure that hMailServer. Applicaton. Authenticate ( ) is called whth proper login credentials.
Code: 800403E9
Source: hMailServer COM library

I have upload the file of import.vbs.
Would you give me a file of import.vbs without any error?
Thanks!

User avatar
mattg
Moderator
Moderator
Posts: 19979
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Add Users and Aliases from CSV

Post by mattg » 2013-05-23 11:32

This was probably written for an earlier version.

You will need to authenticate.

Add this line after 'set' lines and before the 'do while'

Code: Select all

Call obBaseApp.Authenticate("Administrator", "SecretPassword")    ' change me
Change to your password, and then run.

Post back if that works (or if it doesn't), and I'll update the script as shown in your post.
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

jujb.cn
New user
New user
Posts: 5
Joined: 2013-05-18 05:35

Re: Add Users and Aliases from CSV

Post by jujb.cn » 2013-05-23 17:10

After added the code(Call obBaseApp.Authenticate("Administrator", "SecretPassword") ' change me),and excuting the import.vbs, screen tip is following as:

Script: E:\hMailServer\hMailServer\import.vbs
Line: 62
Char: 4
Error: Failed to save object. Another object with the same name already exists in this domain.
Code: 800403E9
Source: hMailServer COM library

percepts
Senior user
Senior user
Posts: 5282
Joined: 2009-10-20 16:33
Location: Sceptred Isle

Re: Add Users and Aliases from CSV

Post by percepts » 2013-05-23 17:36

you are trying to add an account which already exists in hms.
The script doesn't trap save errors so there is no logging of which account it is. You can either modify script to trap save errors and report which account is already in hms or analyse your input file for duplicates and against what is already in hms.

N.B. if your script failed half way through then half of the accounts in your input file will be now be in hms.

You really need to modify script to check if account or alias already exists before trying to add it. Or modify to trap failed save(and report because fail may not be due to duplicate) and continue processing input file.

jujb.cn
New user
New user
Posts: 5
Joined: 2013-05-18 05:35

Re: Add Users and Aliases from CSV

Post by jujb.cn » 2013-05-23 18:09

Dear Mattg and percepts,

Yours is excellent !

After I deleted accounts which already exist in hMailServer in the objects.csv, I made it.

Thanks!

Jujb.cn

User avatar
mattg
Moderator
Moderator
Posts: 19979
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Add Users and Aliases from CSV

Post by mattg » 2013-05-24 01:03

percepts has created a ver 5 of this script
http://www.hmailserver.com/forum/viewto ... 20&t=24810
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

jr3151006
Normal user
Normal user
Posts: 31
Joined: 2011-02-03 14:57

Re: Add Users and Aliases from CSV

Post by jr3151006 » 2014-12-09 14:53

Can someone tell me if this script also creates the DOMAIN before create users???

percepts
Senior user
Senior user
Posts: 5282
Joined: 2009-10-20 16:33
Location: Sceptred Isle

Re: Add Users and Aliases from CSV

Post by percepts » 2014-12-09 15:13

this version does not. The version I pointed you to does.

Use this one viewtopic.php?f=20&t=24810#p151855

It only adds domains if they are not already there.

Post Reply