[SOLVED] Office 365: Exchange mail flow to modify the header 'From'property?

Moltuae

Rest In Peace
Reaction score
3,671
Location
Lancs, UK
So for outgoing emails from a particular email address, where the 'From' property contains no 'friendly' name (ie just the sender's email address), I'd like to intercept it to add a friendly name.

Is this possible with O365/Exchange?

Reason being: I have an SMTP Relay Server (local only) that relays automated emails sent from various devices/software on the LAN, out through a single O365 account (using various aliases). Some of the devices/software do not give the option to add a friendly 'from' property, for example:
Code:
'Invoices - Contoso Ltd. <invoices.outbound@cltd.co.uk>'
Presently the 'from' property looks like this:
Code:
'invoices.outbound@cltd.co.uk <invoices.outbound@cltd.co.uk>'
... so it's not immediately clear to the recipient where the email is from.

I have tried adding a mail flow rule, such that if the "from header includes.." invoices.outbound@cltd.co.uk, it should set the message header 'From' to the value 'Invoices - Contoso Ltd. <invoices.outbound@cltd.co.uk>', but that results in and unhelpful error: "You can't set header 'From' with value 'Invoices - Contoso Ltd. <invoices.outbound@cltd.co.uk>'". I have tried various formats of 'from' values with the same result.

Thanks in advance for any help.
 
Last edited:
Thanks Stonecat. I suppose that's one solution.

I was hoping it might be possible to amend the header 'From' property though, so that recipient doesn't just see an email address, making it clear who the email is from before it's opened. Is there no way to do that?
 
Is the user info filled out in the User Props in O365?
No, I'm using a aliases. There's a (2012) server acting as an SMTP relay server for LAN devices/software. The SMTP relay server relays the mail (via a connector) to an O365 account that has numerous aliases (eg scanner@, backups@, accounts@, alerts@, etc), enabling said software/devices to use an appropriate 'from' email address. So, for devices that have limited email capabilities, it greatly simplifies setup, requiring only the relay server IP and 'from' address, which can be any of the aliases.

For software/devices that have a ' friendly from' field (or accept an email address that includes one in a header format) it works fine; I'm just looking to find a way to intercept and change the from field for software/devices that don't. If that makes sense ...
 
Ahh gotcha. So the mechanism that originate the "invoices" email...on the LAN...shoots it to a local server which relays. The originating mechanism generating the invoices can't reach out to O365 directly...to something like an Invoices account up in the O365 tenant?

I see what you have here...local mechanisms on the LAN generate the original email but only shoot to the server which does SMTP relay. So the headers aren't able to get modified enough there. I'm just wondering if you can do direct auth from the invoice generating software direct to the O365 tenant using an acccount in the tenant that you call "invoices".
 
I'm just wondering if you can do direct auth from the invoice generating software direct to the O365 tenant using an acccount in the tenant that you call "invoices".

I probably could, with a little work ... the software has very limited authentication options. It would just be nice to not have to add another email account for this, and this method of using a single account with multiple aliases is an otherwise universal solution that works well. It seems the ability to intercept and change the header is there in O365 but I just can't get it to work. I suspect it's just a syntax/format error on my part but I can find any documentation about it.
 

There's not unfortunately, in this instance, but thanks for the links. :)

The second link (setting up Outlook to send via an alias) is an interesting idea, which may come in useful at a later date, but Outlook already has a setting for a display name (or 'friendly' name), so there's nothing there that describes how to intercept and add a friendly name using O365 Exchange.

The first link seems to be describing a way of modifying the information passed to a local Exchange server in a domain environment. Interesting, but not what I'm looking for unfortunately. I really appreciate the help though.
 
  • Like
Reactions: GTP
This idea piqued my curiosity. I found the New-AddressRewriteEntry cmdlet. It only seems to work with on-prem Exchange though.
Thanks :)

I'm not sure it'll do what I want but I suppose I could use that method to change the outbound From email address instead. Although I wonder if it might accept a header-formatted email address that also includes the friendly name, like this:
'Invoices - Contoso Ltd. <invoices.outbound@cltd.co.uk>'

EDIT: Yeah, unfortunately looks like in applies to on-premises only, like you said.
 
Last edited:
Thanks :)

I'm not sure it'll do what I want but I suppose I could use that method to change the outbound From email address instead. Although I wonder if it might accept a header-formatted email address that also includes the friendly name, like this:
'Invoices - Contoso Ltd. <invoices.outbound@cltd.co.uk>'

EDIT: Yeah, unfortunately looks like in applies to on-premises only, like you said.

Seems like the answer should be here, unfortunately don't have the time to read through it in detail right now.
 
I almost had cause to replace an iis on premise SMTP relay due to needing to rewrite headers.

I never had to do it in the end but I did find a free email server for Windows that had a lot more functionality than iis: https://www.hmailserver.com/functionality

https://www.hmailserver.com/forum/viewtopic.php?t=31135

Maybe this free email service can rewrite the email before it leaves site..
Thanks!

I did start looking for something similar, thinking that it might be easier to intercept and change the header before it leaves site, but didn't find anything. I'll take a look at hMailServer.
 
I almost had cause to replace an iis on premise SMTP relay due to needing to rewrite headers.

I never had to do it in the end but I did find a free email server for Windows that had a lot more functionality than iis: https://www.hmailserver.com/functionality

https://www.hmailserver.com/forum/viewtopic.php?t=31135

Maybe this free email service can rewrite the email before it leaves site..

This did the trick. Thanks again for the suggestion.

I followed this guide, for the most part, to configure hMS as an SMTP relay service, modifying the relevant settings to work with an Office 365 account:
http://bendetat.com/setting_up_an_open_smtp_relay_in_an_intranet_with_hmailserver.html

My O365 changes are marked in red ..
  1. Install hMailServer on a Windows machine within the intranet. Make sure port 25 isn’t blocked by a firewall on the machine. The default self-hosted SQL Server Compact database option in hMailServer should be fine.
  2. Create an email address on the external mail server. Something like relay@example.com.
  3. Create a wildcard route on hMailServer.
    • This is a domain of *.
    • The target SMTP host should be set to smtp.office365.com
    • The TCP/IP port should be set to 587.
    • Connection security: STARTTLS (Required)
    • Under the Delivery tab check the Server requires authentication box and fill in the user name and password for the relay email address.
  4. The trick is that hMailServer doesn’t seem to support wildcard routes, even though it will let you create a route of *. To work around this you just add a rule that sends everything to the wildcard route:
    1. Name the rule wildcard
    2. Add a criteria: predefined field From, search type Wildcard, value is *
    3. Add an action: Send using route to *
  5. Under Advanced, IP Ranges, add a range called intranet or similar:
    1. Set the lower and upper IP ranges to encompass your intranet (eg 192.168.1.0 to 192.168.1.255)
    2. Set the priority to something greater than 15
    3. Under Other, uncheck Anti-spam, Anti-virus
    4. Under Require SMTP authentication, uncheck all of the boxes
  6. Save everything
It was also necessary to fill in the SMTP -> 'Delivery of email' tab (using the same connection settings as above). This part seems to be omitted from the guide above.

None of the script examples I found were doing exactly what I needed so I wrote my own, to simply detect and re-write the relevant From information.

(Simply add more if...then statements for additional email accounts).

Code:
Sub OnAcceptMessage(oClient, oMessage)

      If (InStr(oMessage.FromAddress, "alerts@cltd.co.uk") > 0) Then
       oMessage.From = "Contoso Alerts" & "<" & oMessage.FromAddress & ">"
        oMessage.save
      End If
 
     If (InStr(oMessage.FromAddress, "backups@cltd.co.uk") > 0) Then
       oMessage.From = "Contoso Backups" & "<" & oMessage.FromAddress & ">"
        oMessage.save
      End If
 
     If (InStr(oMessage.FromAddress, "Contoso.invoices@cltd.co.uk") > 0) Then
       oMessage.From = "Invoices - Contoso Ltd." & "<" & oMessage.FromAddress & ">"
        oMessage.save
      End If
 
     If (InStr(oMessage.FromAddress, "scanner@cltd.co.uk") > 0) Then
       oMessage.From = "Contoso Scanner" & "<" & oMessage.FromAddress & ">"
        oMessage.save
      End If
 
     If (InStr(oMessage.FromAddress, "Contoso@cltd.co.uk") > 0) Then
       oMessage.From = "Contoso Ltd." & "<" & oMessage.FromAddress & ">"
        oMessage.save
      End If

End Sub

Essentially, an email containing (for example) a FromAddress of alerts@cltd.co.uk will have the From property of 'Contoso Alerts <alerts@cltd.co.uk>' added before it is relayed on to the O365 SMTP server, resulting in the sender being displayed to the recipient as 'Contoso Alerts' instead of alerts@cltd.co.uk.

One limitation is that, internally (ie to O365 users on the same domain), the emails appear to be from the main account (not the alias).


Also, it seems it's not possible to modify the 'Reply-To' information (which, when delivered, contains the main account address, rather than the alias) but that doesn't seem to matter. I'm not sure why but it seems the Reply-To address is ignored anyway when replying to these emails.
 
Last edited:
Back
Top