Add Users and Aliases from CSV

http://www.hmailserver.com/forum/viewtopic.php?f=14&t=2634


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:
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:
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:
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.


Top
  Profile  
 
  Post subject:
Post Posted: 2005-12-20 15:40 
New user
New user

Joined: 2005-12-16 16:31
Posts: 9
Hello, 

How to use this script?? 

Thank you  :oops:


Top
  Profile  
 
  Post subject:
Post Posted: 2006-01-19 01:44 
New user
New user

Joined: 2006-01-19 01:39
Posts: 10
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.


Top
  Profile  
 
  Post subject:
Post Posted: 2006-01-27 17:51 
New user
New user
User avatar

Joined: 2004-11-16 23:10
Posts: 9
Location: USA
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


Top
  Profile  
 
  Post subject:
Post Posted: 2006-02-19 01:19 
Normal user

Joined: 2005-12-31 12:57
Posts: 88
Location: USA
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


Top
  Profile  
 
  Post subject:
Post Posted: 2006-04-06 21:29 
New user
New user

Joined: 2006-01-19 01:39
Posts: 10
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:
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()


Top
  Profile  
 
  Post subject: csv for distribution lists
Post Posted: 2006-07-19 14:39 
New user
New user

Joined: 2006-07-19 14:27
Posts: 2
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
 

Top
  Profile  
 
  Post subject: Re: csv for distribution lists
Post Posted: 2006-07-19 23:30 
Senior user
Senior user

Joined: 2005-11-28 11:43
Posts: 886
Quote:
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:
<?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 ;)
 

Top
  Profile  
 
  Post subject:
Post Posted: 2006-08-07 04:02 
Normal user

Joined: 2006-06-28 16:26
Posts: 52
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 : 
Quote:
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:
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
Quote:


Top
  Profile  
 
  Post subject:
Post Posted: 2006-08-23 19:28 
New user
New user

Joined: 2006-07-19 14:27
Posts: 2
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 :
Quote:
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:
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
Quote:
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.


Top
  Profile  
 
  Post subject: Add addresses to Routes - script
Post Posted: 2006-09-06 15:08 
New user
New user

Joined: 2006-09-06 15:01
Posts: 1
Hi 
Anyone got a script to add addresses to the Routes "deliver to these addresses below" 
Craig 
PS thanks for the import.vbs script


Top
  Profile  
 
  Post subject: Script broken in new version?
Post Posted: 2006-12-01 20:10 
New user
New user

Joined: 2006-12-01 17:04
Posts: 13
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.


Top
  Profile  
 
  Post subject:
Post Posted: 2006-12-01 20:12 
Developer

Joined: 2003-11-21 01:09
Posts: 6395
Location: Sweden
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.


Top
  Profile  
 
  Post subject:
Post Posted: 2006-12-01 20:15 
New user
New user

Joined: 2006-12-01 17:04
Posts: 13
Martin, 

Thanks for the reply and developing a great mail server.


Top
  Profile  
 
  Post subject:
Post Posted: 2008-01-05 17:14 
New user
New user

Joined: 2008-01-05 17:10
Posts: 1
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. 

Top
  Profile  
 
  Post subject:
Post Posted: 2008-01-10 15:46 
New user
New user

Joined: 2008-01-09 18:33
Posts: 6
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


Top
  Profile  
 
  Post subject:
Post Posted: 2008-01-10 17:36 
New user
New user

Joined: 2008-01-09 18:33
Posts: 6
HHOOOAAAREEEEEYYYYY.... 
it is working... 
I'm so happy, 
so many thanks everyone... 
Red_Scorpion


Top
  Profile  
 
  Post subject: Re: Add Users and Aliases from CSV
Post Posted: 2008-09-18 06:06 
Normal user

Joined: 2008-01-29 07:04
Posts: 115
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...


Top
  Profile  
 
  Post subject: Re: Add Users and Aliases from CSV
Post Posted: 2008-09-18 10:05 
Site Admin
User avatar

Joined: 2005-07-29 16:18
Posts: 13675
Location: UK
This should do what you need. Find the first line, add the second line.
Code:
Set obBaseApp = CreateObject("hMailServer.Application")
Call obBaseApp.Authenticate ("Administrator", "YourHmailPassword")

_________________
If at first you don't succeed, bomb disposal probably isn't for you! ヅ
 

Top
  Profile  
 
  Post subject: Re: Add Users and Aliases from CSV
Post Posted: 2008-09-18 14:18 
Normal user

Joined: 2008-01-29 07:04
Posts: 115
Thank you very much Doom -- I will try this later.


Top
  Profile  
 
  Post subject: Re: Add Users and Aliases from CSV
Post Posted: 2008-09-19 02:07 
Normal user

Joined: 2008-01-29 07:04
Posts: 115
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


Top
  Profile  
 
  Post subject: Re:
Post Posted: 2008-09-19 02:29 
Site Admin
User avatar

Joined: 2005-07-29 16:18
Posts: 13675
Location: UK
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! ヅ
 

Top
  Profile  
 
  Post subject: Re: Add Users and Aliases from CSV
Post Posted: 2008-09-19 03:01 
Normal user

Joined: 2008-01-29 07:04
Posts: 115
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?


Top
  Profile  
 
  Post subject: Re: Add Users and Aliases from CSV
Post Posted: 2008-09-19 09:10 
Site Admin
User avatar

Joined: 2005-07-29 16:18
Posts: 13675
Location: UK
You are connected to the COM API not the database.

_________________
If at first you don't succeed, bomb disposal probably isn't for you! ヅ
 

Top
  Profile  
 
  Post subject: Re: Add Users and Aliases from CSV
Post Posted: 2008-09-19 14:57 
Normal user

Joined: 2008-01-29 07:04
Posts: 115
^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.


Top
  Profile  
 
  Post subject: Re: Add Users and Aliases from CSV
Post Posted: 2008-09-19 19:09 
Site Admin
User avatar

Joined: 2005-07-29 16:18
Posts: 13675
Location: UK
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! ヅ
 

Top
  Profile  
 
  Post subject: Re: Add Users and Aliases from CSV
Post Posted: 2008-09-20 01:51 
Normal user

Joined: 2008-01-29 07:04
Posts: 115
Thanks for your help yet again, and patience. I guess I wasn't so positive on it being the correct password.


Top
  Profile  
 
  Post subject: Re: Add Users and Aliases from CSV
Post Posted: 2008-09-20 02:12 
Site Admin
User avatar

Joined: 2005-07-29 16:18
Posts: 13675
Location: UK
Did you sort it out then mate?

_________________
If at first you don't succeed, bomb disposal probably isn't for you! ヅ
 

Top
  Profile  
 
  Post subject: Re: Add Users and Aliases from CSV
Post Posted: 2008-09-20 02:20 
Normal user

Joined: 2008-01-29 07:04
Posts: 115
Yes I did, thanks a lot!


Top
  Profile  
 
  Post subject: Re: Add Users and Aliases from CSV
Post Posted: 2009-02-06 04:47 
New user
New user

Joined: 2009-02-06 04:43
Posts: 7
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


Top
  Profile  
 
  Post subject: Re: Add Users and Aliases from CSV
Post Posted: 2009-07-03 15:49 
New user
New user

Joined: 2009-06-20 12:34
Posts: 28
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.
Attachment:
Dlisterror.jpg
Dlisterror.jpg [ 18 KiB | Viewed 11680 times ] 


Top
  Profile  
 
  Post subject: Re: Add Users and Aliases from CSV
Post Posted: 2009-07-24 10:58 
Normal user

Joined: 2009-06-26 20:13
Posts: 87
Hi ...

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


Top
  Profile  
 
  Post subject: Re: Add Users and Aliases from CSV
Post Posted: 2009-07-24 11:23 
Site Admin
User avatar

Joined: 2005-07-29 16:18
Posts: 13675
Location: UK
Try it  ;)

_________________
If at first you don't succeed, bomb disposal probably isn't for you! ヅ
 

Top
  Profile  
 
  Post subject: Re: Add Users and Aliases from CSV
Post Posted: 2009-08-19 19:43 
Normal user

Joined: 2009-06-26 20:13
Posts: 87
^DooM^ wrote:
Try it  ;)


It seems to run ...  8) 


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


Top
  Profile  
 
  Post subject: Re: Add Users and Aliases from CSV
Post Posted: 2013-05-23 11:14 
New user
New user

Joined: 2013-05-18 05:35
Posts: 5
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:
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:
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:
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!


Top
  Profile  
 
  Post subject: Re: Add Users and Aliases from CSV
Post Posted: 2013-05-23 11:32 
Moderator
User avatar

Joined: 2007-06-14 05:12
Posts: 9751
Location: 'The Outback' Australia
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:
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.
Documentation
 

Top
  Profile  
 
  Post subject: Re: Add Users and Aliases from CSV
Post Posted: 2013-05-23 17:10 
New user
New user

Joined: 2013-05-18 05:35
Posts: 5
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


Top
  Profile  
 
  Post subject: Re: Add Users and Aliases from CSV
Post Posted: 2013-05-23 17:36 
Senior user
Senior user
User avatar

Joined: 2009-10-20 16:33
Posts: 871
Location: England
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.

_________________
An old dog learning new tricks...
 

Top
  Profile  
 
  Post subject: Re: Add Users and Aliases from CSV
Post Posted: 2013-05-23 18:09 
New user
New user

Joined: 2013-05-18 05:35
Posts: 5
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值