Set ForwardAddress from a Script doesnt work

Use this forum if you have problems with a hMailServer script, such as hMailServer WebAdmin or code in an event handler.
Post Reply
Wichert
New user
New user
Posts: 7
Joined: 2019-10-07 15:02

Set ForwardAddress from a Script doesnt work

Post by Wichert » 2019-10-21 09:17

Hi,

im new at Hmailserver and the scripting with it.
I searched for a possibility to activate and set the ForwardAddress form a Script.
At the moment i have the follwed code and it works for the Fields. I can look at the Webinterface and the Checkboxes and the Address is correctly insert.
But it doesnt work. The Hmailserver tried 7 times and said that the recipients could not be reached.
After i pressed "Save" in the Webinterface it works fine.

I used the following forum entry for my scripting:
https://www.hmailserver.com/forum/viewtopic.php?t=9875

I hope someone can help.

Best Regards

My Code:

Code: Select all

Sub ForwardON(oMessage)
   Dim obApp
   Set obApp = CreateObject("hMailServer.Application")
   Call obApp.Authenticate("Administrator","******")

	strForward= oMessage.body

	aUsername = Split(oMessage.fromaddress,"@")
	Dim obDomain
	Set obDomain = obApp.Domains.ItemByName(aUsername(1))
	Dim obAccount
	Set obAccount = obDomain.Accounts.ItemByAddress(oMessage.fromaddress)

	obAccount.ForwardAddress  = strForward
	obAccount.ForwardEnabled = 1
	obAccount.ForwardKeepOriginal = 1
	
	oMessage.subject = "Emails are forward to" & strForward
	oMessage.body = strForward
	
	obAccount.Save 'saves account
	oMessage.save
End Sub

User avatar
katip
Senior user
Senior user
Posts: 757
Joined: 2006-12-22 07:58
Location: Istanbul

Re: Set ForwardAddress from a Script doesnt work

Post by katip » 2019-10-21 11:59

strForward= oMessage.body
....
obAccount.ForwardAddress = strForward

there is something wrong i think. you should enter an address here. not the mail body.
Katip
--
HMS 5.7.0 x64, MariaDB 10.4.10 x64, SA 3.4.2, ClamAV 0.101.2 + SaneS

Wichert
New user
New user
Posts: 7
Joined: 2019-10-07 15:02

Re: Set ForwardAddress from a Script doesnt work

Post by Wichert » 2019-10-21 13:07

Hi,

i type only the E-Mail Adress into the "body" an set the mails as "plain-text".

Is that impossible?

User avatar
katip
Senior user
Senior user
Posts: 757
Joined: 2006-12-22 07:58
Location: Istanbul

Re: Set ForwardAddress from a Script doesnt work

Post by katip » 2019-10-21 15:47

i don't think it will be parsed correctly.
i suppose you try to activate forwarding by sending a message. try Subject instead of Body to put the address.
Katip
--
HMS 5.7.0 x64, MariaDB 10.4.10 x64, SA 3.4.2, ClamAV 0.101.2 + SaneS

Wichert
New user
New user
Posts: 7
Joined: 2019-10-07 15:02

Re: Set ForwardAddress from a Script doesnt work

Post by Wichert » 2019-10-22 14:55

Thx Katip,

it was a Problem with the "body".
I wrote the body to a File and read only the row with Email Address.
In my case its the first row.

See code below if someone need this.

Code: Select all

Sub WeiterleitungAN(oMessage)
   Dim CreateGUIDval, obUtilities, PathStore
   PathStore = "C:\Mailing\Weiterleitung"
   Set obUtilities = CreateObject("hMailServer.Utilities")
   TempParamFile = PathStore & "\" & Mid(obUtilities.GenerateGUID, 2, 36) & ".txt"

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile(TempParamFile, 2, true)
    objTextFile.WriteLine(oMessage.body)
    objTextFile.Close
   
   Dim Weiterleitungsadresse
       
   Set oRead = objFSO.OpenTextFile(TempParamFile, 1)
   Do Until oRead.AtEndOfStream
     MessageInputLine = oRead.ReadLine
	 If not MessageInputLine = "" then
		 Weiterleitungsadresse = MessageInputLine
	End if
   Loop
	oRead.Close()  ' Close input file	
	objFSO.DeleteFile TempParamFile
	
   Dim obApp
   Set obApp = CreateObject("hMailServer.Application")
   Call obApp.Authenticate("Administrator","******")

	aUsername = Split(oMessage.fromaddress,"@")
	Dim obDomain
	Set obDomain = obApp.Domains.ItemByName(aUsername(1))
	Dim obAccount
	Set obAccount = obDomain.Accounts.ItemByAddress(oMessage.fromaddress)

	obAccount.ForwardAddress  = Weiterleitungsadresse
	obAccount.ForwardEnabled = 1
	obAccount.ForwardKeepOriginal = 1
	
	oMessage.subject = "E-Mails werden weitergeleitet an: " & Weiterleitungsadresse
	
	obAccount.Save 'saves account
	oMessage.save
End Sub

User avatar
katip
Senior user
Senior user
Posts: 757
Joined: 2006-12-22 07:58
Location: Istanbul

Re: Set ForwardAddress from a Script doesnt work

Post by katip » 2019-10-22 15:49

that looks fine now.
thanks for sharing.
Katip
--
HMS 5.7.0 x64, MariaDB 10.4.10 x64, SA 3.4.2, ClamAV 0.101.2 + SaneS

Post Reply