change external Accounts from Textfile

This section contains scripts that hMailServer has contributed with. hMailServer 5 is needed to use these.
Post Reply
thoenny
New user
New user
Posts: 1
Joined: 2018-02-06 21:30

change external Accounts from Textfile

Post by thoenny » 2018-02-22 23:20

Hi all,

I had the issue, that our ISP changed his mail configuration and we had to change the pop3 hostname and the users passwords in all th 238 external accounts of our hmail installation. I found some useful postings here like (thanks to percepts, jimimaseye, Bladeinger, Bill48105 and others....):
viewtopic.php?f=20&t=18303
viewtopic.php?f=20&t=24810&p=201594&hilit=csv#p149923

Since my day as a programmer are long ago (BASIC on a VC20, Pascal on Apple and some sort of C, C++ on Unix) I was glad to have some kind of templates here that helped me. I coded something, that was suitable in my situation (each account has exactly one external account, for example), but it worked for me and perhaps somebody else has the same issue and some programming professionals can correct/improve the code:

Code: Select all

'vbs script to update external accounts from a textfile into hmail server
'C. Thönny
'inspired by percepts, jimimaseye, Bladeinger, Bill48105 and others....
' see https://www.hmailserver.com/forum/viewtopic.php?f=20&t=18303
' and https://www.hmailserver.com/forum/viewtopic.php?f=20&t=24810&p=201594&hilit=csv#p149923


Dim user
Dim domain
Dim password 
Dim tmp
Dim obDomain
Dim obAccount
Dim obExtAccount  
Dim c 
 
 
 Dim obApp
   Set obApp = CreateObject("hMailServer.Application")
   Call obApp.Authenticate("superuser", "extremlytopsecretpassword")
   ' get the text file with data, make sure it exists
   Set obFSO=CreateObject("Scripting.FileSystemObject")
   Set obTextFile=obFSO.OpenTextFile("C:\scripts\mails.csv",1)
   ' open a log file for writing, make sure, it exists
   Set obLogFile=obFSO.OpenTextFile("c:\scripts\hmailtest.log",2)
   
   ' process the data file and count the data lines
   c=0
   MsgBox("Beginning ....")
   Do while obTextFile.AtEndOfStream <> True
		c=c+1
		data=split(obTextFile.Readline,",")
		tmp=split(data(0),"@")
		user=tmp(0)
		domain=tmp(1)
		password=data(2)
		obLogFile.Write("Dataset "&c&":"&"user: "&user&"@"&domain&" pass:" &password &"----------")
		'process the data:
		'Locate the domain we want to add the account to
		Set obDomain = obApp.Domains.ItemByName(domain)
		On Error Resume Next 
		Set obAccount = obDomain.Accounts.ItemByAddress(data(0))
		if err.number <> 0 then 
			obLogFile.Writeline("FAIL, probably doesn't exist in hmail Accounts? ")
		else 
			Set obExtAccount=obAccount.FetchAccounts.Item(0)
			if StrComp(obExtAccount.username,data(0)) <> 0 then obLogFile.Write("---------------------------------------------CAUTION, Missmatch of Account and external Account:" & obAccount.Address)
			obExtAccount.ServerAddress="new.pop3server.isp"
			obExtAccount.Password=password	
			'do something else like:
			'obExtAccount.DaysToKeepMessages=0
			'obExtAccount.Enabled=False
			obExtAccount.Save()
			obLogFile.Writeline("Success External Account " & obExtAccount.Username)
		end if 
    Loop
	obLogFile.WriteLine("Ready, processed "& c & " external Accounts")	
	obLogFile.Close
	obTextFile.Close
	MsgBox(" ... ready. That's all, folks")



any improvements appreciate :-)

Post Reply