change Sender Address

Use this forum if you want to suggest a new feature to hMailServer. Before posting, please search the forum to confirm that it has not already been suggested.
Post Reply
rookie089
New user
New user
Posts: 6
Joined: 2019-12-17 01:04

change Sender Address

Post by rookie089 » 2019-12-17 06:52

Hello Guys,

my name is Timur from Germany. I have a problem with hmailserver and would appreciate, if someone could help me. In my Lan i have some smtp clients that dont support smtp auth. thats why i set up an hmailserver with smtp relayer and disables "requires smtp auth" in ip ranges settings.
as far as these smtp clients use the same sender address thats configured in my smtp relayer account everything works fine. lets call this Email address smtprelay@mydomain.com. Unfortunaltely i have some smtp client, that cannot use smtprelay@mydomain.com as the sender address.They have subdomain sender addresses, like server1@sub.mydomain.com. so i decided to replace the sender address by a rule. So i created:

criteria
from|contains|sub.mydomain.com

Actions
Set header value|from|smtprelay@mydomain.com

but its not working

how can i access the modified Email?

in hmailserver_awstats.log its trying to send with server1@sub.mydomain.com
in htmailserver log it says
"DEBUG" 4300 "2019-12-17 05:42:24.381" "Applying rules"
"DEBUG" 4300 "2019-12-17 05:42:24.381" "Applying rule Rewrite sender"
"DEBUG" 4300 "2019-12-17 05:42:24.381" "Performing rule action"
"DEBUG" 4300 "2019-12-17 05:42:24.381" "Performing local delivery"
"DEBUG" 4300 "2019-12-17 05:42:24.381" "Local delivery completed"

"SMTPC" 6828 73 "2019-12-17 05:47:57.958" "83.246.107.173" "RECEIVED: 354 Start mail input; end with <CRLF>.<CRLF>"
"SMTPC" 6828 73 "2019-12-17 05:47:57.958" "83.246.107.173" "SENT: [nl]."
"SMTPC" 4352 73 "2019-12-17 05:47:58.036" "83.246.107.173" "RECEIVED: 550 5.7.60 SMTP; Client does not have permissions to send as this sender"
"SMTPC" 4352 73 "2019-12-17 05:47:58.036" "83.246.107.173" "SENT: QUIT"
"SMTPC" 5296 73 "2019-12-17 05:47:58.067" "83.246.107.173" "RECEIVED: 221 2.0.0 Service closing transmission channel"


thanks for help

Timur

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

Re: change Sender Address

Post by mattg » 2019-12-17 07:53

Here is a script to do that

Code: Select all

Sub OnAcceptMessage(oClient, oMessage)
	if instr(oMessage.FromAddress,"sub.domain.com") >0  then
		oldFrom = oMessage.FromAddress
		oMessage.FromAddress = "new@example.com"
		oMessage.From = "<new@example.com>"
		EventLog.Write("Testing account From rewrite " & oldFrom & " - to - " & oMessage.FromAddress )
		oMessage.save
		Result.value = 0
	end if
End Sub
Add to your eventhandlers.vbs, and enable scripting in the admin gui

From this thread, kudos to @tunis
viewtopic.php?f=9&t=32387#p202248
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

rookie089
New user
New user
Posts: 6
Joined: 2019-12-17 01:04

Re: change Sender Address

Post by rookie089 » 2019-12-17 13:31

it worked like a charm. Thank you so much!!!

rookie089
New user
New user
Posts: 6
Joined: 2019-12-17 01:04

Re: change Sender Address

Post by rookie089 » 2019-12-17 18:06

Hello MattG,

can i bother you once more.

is it possible to change the script to make it universal. What i mean is, i have different sender addresses, but they have the same pattern, for example: server1@sub.mydomain.com, server5@sub2.mydomain.com or server2@sub125.mydomain.com

the script should eliminate the part of the subdomain, regardless which value it has. So it should replace it with: server1@mydomain.com, server5@mydomain.com or server2@mydomain.com

Is this possible?

i would appreciate your help

thanks

Timur

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

Re: change Sender Address

Post by mattg » 2019-12-17 22:21

just change this line

if instr(oMessage.FromAddress,"sub.domain.com") >0 then

to be like

if instr(oMessage.FromAddress,".domain.com") >0 then


And maybe even you can remove the leading . to just have "domain.com"
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

rookie089
New user
New user
Posts: 6
Joined: 2019-12-17 01:04

Re: change Sender Address

Post by rookie089 » 2019-12-18 00:13

ok, but how do i pick the local Part of the Email address?
for example server5@sub2.mydomain.com and put it in
oMessage.From = "<server5@mydomain.com>"

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

Re: change Sender Address

Post by mattg » 2019-12-18 00:19

left(oMessage.FromAddress, instr(oMessage.FromAddress, "@")-1)

I think the -1 is needed without testing, but it may not be.

Post your script as it stands if you are still stuck please
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

rookie089
New user
New user
Posts: 6
Joined: 2019-12-17 01:04

Re: change Sender Address

Post by rookie089 » 2019-12-18 02:36

how can i add mydomain.com ?

this does not work:

oMessage.FromAddress = left(oMessage.FromAddress, instr(oMessage.FromAddress, "@") & "mydomain.com" )

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

Re: change Sender Address

Post by mattg » 2019-12-18 03:51

rookie089 wrote:
2019-12-18 02:36
how can i add mydomain.com ?

this does not work:

oMessage.FromAddress = left(oMessage.FromAddress, instr(oMessage.FromAddress, "@") & "mydomain.com" )
Bracket is in the wrong place


oMessage.FromAddress = left(oMessage.FromAddress, instr(oMessage.FromAddress, "@")) & "mydomain.com"
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

rookie089
New user
New user
Posts: 6
Joined: 2019-12-17 01:04

Re: change Sender Address

Post by rookie089 » 2019-12-18 05:49

it worked, thanks

mcain
New user
New user
Posts: 1
Joined: 2023-02-17 03:13

Re: change Sender Address

Post by mcain » 2023-02-17 03:16

Posted for anyone who might find this useful: to replace one domain with another. This also updates the Message ID value successfully.

Code: Select all

Sub OnAcceptMessage(oClient, oMessage)

	Dim domainOld, domainNew
	domainOld = "domain1.com"
	domainNew = "domain2.com"

	If (instr(oMessage.FromAddress, domainOld) > 0) Then

		oldFrom = oMessage.FromAddress

		oMessage.FromAddress = Replace(oMessage.FromAddress, domainOld, domainNew)
		oMessage.From = Replace(oMessage.From, domainOld, domainNew)
		oMessage.HeaderValue("Message-ID") = Replace(oMessage.HeaderValue("Message-ID"), domainOld, domainNew)

		'EventLog.Write("Testing account From rewrite " & oldFrom & " - to - " & oMessage.FromAddress & " ID:" & oMessage.HeaderValue("Message-ID") )
		oMessage.Save
		Result.value = 0

	End If

End Sub

Enghany
New user
New user
Posts: 1
Joined: 2024-06-14 23:00

Re: change Sender Address

Post by Enghany » 2024-06-14 23:03

I just want to change the sender display name
Let’s say my sender account is someone@domain.local
I want to change the display name to New One

Could you help me with the script for this

Post Reply