Problem, different from address when forwarding using account forwarding tab and account rule

Use this forum if you have installed hMailServer and want to ask a question related to a production release of hMailServer. Before posting, please read the troubleshooting guide. A large part of all reported issues are already described in detail here.
Post Reply
ilpanos
New user
New user
Posts: 9
Joined: 2017-12-12 14:52

Problem, different from address when forwarding using account forwarding tab and account rule

Post by ilpanos » 2019-05-29 11:08

Hello,

I currently use hMailServer 5.6.6-B2383.
I am trying to forward emails using account rules, ex:

Criteria: From contains abc@domain1.com
Actions: Forward email to xyz@externaldomain.com

The emails are being forwarded using the From address of the account. However I need the forwarded emails to keep the original From address rather than changing to that of the forwarding account.

I have also added the following at the end of hMailServer.ini:
[Settings]
RewriteEnvelopeFromWhenForwarding=0

I noticed that when using the account's forwarding tab to set the forward, the forwarded emails keep the original From address, but this tab lacks any further needed settings.

So I need to achieve the same functionality (forwarding emails that keep the original From address) using account rules.
Is there any way to do that?

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

Re: Problem, different from address when forwarding using account forwarding tab and account rule

Post by mattg » 2019-05-29 17:51

In your hmailserver.ini file add this to the bottom

Code: Select all

[settings]
RewriteEnvelopeFromWhenForwarding=0
; When performing forwarding, hMailServer now keeps the original From address rather than changing to that of the forwarding account.
; This change was made to reduce risk of message delivery failures.
; To force the previous behavior, set RewriteEnvelopeFromWhenForwarding=1
Then restart your hmailserver
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

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

Re: Problem, different from address when forwarding using account forwarding tab and account rule

Post by katip » 2019-05-29 22:12

ilpanos wrote:
2019-05-29 11:08
The emails are being forwarded using the From address of the account. However I need the forwarded emails to keep the original From address rather than changing to that of the forwarding account.
interesting. i didn't know this.
i use account level rules to forward tagged (by script) messages to another account. hundreds every day. all arrive there with original From intact.
no RewriteEnvelopeFromWhenForwarding in ini. i.e. default behaviour applies.
v5.6.7 b2425

from changelog:
Version 5.6.2 - Build 2223 (2015-03-12)
Issue 50: When performing forwarding, hMailServer now keeps the original From address rather than changing to that of the forwarding account. This change was made to reduce risk of message delivery failures. To force the previous behavior, set RewriteEnvelopeFromWhenForwarding=1 under the [Settings] section in hMailServer.ini.
Katip
--
HMS 5.7.0-B2428-LTS-64-bit, MySQL 5.7.24, SA 3.4.2, ClamAV 0.101.2 + SaneS

ilpanos
New user
New user
Posts: 9
Joined: 2017-12-12 14:52

Re: Problem, different from address when forwarding using account forwarding tab and account rule

Post by ilpanos » 2019-05-30 09:30

mattg wrote:
2019-05-29 17:51
In your hmailserver.ini file add this to the bottom

Code: Select all

[settings]
RewriteEnvelopeFromWhenForwarding=0
; When performing forwarding, hMailServer now keeps the original From address rather than changing to that of the forwarding account.
; This change was made to reduce risk of message delivery failures.
; To force the previous behavior, set RewriteEnvelopeFromWhenForwarding=1
Then restart your hmailserver
That was my first action, it changed to keeping the original FromAddress when using the account's forwarding tab to set the forward.
When using account rules to forward it still changes the envelope sender (FromAddress) to that of the account.

ilpanos
New user
New user
Posts: 9
Joined: 2017-12-12 14:52

Re: Problem, different from address when forwarding using account forwarding tab and account rule

Post by ilpanos » 2019-05-30 09:34

katip wrote:
2019-05-29 22:12
ilpanos wrote:
2019-05-29 11:08
The emails are being forwarded using the From address of the account. However I need the forwarded emails to keep the original From address rather than changing to that of the forwarding account.
interesting. i didn't know this.
i use account level rules to forward tagged (by script) messages to another account. hundreds every day. all arrive there with original From intact.
no RewriteEnvelopeFromWhenForwarding in ini. i.e. default behaviour applies.
v5.6.7 b2425

from changelog:
Version 5.6.2 - Build 2223 (2015-03-12)
Issue 50: When performing forwarding, hMailServer now keeps the original From address rather than changing to that of the forwarding account. This change was made to reduce risk of message delivery failures. To force the previous behavior, set RewriteEnvelopeFromWhenForwarding=1 under the [Settings] section in hMailServer.ini.
The From header is indeed intact, however the FromAddress (envelope sender from my understanding and not From header) changes, maybe i wasn't clear in the first post.

I added:
[Settings]
RewriteEnvelopeFromWhenForwarding=0
and restarted the service but it didn't help.

hMailServer 5.6.6-B2383

User avatar
RvdH
Senior user
Senior user
Posts: 798
Joined: 2008-06-27 14:42
Location: Netherlands

Re: Problem, different from address when forwarding using account forwarding tab and account rule

Post by RvdH » 2019-05-30 11:44

@ilpanos

You are right, the FromAddress is modified using the Forward Account rule mechanism...but i do'nt see why you need that to be different as you pointed out
  • If it's forwarded to a domain on the same hmailserver instance it doens't influence anything
  • If it's forwarded to a external domain the SPF and DKIM signatures/records can made matching to your hmailserver instance so it most likely not as easy marked as spam by the external receiving MTA
CIDR to RegEx: d-fault.nl/CIDRtoRegEx
DNS Lookup: d-fault.nl/DNSTools
DNSBL Lookup: d-fault.nl/DNSBLLookup
GEOIP Lookup: d-fault.nl/GeoipLookup

ilpanos
New user
New user
Posts: 9
Joined: 2017-12-12 14:52

Re: Problem, different from address when forwarding using account forwarding tab and account rule

Post by ilpanos » 2019-05-30 13:47

RvdH wrote:
2019-05-30 11:44
@ilpanos

You are right, the FromAddress is modified using the Forward Account rule mechanism...but i do'nt see why you need that to be different as you pointed out
  • If it's forwarded to a domain on the same hmailserver instance it doens't influence anything
  • If it's forwarded to a external domain the SPF and DKIM signatures/records can made matching to your hmailserver instance so it most likely not as easy marked as spam by the external receiving MTA
Yes this is the problem I have, the FromAddress is modified.

After adding:
[Settings]
RewriteEnvelopeFromWhenForwarding=0

, if I use the account's forwarding tab to forward, the forwarded emails keep the original FromAddress just the way I need.
However, when using the Forward Account rule mechanism the FromAddress keeps being modified.

Is this the expected behavior? Is there any way to change it?

Ok, to put things in the right context I use a new external SMTP relay server that needs per user authentication (that unfortunately I have to accomplish by making matching rule and route pair for each user account) and in the same time it won't accept emails that have different FromAddress and SMTP username credentials.

Now, I need some account forwards to external domains. That means that if user aaa@domain.xyz sends an internal email to user bbb@domain.xyz and user bbb@domain.xyz has a forward rule to an external domain, hmailserver will try to forward the email using bbb@domain.xyz FromAddress and aaa@domain.xyz From header.

So an email with bbb@domain.xyz FromAddress and aaa@domain.xyz From header will be send using aaa@domain.xyz user's route and SMTP credentials.
And the SMTP relay server will not accept it for delivery due to the FromAddress and username in SMTP credentials not matching.

User avatar
RvdH
Senior user
Senior user
Posts: 798
Joined: 2008-06-27 14:42
Location: Netherlands

Re: Problem, different from address when forwarding using account forwarding tab and account rule

Post by RvdH » 2019-05-30 14:21

ilpanos wrote:
2019-05-30 13:47
Is this the expected behavior? Is there any way to change it?
If you know your c++ and build your own hmailserver.exe
the code you are looking for is in RuleApplier.cpp

approx line 248

Code: Select all

   void 
   RuleApplier::ApplyAction_Forward(std::shared_ptr<RuleAction> pAction, std::shared_ptr<const Account> account, std::shared_ptr<MessageData> pMsgData) const
   {
   ...
   
   
      // We need to update the SMTP envelope from address, if this
      // message is forwarded by a user-level account.
      std::shared_ptr<CONST Account> pAccount = CacheContainer::Instance()->GetAccount(rule_account_id_);
      if (pAccount)
         pMsg->SetFromAddress(pAccount->GetAddress());
         
         
   ...     
   }
CIDR to RegEx: d-fault.nl/CIDRtoRegEx
DNS Lookup: d-fault.nl/DNSTools
DNSBL Lookup: d-fault.nl/DNSBLLookup
GEOIP Lookup: d-fault.nl/GeoipLookup

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

Re: Problem, different from address when forwarding using account forwarding tab and account rule

Post by katip » 2019-05-30 14:45

ilpanos wrote:
2019-05-30 09:34
The From header is indeed intact, however the FromAddress (envelope sender from my understanding and not From header) changes, maybe i wasn't clear in the first post.

I added:
[Settings]
RewriteEnvelopeFromWhenForwarding=0
and restarted the service but it didn't help.

hMailServer 5.6.6-B2383
learnt something new... yes, Return-Path reads address of forwarding account.
it's like Bounce/Redirect feature of some email clients.
Katip
--
HMS 5.7.0-B2428-LTS-64-bit, MySQL 5.7.24, SA 3.4.2, ClamAV 0.101.2 + SaneS

ilpanos
New user
New user
Posts: 9
Joined: 2017-12-12 14:52

Re: Problem, different from address when forwarding using account forwarding tab and account rule

Post by ilpanos » 2019-05-31 09:31

katip wrote:
2019-05-30 14:45
ilpanos wrote:
2019-05-30 09:34
The From header is indeed intact, however the FromAddress (envelope sender from my understanding and not From header) changes, maybe i wasn't clear in the first post.

I added:
[Settings]
RewriteEnvelopeFromWhenForwarding=0
and restarted the service but it didn't help.

hMailServer 5.6.6-B2383
learnt something new... yes, Return-Path reads address of forwarding account.
it's like Bounce/Redirect feature of some email clients.
But the forwarded emails keep the original FromAddress if I use the account's forwarding tab to forward,
2019-05-31 09_43_00.png
2019-05-31 09_43_00.png (3.45 KiB) Viewed 1009 times

but do not keep it when using account forward rule.
2019-05-31 09_43_47.png
2019-05-31 09_43_47.png (3.74 KiB) Viewed 1009 times


Shouldn't they both use the same account forward mechanism and have the same functionality?
Does the setting RewriteEnvelopeFromWhenForwarding affect only one of the 2 options or am I missing something?

ilpanos
New user
New user
Posts: 9
Joined: 2017-12-12 14:52

Re: Problem, different from address when forwarding using account forwarding tab and account rule

Post by ilpanos » 2019-05-31 09:39

RvdH wrote:
2019-05-30 14:21
ilpanos wrote:
2019-05-30 13:47
Is this the expected behavior? Is there any way to change it?

If you know your c++ and build your own hmailserver.exe
the code you are looking for is in RuleApplier.cpp

approx line 248

Code: Select all

   void 
   RuleApplier::ApplyAction_Forward(std::shared_ptr<RuleAction> pAction, std::shared_ptr<const Account> account, std::shared_ptr<MessageData> pMsgData) const
   {
   ...
   
   
      // We need to update the SMTP envelope from address, if this
      // message is forwarded by a user-level account.
      std::shared_ptr<CONST Account> pAccount = CacheContainer::Instance()->GetAccount(rule_account_id_);
      if (pAccount)
         pMsg->SetFromAddress(pAccount->GetAddress());
         
         
   ...     
   }
Unfortunately I do not have the required programming skills to do that.
I do however try to achieve the desired functionality by writing a VBscript function in event handlers file.

I plan to tag the emails that are to be forwarded using account rules and then have a global rule that runs the function against these emails. I need this function to copy oMessage.From to oMessage.FromAddress without the name or char < or >, just a clean abc@domain.xyz.

I share the above mentioned function and I am open to any suggestion for doing this more efficiently or showing something I missed or even an entirely different approach:

Code: Select all

    Sub ChangeEnvelopeFrom(oMessage)
		'split the From address at each space char into an array of words
		temparray=Split(oMessage.From)
		'find how many rows-words are in the array
		array_size=UBound(temparray)
		'if the last char of the last word of the array is the char >
		if Right(temparray(array_size),1) = ">" Then
			'starting from left copy the chars of the word except the last char
			truncate_from_last_char = Left(temparray(array_size), Len(temparray(array_size)) - 1)
		Else
			'copy all chars
			truncate_from_last_char = temparray(array_size)
		End If
		'if the first char of the last word of the array is the char <
		if Left(truncate_from_last_char,1) = "<" Then
			'starting from right -the end- copy the chars of the word except the last one the first char of the word
			truncate_from_first_char = Right(truncate_from_last_char, Len(truncate_from_last_char) - 1)
		Else 
			'copy all chars
			truncate_from_first_char = truncate_from_last_char
		End If
		oMessage.FromAddress = truncate_from_first_char
    End Sub
 

User avatar
RvdH
Senior user
Senior user
Posts: 798
Joined: 2008-06-27 14:42
Location: Netherlands

Re: Problem, different from address when forwarding using account forwarding tab and account rule

Post by RvdH » 2019-05-31 10:28

I can make you a test build that respects the 'RewriteEnvelopeFromWhenForwarding' ini file setting when used in Account Forwarding rules

I think it actually very simple, eg:

Code: Select all

      // We need to update the SMTP envelope from address, if this
      // message is forwarded by a user-level account.
      std::shared_ptr<CONST Account> pAccount = CacheContainer::Instance()->GetAccount(rule_account_id_);
	  if (pAccount && IniFileSettings::Instance()->GetRewriteEnvelopeFromWhenForwarding())
         pMsg->SetFromAddress(pAccount->GetAddress());
You need to upgrade to (non officially released) version 5.6.8-B2437 first though
CIDR to RegEx: d-fault.nl/CIDRtoRegEx
DNS Lookup: d-fault.nl/DNSTools
DNSBL Lookup: d-fault.nl/DNSBLLookup
GEOIP Lookup: d-fault.nl/GeoipLookup

User avatar
RvdH
Senior user
Senior user
Posts: 798
Joined: 2008-06-27 14:42
Location: Netherlands

Re: Problem, different from address when forwarding using account forwarding tab and account rule

Post by RvdH » 2019-06-02 09:37

If you would like to give my build a go, it is here: https://www.hmailserver.com/forum/viewt ... 68#p212268
CIDR to RegEx: d-fault.nl/CIDRtoRegEx
DNS Lookup: d-fault.nl/DNSTools
DNSBL Lookup: d-fault.nl/DNSBLLookup
GEOIP Lookup: d-fault.nl/GeoipLookup

Post Reply