How to Create Mail Flow Rules With Exceptions

December 26, 2023
12 min read

In this four-part series, I discuss the elements of a mail flow rule—available in the Microsoft 365 Exchange admin center (EAC)—and explain how you can implement them in your organization.

It is recommended that you read these articles in the order they have been written.

In my second article of this series, I explained how to create mail flow rules from scratch without any exceptions. Now I’ll focus on exceptions to the mail flow rules. They override the rule under specific circumstances; they also help you specify which messages to exclude or exempt from the rule being configured.

To understand exceptions better, I’ll walk you through a use case and create a mail flow rule that mitigates the issues arising from it.

Use Case

Let’s assume an organization’s new hires are subjected to phishing attacks where the attacker, posing as a member of the HR team, requests personal information. The attacker has HR’s details (like display name) and is using an email address [noreply123XXX@w4l0XX.onmicrosoft.com] very similar to the email address your HR team uses [noreply@w4l0s.onmicrosoft.com] for sending emails that don’t require a response. Now how would you stop this attack?

Of course, you have to increase your organization’s security measures. But as an immediate workaround, you can also create a mail flow rule where you can personally monitor these emails and let only legitimate emails pass through to your new hires.

The rule should be configured such that the mail passes through to new hires if and only if the mail is received from the legitimate email address. This means you have to set the legitimate email address as the only exception that lets the emails go through. The rest of the mail flow rule (conditions and actions) should work towards diverting the mail to you for inspection.

Let’s see how to implement this rule.

Figure 1 shows a snapshot of the mail flow rule that I am going to configure.

This screenshot shows how you can configure a mail flow rule that prevents phishing attacks by setting up the phishing email address as one of the conditions and the legit email address as the exception.
Figure 1: The mail flow rule that will be configured in this demo. | Used with permission from Microsoft.

Preliminary Notes

Before getting started, keep the following in mind:

Prerequisites

You need to have either a global administrator or Exchange administrator role.

Setting the Message Header to Match the Malicious Address (to Stop Malicious Emails from Reaching New Hires)

To set your mail flow rule’s condition as Apply this rule if the message header contains the from address as <malicious email address>, configure the message header by doing the following.

From the Apply this rule if* dropdown, select The message headers option, as shown in Figure 2.

This screenshot explains how you can configure a mail flow rule where you select the message headers value from Apply this rule if dropdown.
Figure 2: Configuring the mail flow rule condition. | Used with permission from Microsoft.

To set The message headers value, select includes any of these words as shown in Figure 3.

This screenshot shows how you can set the 'message headers' value to 'includes any of these words' option for a mail flow rule.
Figure 3: Setting the message headers value. | Used with permission from Microsoft.

Select the Enter text link, as shown in Figure 4,  

This screenshot shows how you can select the Enter text link for specifying the header name while configuring a mail flow rule.
Figure 4: Selecting the Enter text link. | Used with permission from Microsoft.

Set the header name as from, as shown in Figure 5, and then save it.

This screenshot shows how you can specify the header name while configuring a mail flow rule and save it.
Figure 5: Specifying the header name. | Used with permission from Microsoft.

Select the second Enter text link, set the header value as noreply123XXX@w4l0XX.onmicrosoft.com (the malicious email address), select Add, and then save it, as shown in Figure 6. 

This screenshot shows how you can specify the words or phrases the mail flow rule should to look out for and save it.
Figure 6: Specifying the words or phrases to look out for. | Used with permission from Microsoft.

Your mail flow rule should now look like the one shown in Figure 7.

This screenshot shows the mail flow rule being configured with condition that sets 'the message header' as 'from' and 'includes any of these words' option as 'noreply123XXX@w4l0XX.onmicrosoft.com'.
Figure 7: The mail flow rule should look like this now. | Used with permission from Microsoft.

Note: Just to be safe, I’ll add another condition that looks for emails coming from outside the organization because emails from your HR team would be considered internal emails.

Select the + button. Doing so opens another condition setting, as shown in Figure 8.

This screenshot shows how you can add another condition to the mail flow rule being configured by using the And operator.
Figure 8: Adding another condition to the mail flow rule. | Used with permission from Microsoft.

Select The sender option, as shown in Figure 9.

This screenshot shows how you can select or set 'the sender' as one of the conditions for the mail flow rule being configured.
Figure 9: Selecting The sender as the mail flow rule condition. | Used with permission from Microsoft.

Select The sender value as is external/internal, as shown in Figure 10.

This screenshot shows how you can set 'the sender value' as 'is external or internal' as one of the conditions for the mail flow rule being configured.
Figure 10: Setting The sender value as is external/internal. | Used with permission from Microsoft.

From the select sender location dropdown, select the Outside the organization option and save it, as shown in Figure 11.

This screenshot shows how you can set the sender value as 'outside the organization' as one of the conditions for the mail flow rule being configured.
Figure 11: Selecting the sender location as Outside the organization. | Used with permission from Microsoft.

Your mail flow rule should look as shown in Figure 12.

This screenshot shows mail flow rule being configured with two set conditions: i) from  address includes noreply123XX@w4l0XX.onmicrosoft.com and ii) sender is not in organization.
Figure 12: Configuring the mail flow rule with two set conditions. | Used with permission from Microsoft.

Configuring the Action of Forwarding the Malicious Emails to Yourself for Inspection

To configure the action of forwarding the emails coming from the malicious email address to yourself for further examination, follow these steps.

From Do the following dropdown, select Forward the message for approval option, as shown in Figure 13.

This screenshot shows how you can select or set the 'Forward the message for approval' option as the action for the mail flow rule.
Figure 13: Selecting the Forward the message for approval option as the action. | Used with permission from Microsoft.

For the Forward the message for approval value, set it as to these people, as shown in Figure 14.

This screenshot shows how you can set the 'Forward the message for approval' value to 'to these people' as the action for the mail flow rule.
Figure 14: Setting the Forward the message for approval value as to these people. | Used with permission from Microsoft.

Select the user who is going to monitor these malicious emails, and then save the rule setting, as shown in Figure 15.

This screenshot shows how you can select the people responsible for approving the mail as the action of the mail flow rule action.
Figure 15: Selecting the user to whom the mail gets forwarded for approval. | Used with permission from Microsoft.

The mail flow rule should look as shown in Figure 16.

This screenshot shows the mail flow rule for preventing phishing attacks being configured with two conditions and an action.
Figure 16: Mail flow rule configured with two conditions and an action. | Used with permission from Microsoft.

Configuring the Exception That Lets Emails from the Legitimate Email Address Go Through to New Hires

To configure the exception for this rule, which lets emails from the legitimate mail address go through directly to new hires, follow these steps.

From the Apply this rule if dropdown, select The Message Headers option, as shown in Figure 17.

This screenshot shows how you can set the message header while configuring an exception to the mail flow rule.
Figure 17: Configuring an exception to the mail flow rule. | Used with permission from Microsoft.

For The message header value, select includes any of these words, as shown in Figure 18.

This screenshot shows how you can set the message header value to 'includes any of these words' option while configuring an exception to the mail flow rule.
Figure 18: Setting includes any of these words as the value of the message header. | Used with permission from Microsoft.

Select the Enter text link and set the header name as from, and then save it, as shown in Figure 19,

This screenshot shows how you can set the message header to 'from' while configuring an exception to the mail flow rule.
Figure 19: Setting the message header parameter as from. | Used with permission from Microsoft.

Select the second Enter text link and set the header value as noreply@w4l0.onmicrosoft.com (the legitimate HR email address), select Add, and then save it, as shown in Figure 20.

This screenshot shows how you can set the message header to 'from' while configuring an exception to the mail flow rule.
Figure 20: Specifying the from email address. | Used with permission from Microsoft.

Your mail flow rule should now look like the one shown back in Figure 1, with the configured conditions, action, and the exception ready to implement once you configure the rule’s settings.

Note: To configure the mail flow rule’s settings, read How to Create Mail Flow Rules from Scratch Using the Microsoft 365 Exchange Admin Center and Windows PowerShell.

Configuring the Same Rule With PowerShell

To create the same mail flow rule using Windows PowerShell:

  1. Run your Windows PowerShell as an administrator.
  2. Connect to Microsoft 365 Exchange Online using the Connect-Exchange cmdlet. Read How to Connect to Microsoft 365 Exchange Online With PowerShell for more information.
  3. Run the following, as shown in Figure 21: New-TransportRule "Preventing Phishing Attack" -From <noreplyxxx@xxx.contoso.com> -FromScope NotInOrganization -SetAuditSeverity 'High' -ModerateMessageByUser <admin@contoso.com> -ExceptIfFrom <noreply@contoso.com>.
This screenshot shows how you can configure a mail flow rule to prevent phishing attack using Windows PowerShell.
Figure 21: Using Windows PowerShell to create a mail flow rule for preventing a phishing attack. | Used with permission from Microsoft.

How Does the Script Work?

Let’s break down the script:

  1. Use the New-TransportRule cmdlet for creating the mail flow rule.
  2. Pass in the name of the rule within double quotes, as shown earlier in Figure 21.
  3. Set the -From parameter value to noreplyxxx@xxx.contoso.com.
  4. Set the -FromScope parameter value to NotInOrganization.
  5. Set the -SetAuditSeverity parameter value to High.
  6. Set the -ModerateMessageByUser parameter value to <admin@contoso.com>
  7. Set the -ExceptIfFrom parameter value to <noreply@contoso.com>

To find out whether the rule has been successfully created, run the Get-TransportRule <name of the rule> command.

Note: (3) and (4) are conditions; (5) and (6) are actions; (7) is the exception.

Possible Errors You Might Face

Here are some possible errors you might face and ways to rectify them:

  • Whitespace in your mail flow rule’s name: Ensure you name your mail rules without any whitespace between them, as this can throw errors. Remember that whitespace causes errors only when you are creating your rules using the Microsoft 365 Exchange admin center, not when you are using PowerShell.
  • Typos in your message header and message header value: Ensure you get the message header and its value right. You should enter them exactly as it has been provided in this article. If not, you won’t be able to save/run your rule successfully.
  • Not running your shell as administrator: Select the PowerShell program from the Windows start menu and choose Run as administrator, as shown in Figure 22.
This screenshot shows how you can run your Windows PowerShell in Administrator mode.
Figure 22: Running PowerShell as Administrator. | Used with permission from Microsoft.
  • Execution Policy set to restricted mode: Execution policy setting determines the script execution rights for the current user. If you are logging in for the first time, chances are this is set to ‘restricted’ mode. Ensure the execution policy is set to RemoteSigned (which allows you to execute your scripts) by running the Set-ExecutionPolicy RemoteSigned command.

         Note: To check your current script execution rights, run the Get-ExecutionPolicy command.  

  • Typos in your PowerShell script: You can run into typos even while copying/pasting scripts. So, it’s best to save your scripts in separate .ps1 files and execute them. This not only saves time but also prevents your script from running into unnecessary errors.

Conclusion

This is the last article in this mail flow series. I began by explaining what Microsoft 365 Exchange mail flow rules are and implemented one using predefined templates. Later, I focused on how to create mail flow rules from scratch. Then the focus shifted to complex mail flow rules involving message headers. In this final part, I explained how exceptions can play a critical role in configuring mail flow rules.

Thilak Kumar Singh

Thilak Kumar Singh

Thilak Kumar Singh is a trainer who believes in teaching about IT in the simplest possible way with more emphasis on visual mode of learning. He firmly believes in democratizing IT knowledge, especially of Microsoft solutions, in which he specializes as a trainer and consultant.

He observes that in this era dominated by cloud computing and collaboration, where IT has reached users unimaginable hitherto, IT knowledge must not be an obscure science, but rather be available to anyone who wishes to engage with IT.