create new account with forwarding

Use this forum if you have problems with a hMailServer script, such as hMailServer WebAdmin or code in an event handler.
Post Reply
Erez.Mor
New user
New user
Posts: 3
Joined: 2023-06-07 11:02

create new account with forwarding

Post by Erez.Mor » 2023-09-26 14:55

Hello all
I am trying to use the following script to create a new account that will auto-forward incoming messages to an external address:

Code: Select all

        Dim obApp
	Set obApp = CreateObject("hMailServer.Application")
	Call obApp.Authenticate("Administrator", "MySuperSecretPasswordComesHere")
   
	' Locate the domain we want to add the account to
	Dim obDomain
	Set obDomain = obApp.Domains.ItemByName("sampledomain.com")

	Dim obAccount
	Set obAccount = obDomain.Accounts.Add

	' Set the account properties
	obAccount.Address = accName & "@sampledomain.com"
	obAccount.Password = accPass
	obAccount.Active = True
	obAccount.MaxSize = 10 'megabytes

	obAccount.ForwardAddress = "this.is@my.forward.email"
	obAccount.ForwardEnabled = True
	obAccount.ForwardKeepOriginal = False
	
	obAccount.Save
The above code creates the new account, Sets it's size, password, And even adds the auto-forward address and checks/unchecks the "Enabled" and "Keep original" check boxes.
BUT: The auto-forwarding is not working!
I noticed that if I select that account in "hMailServer Administrator" application, And then select the "forwarding" tab, The check-boxes are in-sync with the selections I made in the script, and the address to forward-to is there...BUT (only for this account), When I try to switch away to anything else from this "forwarding" tab, The app asks if I want to save changes (I didnt make any! i only selected the "forwarding" tab of the new account to see what's there...),
And finally, if I do select "yes" for the "save changes" request, The forwarding starts to work.

So in short, It seems like the 3 last settings in the code above (concerning the forwarding) are not saved (or not activated) until I save them manually in the UI. of course this misses the whole point of scripting the process.

Any thoughts/suggestions?

Thanx in advance,
E.

palinka
Senior user
Senior user
Posts: 4360
Joined: 2017-09-12 17:57

Re: create new account with forwarding

Post by palinka » 2023-09-26 15:27

Erez.Mor wrote:
2023-09-26 14:55
Hello all
I am trying to use the following script to create a new account that will auto-forward incoming messages to an external address:

Code: Select all

        Dim obApp
	Set obApp = CreateObject("hMailServer.Application")
	Call obApp.Authenticate("Administrator", "MySuperSecretPasswordComesHere")
   
	' Locate the domain we want to add the account to
	Dim obDomain
	Set obDomain = obApp.Domains.ItemByName("sampledomain.com")

	Dim obAccount
	Set obAccount = obDomain.Accounts.Add

	' Set the account properties
	obAccount.Address = accName & "@sampledomain.com"
	obAccount.Password = accPass
	obAccount.Active = True
	obAccount.MaxSize = 10 'megabytes

	obAccount.ForwardAddress = "this.is@my.forward.email"
	obAccount.ForwardEnabled = True
	obAccount.ForwardKeepOriginal = False
	
	obAccount.Save
The above code creates the new account, Sets it's size, password, And even adds the auto-forward address and checks/unchecks the "Enabled" and "Keep original" check boxes.
BUT: The auto-forwarding is not working!
I noticed that if I select that account in "hMailServer Administrator" application, And then select the "forwarding" tab, The check-boxes are in-sync with the selections I made in the script, and the address to forward-to is there...BUT (only for this account), When I try to switch away to anything else from this "forwarding" tab, The app asks if I want to save changes (I didnt make any! i only selected the "forwarding" tab of the new account to see what's there...),
And finally, if I do select "yes" for the "save changes" request, The forwarding starts to work.

So in short, It seems like the 3 last settings in the code above (concerning the forwarding) are not saved (or not activated) until I save them manually in the UI. of course this misses the whole point of scripting the process.

Any thoughts/suggestions?

Thanx in advance,
E.
This is a shot in the dark, but maybe try making "enabled" last?

Code: Select all

        Dim obApp
	Set obApp = CreateObject("hMailServer.Application")
	Call obApp.Authenticate("Administrator", "MySuperSecretPasswordComesHere")
   
	' Locate the domain we want to add the account to
	Dim obDomain
	Set obDomain = obApp.Domains.ItemByName("sampledomain.com")

	Dim obAccount
	Set obAccount = obDomain.Accounts.Add

	' Set the account properties
	obAccount.Address = accName & "@sampledomain.com"
	obAccount.Password = accPass
	obAccount.MaxSize = 10 'megabytes
	obAccount.ForwardAddress = "this.is@my.forward.email"
	obAccount.ForwardKeepOriginal = False
	obAccount.ForwardEnabled = True
	obAccount.Active = True
	obAccount.Save

Also, for whatever its worth, I just set up a test script to see what gets returned for "ForwardEnabled" on an address that has forwarding enabled.

Code: Select all

Option Explicit
Dim obApp : Set obApp = CreateObject("hMailServer.Application")
Call obApp.Authenticate("Administrator", "secret")
Dim obAccount : Set obAccount = obApp.Domains.ItemByName("mydomain.com").Accounts.ItemByAddress("user@mydomain.com")
WScript.Echo obAccount.ForwardEnabled
It returned "-1". I have no idea why its negative.

User avatar
SorenR
Senior user
Senior user
Posts: 6128
Joined: 2006-08-21 15:38
Location: Denmark

Re: create new account with forwarding

Post by SorenR » 2023-09-26 17:22

Works fine on my server... Servers are headless so using DCOM thus the "\\mailserver"

Code: Select all

Dim oApp : Set oApp = CreateObject("hMailServer.Application", "\\mailserver")
Call oApp.Authenticate("Administrator", "DoubleSecretCosmicPassword")

' Locate the domain we want to add the account to
Dim oDomain : Set oDomain = oApp.Domains.ItemByName("acme.inc")

Dim oAccount : Set oAccount = oDomain.Accounts.Add

' Set the account properties
oAccount.Address = "wile.e.coyote@acme.inc"
oAccount.Password = "secret"
oAccount.Active = True
oAccount.MaxSize = 10 'megabytes

oAccount.ForwardAddress = "pinky.and.the.brain@acme.inc"
oAccount.ForwardEnabled = True
oAccount.ForwardKeepOriginal = False

oAccount.Save

Set oAccount = Nothing
Set oDomain = Nothing
Set oApp = Nothing
SørenR.

To understand recursion, you must first understand recursion.

Post Reply