From Chaos to Clarity: Revolutionize Your Inbox with Power Automate

Ethan Guyant
Power Automate
12 min readAug 19, 2022

--

Photo by Alvaro Reyes on Unsplash

Escape the email madness! Discover how to use Power Automate to declutter your inbox with effortless automation. Take back control of your inbox!

Power Automate offers a wide range of connectors for various services allowing for the automation of certain tasks. Managing and organizing emails is a common repetitive task that can take a fair amount of time and manual effort, and let’s face it’s not exciting work. Thankfully, Power Automate and the Outlook in Office 365 connector can provide some relief.

When searching the Power Automate connectors and actions for Outlook there is an Office 365 Outlook and an Outlook.com option. Microsoft recommends when using a work or school email account use Office 365 Outlook and when using a personal account use the Outlook.com connector

This article will provide an overview of the Outlook in Office 365 connector and some examples of how it can be used to conduct an Inbox Cleanup task.

Office 365 Outlook Actions

The Office 365 Outlook connector offers several actions or events that the Power Automate workflow could do — List of Office 365 Outlook Actions — below shows just a partial list.

Automating an Outlook inbox cleanup task will focus on the following actions.

Get emails (V3)

This action will retrieve emails from a folder (e.g. Inbox). This action can be configured to fetch both read and unread emails.

Move email (V2)

This action will move an email to the specified folder, within the same mailbox.

Using the low-code interface the specified folder must be selected, to dynamically set the destination folder based on the characteristics of the email the folder ID must be specified, see below for details.

The Basics

To get started the workflow will fetch up to 10 emails, and process them based on the Read status. Unread emails will be moved to the Unread folder and Read emails will be moved to the Read folder.

Flow Diagram

Power Automate Flow

To start the Power Automate flow the trigger is defined, here it is a manual trigger, but could be scheduled. Following this, a noEmails variable is defined to be used to exit the Do until loop which will continue to fetch 10 email batches from the inbox until there are no more emails ( noEmails = True). After initializing the variable the Do until flow control is added.

Within the Do until loop, there are 3 main events:

  1. The Get emails action retrieves the top 10 emails from the Inbox. To configure this action, select the folder to retrieve the emails from, and then for this flow the Fetch Only Unread Messages was set to No and the Top option was set to 10 (pictured above in the Get emails (V3) section).
  2. Determine if there are emails to process and set the noEmails variable to the appropriate True or False value. The set variable value is determined by the expression: if(equals(length(outputs('Get_emails_(V3)_from_Inbox')?['body/value']),0), true, false)
  3. The For Each Email loop iterates over each of the retrieved emails and moves the emails to the appropriate folder based on the email's read status.

The For Each Email control structure applies the actions contained within it to each of the emails retrieved by the Get emails action.

Limitations

A limitation of the above flow is that the destination folder for the Move email actions had to be explicitly selected. This will likely be fine if there are only a few potential mailbox folders, however, can easily become unmanageable when the number of potential Inbox folders grows.

This limitation can be addressed by using Id:: <folder_id> for the Folder option within the Move email action. This solution comes with two main points to address:

  1. Where to find the Outlook folder IDs
  2. How to store/search for the folder IDs based on specific email characteristics (e.g. from specific people or domains).

Outlook Inbox Cleanup Workflow

The first example shown in this article explicitly selected the destination folder, in this example, the folder display names and IDs will be stored in an object variable. The workflow will then process the email, identifying which property from the object variable should be retrieved. Then the returned value (the folder ID) will be used in the Move email action. In this example, an object variable was used to store the display name and folder IDs, a Microsoft List could also be used. Retrieving the folder ID using this option would require a Get items action with the appropriate filter query.

Workflow Diagram

Outlook Folder Setup

The outlook mailbox used by this flow has the following folders to which the emails will be moved from the inbox. Internal emails will be moved into the Departments subfolder based on the sender’s department. External emails will be moved into the External subfolder based on the sender’s domain.

Get Folder IDs

Getting the folder IDs of an Outlook mailbox folder can involve using the Microsoft Graph API or using the peak code setting within Power Automate. Using Microsoft Graph can be a bit more advanced but still approachable when manually retrieving the folder IDs using peak code is unmanageable.

Peak Code

This method requires manually identifying each destination folder’s ID and adding it to the object variable. This can be an appropriate method when the number of destination folders is low and does not change frequently. The organizing of emails in this example primarily focuses on sorting into department folders which can be a good use case for this method because the number of departments in an organization is likely to be relatively stable.

The Move email action, shown below, shows that the folder is selected (HR).

After selecting the destination folder, in the upper right of the action select the ellipses and then Peak code.

This will reveal the inputs to this action. Under the parameters property, the folderPath can be seen. What is required to be stored for this flow is the entire folder path following Id::, this ID and the folders display name are what is stored and accessed by the workflow. See the Power Automate Flow - Variable section for an example.

Microsoft Graph

When using the Microsoft Graph approach Graph Explorer is a helpful tool. This method can also be used to get the display names and IDs of child folders. See the Power Automate Flow — Microsoft Graph section for an example.

Power Automate Flow — Variable

Again the workflow is started with a manual trigger but could be set to a scheduled trigger. The first few actions of the flow initialize four variables utilized by the flow: noEmails, folderIDs, propertyName, and sorted.

  • noEmails: boolean variable used to exit the Do until loop which continues to retrieve emails from the Inbox until noEmails=True
  • folderIDs: an object variable that stores a folder’s display name (key) and folder id (value)
  • propertyName: string variable used to get the corresponding folderIDs from the folderIDs object
  • sorted: boolean variable used to determine if the current email has been sorted by a prior step

The Do until noEmail=True is a Do until flow control action which will continue to process and sort emails in the Inbox until there are no more emails to be processed. This contains the same three actions that were described above Get emails, determine if there are emails to process, then process each email. The For Each Email loop is where a majority of the Inbox management actions occur.

The For each loop contains 4 main steps:

  1. Get the current email object, this will make attributes of the email accessible through dynamic content
  2. Set the sorted variable to False and split the From email address into the domain and the user id (everything after @ and everything before @)
  3. Move Unread emails into an Unread folder. These will be moved back to the Inbox after all emails have been processed
  4. Check if the email was sorted into the Unread folder and if not evaluate email characteristics and move the email to the appropriate folder

Action Step #1

The first step gets the current email and makes the properties of the email accessible through dynamic content.

Action Step #2

This step consists of setting the sorted value and two Compose data operations which splits the From email address into two parts, before the @ and after. The output of these two actions will be used to determine if the email came from an internal source which will be sorted by the sender's department or an external source which will be sorted by domain.

The inputs expressions used are:

  • Compose — Domain: last(split(outputs('Get_email_(V2)_-_Current_Email_Object')?['body/from'], '@'))
  • Compose — User Id: first(split(outputs('Get_email_(V2)_-_Current_Email_Object')?['body/from'], '@'))

Action Step #3

This set of actions uses a Condition flow control to check the Is Read status of the current email. If False the email is moved to an Unread folder to be moved back to the Inbox folder later in the flow and sorted is set to True. If True no action is taken and the flow moves to the next set of actions.

Action Step #4

This step carries out the processing and sorting of each of the read emails. First, the flow checks the value of sorted, if sorted equals True no additional actions are taken, if False the email is sorted based on the attributes of the email.

If the above condition is met the email is sorted into a department folder by searching for the user profile of who sent the email using the Office 365 connector and setting the propertyName variable equal to the department attribute. Then the folder argument of the Move email action is set to:

variables('folderIds')?['departments'][variables('propertyName')].

It is important to note that the folder ID in the Move email actions is preceded by Id::

If the above condition is not met, the email is sorted by domain. This sorting involves three steps. First, a compose action is used to get the folderID of the corresponding domain folder with the inputs argument set to:

variables('folderIds')?['domain'][variables('propertyName')]

This is then followed by two Move email actions that have different configure after run settings. The Sorting Needed action is configured to run only when the compose Get propertName action fails (i.e. a property matching the domain does not exist in the folderIDs variable) and the email is moved into a Sorting Needed folder to be manually sorted.

The following External Sorting action is configured to run only when the Sorting Needed action is skipped (i.e. the Get propertName action was successful). The folder argument of the Move email action is set to the same expression as the above Get propertyName action.

After all emails in the Inbox have been processed the flow retrieves all emails that were moved to the Unread folder and moves them back to the Inbox.

Limitations

A main advantage of the approach is that the destination folder can be dynamically set without nesting Condition flow controls. However, this approach may become unmanageable if the destination folders change or if new folders are frequently added. To continue sorting into new folders would require a manual update of the folder name and ID storage source (variable or Microsoft List).

Power Automate Flow — Microsoft Graph

This approach has the same general layout as the previous flow, the only difference occurs within the Check if Internal Email flow control.

Here there are additional actions: Send an HTTP request, Parse JSON, and For Each Child Folder.

The Send an HTTP request Outlook action requires a URI this can be explored and created using the Graph Explorer tool. The generalized query used here is:

https://graph.microsoft.com/v1.0/me/mailFolders/<ParentFolderId>/childFolders.

The returned body of this action is then parsed with the Parse JSON action which allows properties of child folders (folders within Department and External) to be utilized as dynamic content.

The Schema of the Parse JSON action can be generated from a sample by testing the workflow before adding this action. Following the test, the output of the Send an HTTP request can be copied, then add the Parse JSON action and paste the output into the Insert a sample JSON Payload dialog box

After these actions, there is a For Each flow control that iterates through each child folder and compares the display name of the folder with the department of the sender. If the two are equal the folderID variable is set to the id of the corresponding folder.

The folderID variable is then used in the following Move email action in the same way as the prior flow example.

The False branch of the Check if Internal Email operates in the same way, with the only changes being there is no need to search for users (external email) and the <ParentFolderID> in the Microsoft Graph query is the folder ID corresponding to the External folder, rather than Department.

Summary

This article covered various approaches using Power Automate to automate Outlook inbox cleanup and management. The three approaches shown in the article covered a basic application of sorting emails into a Read and Unread folder. The main limitation of this flow is that the destination folder was explicitly selected and set. The following two approaches in the article covered two methods of addressing this limitation by using a variable object and querying the folders present using Microsoft Graph.

Check out these blog posts for other helpful Power Automate Guides:

If you enjoyed this article and found it helpful don’t forget to give it a clap, and follow the Power Apps & Power Automate publication!

Be sure not to miss a post by subscribing here, with each new post comes an opportunity to learn something new.

Eager for a deeper exploration? Consider venturing further by joining Medium, with a Medium membership you gain unlimited access to a world brimming with insights.

Originally published at https://ethanguyant.com on August 19, 2022.

--

--