How to apply Outlook.com rules on the Junk folder with Power Automate
Robin Hobo
Cloud Endpoint Technology Specialist at Microsoft - Team lead | Windows 365 | Azure Virtual Desktop | Intune Suite
I use Microsoft Outlook.com for my private email for many years now, and over the years I receive more and more spam. In most cases Outlook.com redirects all spam to my Junk folder, which is good. But in some cases, Outlook.com also redirects legitimate messages to my Junk folder, and this force me to scan my Junk folder on legitimate messages every few days (default retention policy of Outlook.com let Junk items expire in 10 days).
If I find legitimate/”no-junk” mails between dozens of junk mails in my Junk folder, I can, for example, add the sender to the “Save sender list” so that it will not be forwarded to the Junk folder the next time. But I don’t what to check my Junk folder every few days, what I wanted to realize was to filter the Junk mail folder. Delete mail that contains certain keywords in the subject or body, and if a mail is legitimate that it will automatically be forwarded to my Inbox folder. As a second requirement I wanted an easily way to add new keywords to the filter.
I found the solution to realize this in Microsoft Power Automate. Everyone with a Microsoft Account (Outlook.com / Hotmail.com / Live.com) have access to the free version of it, and you can perfectly use it to apply Mail rules as I will show step-by-step below.
Step 1: Create a text file on your OneDrive
Open Notepad and save the (empty) file on your OneDrive. In my case I will save it in the root of my OneDrive but you can also save it in another folder of your choice.
Now add the words to the file for the spam filter. Emails that contains this words in the subject or body (depending on the flow configuration) will be deleted (in this case). Place a semicolon ( ; ) between every word of words (see example above) and save the file.
Step 2: Create the Flow
Open a browser and navigate to https://powerautomate.microsoft.com/ and login with your Outlook account.
On the left side, click Create and next, click Automated cloud flow.
Give the flow a name, in this case I will give it the name Spam Filter. Select the trigger When a new email arrives (V2) (Outlook.com). If you are using Microsoft 365 - Exchange Online select the V3 version.
Click Create
If this is the first time you create a flow with triggers to your email, you need to create the Connection first. To do so, click the Sign in button and login with your Outlook credentials.
Click the Folder icon on the right.
Select the Junk Email folder
Click Next step
Select the Build-in tab and select Variable
Select Initialize variable
Fill in the following information:
Name : Empty
Type : String
Value : <keep empty>
Click the 3 dots button in the top right corner and click on Rename
Give it the name EmptyString
Repeat this step for the following two Initialize variable:
Initialize variable name (rename action) : SearchTerm
Name : SearchTerm
Type : String
Value : <keep empty>
and
Initialize variable name (rename action) : InitTestResult
Name : TestResult
Type : String
Value : <keep empty>
Make sure you have all three the Initialize variable as shown below.
Click New step
Search for OneDrive and select Get file content using path
If this is the first time you use OneDrive with Flow you need to create the Connection first. To do so, click the Sign in button and login with your Outlook credentials.
Click on the Folder icon on the right hand side and select the “spamfilter” text file.
Rename this step to LoadWordList
Click New step
Open the Built-in tab and select Variable
Select Initialize variable
Create the following Initialize variable:
Initialize variable name (rename action) : ParseList
Name : eachRow
Type : Array
Click in the Value field
Open the Expression tab and create the following expression:
split(outputs('LoadWordList')['body'],';')
Click OK
Click Next step
Open the Built-in tab and select the Apply to each action.
Rename the action to blackListLoop. Click the Select an output from previous steps bar and select eachRow
Click Add an action
Select the Built-in tab and select Variable
Select Set variable
By Name, select SearchTerm. Click in the Value box and open the Expression tab. Create the following expression:
toLower(items('blackListLoop'))
Click Add an action
Select Condition
Click in the Choose a value box and open the Expression tab. Click on toLower(text). The expression toLower() is now set with cursor active between the parentheses. Don’t click OK, but open the Dynamic content tab.
If you want to filter on text in the body of the mail, select Body. The following expression is now created:
toLower(triggerOutputs()?['body/Body'])
Click OK
Select contains, and in de Choose a value box, select SeachTerm
You can repeat the steps for the subject if you want to filter on that too. The expression for the subject will be:
toLower(triggerOutputs()?['body/Subject'])
If you create two or more conditions, make sure you set the conditions to Or (see screenshot above).
In the If yes window, click Add an action
Open the Built-in tab and select Variable
Select Set variable
Choose the variable TestResult. Click in the Value box and select Current item
Click New step (outside the blackListLoop)
Select Condition
Rename the action to Result. Configure the action as follows:
TestResult – is not equal to – Empty
In the If yes window, click Add an action
In my case I want to Delete the email if one of the words from the file on OneDrive is in the subject or body of the email. Therefor I search for Outlook.com and select Delete email as the action.
In the Message Id box select Message Id
In the If no window, click Add an action
I want mail to move to another folder after the flow has been triggered (and no words have been found in the text file). This so I know it was not filtered by any words. Therefor I have created a “JunkMail NotFiltered” folder in my inbox. But you can also move it to your Inbox folder.
Search for Outlook.com actions and select Move email
In the Message Id field, select Message Id. In the Folder box browse to the desired target folder.
Finally, click Save
If everything is configured correctly, your flow looks like this:
Hope it will help you with your “spam problem”. You can use this same flow to filter email in your Inbox for example.
#PowerAutomate #CommunityRocks #Microsoft #Outlook #spamfilter #hotmail #flow #stopspam
This is amazing. Just needed the part of getting email out of this oversensitive spam filter. Thank you very much
Owner at Getty Computer Services, Getty Enterprises LLC
3 个月I was hoping that this would work for me, but Power Automate does not allow personal Microsoft Accounts. You must have a work or school account. ??
Enterprise Architect - Individual life insurance sector at iA Financial Group (Industrial Alliance)
9 个月Thank you. Exactly what I planned to do. A chance that I found your post. It saved me a lot of time ! Also, in order to share an improvment or idea, I added a step in the last Yes step in order to keep track of matched words. It add a line in an Excel table in order to keep track of what is going on. It will allow me to delete words that are never matched and also, maybe, to diagnose a mail that shouldn't have been deleted.
Owner, IvaSoft
10 个月This freeware flow is much easier to install: https://ivasoft.com/deleteitemsinjunkfolderflow.shtml
Tech Innovator & Strategist | Low-Code & Digital Transformation Expert | Consultant, Developer, System Architect & Pre-Sales Manager
1 年Anyone know a way to get the sender display name in the trigger output? It seems we can only get the email address? Robin Hobo For the persistent spam I'm getting (in the junk folder), that field would eliminate most of it. I've noticed a flood of spam from ".onmicrosoft.com" accounts recently. You can clear those with a trigger filter.