Exchange Online Mail Flow Rules for Aliases
I ran into a somewhat annoying issue with using Mail Flow Rules with Exchange Online to modify the subject header on emails that were sent to aliases on some of my Office 365 accounts. I wanted to modify the email subject of those emails to show what alias it was meant for, and I figured I should share that process here if someone else runs into the same issue. This isn’t something that I do very often, and I was doing it wrong from the beginning, hence why I was having an issue.
Creating these Mail Flow Rules is not a difficult task to perform, but if you use the wrong settings in the rule creation process, then it will not work exactly as intended. Just for reference, I still use Office 365 and Microsoft 365 interchangeably in a lot of cases. I will just add it to the list of branding choices by Microsoft that I don’t always agree with or follow and knowing Microsoft they will just change it again in the future.
Table Of Contents
I have spent a lot of time over the last few months trying to consolidate a lot of my online services, both on my personal accounts and on my business-related accounts. This was for several reasons, but it was mostly to reduce the complexity of maintaining multiple accounts and to simplify access, but also to save some money on services. I have been using Office 365 for my Ten Fifteen Solutions business for the last few years, as well as on another unrelated Office 365 tenant that I also maintain for other purposes.
While I like the functionality of Office 365 and all the services that come with it (especially Teams), the cost has gotten a bit out of control since I started using it and I feel like I needed to consolidate those costs. Email aliases are a convenient way to assign a lot of rarely used email addresses onto a single account and simplify a lot of things.
An email alias is a way to add an “alternate” email address to a primary email account. Aliases are very useful for several reasons which can include:
- Allowing for a new domain name if a company changed names but want to retain the old company domain name during the transition.
- Allowing for a company that was acquired to be absorbed into the new company and allowing for the old domain name during the transition.
- Adding misspellings for users that have names which are commonly misspelled or have changed their name.
- Adding multiple addresses to common services, such as the ones in RFC 2142.
- Adding subdomains for specific purposes (such as internal domains, filters, etc.).
There is another way to handle email aliases on email servers, and that is with the “catch-all” email address. This is a setting on an email server that forwards all “unknown” email addresses to a specific user or account. When something like this is setup, any emails sent to a domain will be sent to a specific account, regardless of who the recipient is. I personally do not like this setting for an email server, as it can easily flood an email server with unsolicited email. Exchange Online does not actually support this setting without a lot of hacks (at least as of early 2022), and even if it were supported, I would not use it for the security implications.
Exchange Online Mail Flow Rules
Mail Flow Rules (formerly known as Transport Rules in on-premises Exchange) are rules that are applied on an email domain that can identify and perform actions on emails that flow in and out of an organization. They are similar to regular Inbox rules that are applied in Outlook, except they are applied to all email rather than an individual mailbox and support many more options.
They support multiple options and conditions, but have many purposes such as:
- Redirecting emails to other users.
- Applying filters.
- Adding disclaimers to emails.
- Modifying the email subject.
- Dropping emails from certain domains.
- Flagging emails as originating from outside the organization.
Configuring Mail Flow Rules is a straightforward process, and they can be configured through PowerShell or through the Exchange Online Portal.
For this post, the following Mail Flow Rule will be setup for the following user:
I have already setup several aliases for that user, which are specified in RFC 2142:
These 15 aliases will be setup with 3 different Mail Flow Rules so that when an email is sent to them, the subject line will be modified to show the alias that received the email:
The email subject line name is based on the categories outlined in RFC 2142.
Creating a Mail Flow Rule for Aliases
In this example, a Mail Flow Rule named MJCB RFC 2141 - Business will be created for the INFO, MARKETING, SALES, and SUPPORT aliases for the matthew[at]mjcb[dot]io email address. The subject line for any emails that are sent to those aliases will be prepended with [MJCB Business].
The fastest way to setup a Mail Flow Rule is to use PowerShell instead of using the Exchange Online web interface. The current method of connecting to the Exchange Online service is to use the EXO V2 PowerShell module, which can be downloaded from Microsoft.
If you would prefer to create the Mail Flow Rule using the Exchange Online web interface instead of with PowerShell, those instructions can be found later in this post.
Use the following commands to use the EXO V2 PowerShell module for connecting to Exchange Online (you will need to install this first). You will need to replace <UPN> with your credentials:
Connect-ExchangeOnline -UserPrincipalName <UPN>
Once connected to Exchange Online, create the Mail Flow Rule by running the following command:
-Name "MJCB RFC 2141 - Business" `
-HeaderMatchesMessageHeader To `
-HeaderMatchesPatterns "firstname.lastname@example.org","email@example.com","firstname.lastname@example.org","email@example.com" `
-PrependSubject "[MJCB Business] " `
Exchange Admin Center Configuration
The Exchange admin center is offered with a new and updated user interface, as well as the classic interface. The Mail Flow Rules interface is the exact same regardless of which admin center that you use, the only difference is where it is located within the interface. In these instructions, Step 3 will specify the difference in accessing the Mail Flow Rules configuration.
- Open the Microsoft 365 Admin Center.
- Under the Admin centers menu on the left side of the screen, select Exchange to access the Exchange admin center:
- If you are using the New Exchange admin center, go to Mail flow -> Rules in the menu on the left side of the screen. If you are using the Classic Exchange admin center, click on mail flow in the menu on the left side of the screen and select the rules tab (it should be selected by default). The remainder of the configuration is the same regardless of which interface is used, which is shown below:
- Click on + button and select the option to Create a new rule….
- The new rule window will appear. At the bottom of the new rule window, click the link for More options…, which will show more advanced options for rule creation.
- Enter a name for the rule in the Name field.
- Under Apply this rule if… drop-down list, select A message header… -> matches these text patterns.
- Two options will now appear beside the drop-down list, Enter text… and Enter text patterns….
- Click on the Enter text… option and in the specify header name window, enter To and click the Save button.
- Click on the Enter text patterns… link and in the specify words or phrases window, enter the aliases. Click the + icon to add each email address to the list and click the OK button when finished.
- Under Do the following… drop-down list, select the Prepend the subject of the message with… option.
- On the specify subject prefix window, enter [MJCB Business] and click the OK button. Ensure that there is a space after the bracket to ensure that there are no issues with readability.
- Click the Save button to finish adding the Mail Flow Rule:
The Mail Flow Rule how now been created and should be visible in the list of rules that have been configured:
If an email is sent to any of the aliases specified within it, should have the subject name modified.