How to apply Outlook.com mail rules on the Junk Mail folder and delete email based on words saved in a txt file on OneDrive

Outlook Mail Rules cannot be applied to the Junk Folder. This was a bit frustrating for me because after several years of using Outlook.com for my private email, I get quite a bit of spam every day. The problem is that sometimes legitimate messages also ends up in this folder and is deleted after 10 days by default. This forces me to go through my spam mail folder every few days to check if there is no legitimate messages in it.

That’s why I came up with a solution with Microsoft Flow / Power Automate last year. With Microsoft Power Automate you can create triggers for almost every action/event in your mail box (and many more), and you can use the basic functions for free. I wrote this step-by-step blog about the flow that I had created.

There were many reactions to this blog (via commends below the blog and via email and social networks). A lot of people had the same problem and with that flow their problem was solved. It’s always nice to get positive comments on your blog.

Recently I also received a reaction from Tobias Wagner, he told me he had made a more advanced flow where the Junk Mail was filtered based on words that were saved in a text file on his OneDrive! This way you can easily add words to the spam filter by simply adding the words to the text file on OneDrive and without changing the flow itself!

Tobias Wagner was willing to share his flow with me, and I got his permission to share it with you via my website. So big props for Tobias, all credits for this blog are for him! #CommunityRocks #CommunityPower

I will guide you step-by-step how to create this flow.

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 a folder.

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://flow.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).

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 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.

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”, and again many thanks to Tobias Wagner for sharing this information with me!

22 comments

  • Thanks for this guide, but one of the steps appears different to the current workflow version. The eachRow step doesn’t provide an option to add a dynamic expression. It only provides an option to insert a parameter from a previous step. Would you be so kind as to check and update the procedure accordingly? Many thanks in advance!

    • Found out that resolution seems to play a role whether or not you see the dynamic expression prompt. I ran into the same thing. Either enlarge your browser window or change the zoom rate of your browser. That worked for me.

      • Yes I encountered this too – it’s a weird bug. But enlarging the browser to full screen fixed it for me too. Then the little blue ‘Add Dynamic Content’ button shows up no problem.

  • Hi Robin,

    great post but as Outlook.com doesn’t allow a user to import a flow, only a few people will be able to re-create this flow by hand step-by-step. Really pity.
    Thank you and good luck!

  • Holy Cow Robin! Thank you so much for publishing this. Can’t believe Microsoft can’t simply filter out Clubbulk.live, OnePeice.live or Utopup.live. That would eliminate 90% of the crap. It’s so cool that you can monitor the successes or failures history and ensure it’s working correctly. And figure out the keywords that seem to be predominate in the email.

  • Huge thanks to Robin and Tobias for this clever fix! I’ve implemented it and for the first time in years my hotmail/outlook junk folder is easily managed and I can retrieve legitimate email without having to look through tons of annoying junk. The directions were perfect and easy to follow. It’s so easy to update the test file.

  • I’m trying to test the keyword “Coinbase” for the body with a spam message but it won’t work? If I send myself a email with the term Coinbase in the body and manually place it into the spamfolder it triggers. It won’t trigger with a original spam email that contains the term Coinbase in the body.

  • This power automate flow works 98% of the time, but then once in a while I get this error message.
    “Unable to process template language expressions for action ‘Condition’ at line ‘1’ and column ‘25842’: ‘The template language function ‘toLower’ expects its parameter to be a string. The provided value is of type ‘Null’. Please see https://aka.ms/logicexpressions#toLower for usage details.’.”
    I double checked my flow against what is listed above and it matches. I have tried to search for this exact error message and I can’t seem to find any info on it. The flow error details page is vague, but it does recommend this which I have not had time to do yet.
    “This is a new issue. Try posting about it on our community page—somebody may have a fix. ”
    Any ideas Robin?
    Any Ideas?

    • I should add, this error is being flagged under the condition within the blackListLoop. I have 4 conditions with “OR”, Body, Subject, ReplyTo and From.

  • I got the same “Test connection failed. Details: The tenant for tenant guid does not exist. Only work or school accounts are supported” error others received in the previous post when I used the “Office 365 Outlook” connector. Switching to the outlook.com connector fixed it. It’s easy to miss since everything else matches (“When a new email arrives” / “Delete email” / etc) and sometimes you have to scroll pretty far to find the outlook.com one. I found searching on outlook.com made it easier to find the right one.

  • Werkt perfect! Bedankt voor deze handige tutorial. Enige verschil is dat ik als postbus postvak-in gebruikt heb en de e-mails met de spam-kernwoorden naar de ongewenste mailbox verplaatst worden. Keep up the good work!

  • Absolutely brilliant.
    I used the original method very successfully for over a year before the spammers changed their method.
    This new Flow routine, while being more difficult to implement (write), works very well and just as importantly it looks to have a large degree of future proofing.
    Especially good is the fact that you don’t have to delve into the routine to add another spam style, instead you just edit a simple notepad file.

    Prior to finding your site I was receiving over 20 spam mails in junk every day.

    A big thank you to you and to Tobias

    Kieran

  • Is there a way to create a flow that moves messages from specific senders by name and not by email address? I am receiving a lot of spam from the same sender names as “Huusk”, “Earwax Cleaner”, etc, but the spammers change the email addresses all the time. It looks like the filter “From” on the Microsoft Flow app only filters by the email address. I say this because I created a flow following the steps on your other post and is not working (https://www.robinhobo.com/how-to-apply-outlook-com-rules-on-the-junk-folder-and-how-to-stop-outlook-com-from-moving-emails-to-junk-or-spam-folder/#respond)

    • So I just used this exact technique to address those kinds of emails. My issue is like yours with specific ‘names’ like ‘Huusk’, etc and changed my flow to only check the from field. In my BlackListLoop I have one condition check, and that is “toLower(triggerOutputs()?[‘body/From’])”. I dont even bother with subject or the full email body anymore as they are written so generically these days.

    • I am having the same issue. I just set mine up using “From” and put the sender names using “Earwax Cleaner”, “Zippy Loan”, etc… I will let you know how it turns out.

  • This was the fist time I ever set up a flow and I love it. I am wanting to set up another one but I have a question. I have two emails that I use (one for personal stuff and one for business). Would it be possible to create a flow using one email address but then using the One Drive account associated with the other email address? Thanks!

  • Hi, I’m so glad that I found this blog post. I have been wanting a way to filter out my email junk folder on my business email. I have a couple of questions and I’m hoping someone can help me before I try and make my first flow. #1..I have two emails that I use (one for personal and one for business). I’m wanting to put the flow on my business email but my OneDrive account is associate with my personal email address. Would I be able to put the flow on the business email but log in to my personal onedrive to assess the txt file?. If not, that brings me to my next question. Could I Just store a txt or excel file on my desktop and then when setting up the flow for my business email I could just link the flow to the desktop file?

    Thanks in advance for any and all help!

    Sorry if this double posts. It shows that it posted and then when I refresh it goes away.

    • Yes, you can use multiple accounts to link to. And secondly you can link to files that are stored on OneDrive. If your Desktop is redirected to OneDrive you can store a file there since you can configure a file path in the connection with OneDrive.

  • Thanks for your approach! Newsletter spam is now no problem.

    However, there is another type of spam that bothers me. A lot of spam has no sender, so what to do with this kind of spam.

    I do not want to receive no sender email on my outlook.

About Robin Hobo

Robin Hobo

I work as a Senior Solution Architect with focus on the Modern Workspace. I am specialized in Azure Virtual Desktop (AVD), Windows 365 and Microsoft EM+S (including Microsoft Endpoint Manager - Microsoft Intune).

For my full bio, check the About Me page. You can also join me on the following social networks:

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close