
Working with email
The challenge of automating email has been around almost as long as email itself. As we mentioned at the beginning of the chapter, many solutions have been developed to address this over the years using technologies such as VBA scripting, Outlook rules, and COM add-ins. These solutions have required both programming skills to develop and a dependence on a desktop computer always running to be able to execute the commands.
With Power Automate and Microsoft 365, Outlook.com, or Gmail services, the necessary components are "always on," so you don't need to worry about power outages, software updates, or network connectivity issues disrupting a business process. In this section, we're going to look at options for handling incoming emails, processing attachments, and sending messages.
Receiving email
Many organizations use the receiving of an email (typically, to some sort of shared mailbox) as a sort of trigger to start a business process. This could be a sales order, a service ticket request, or a request for some sort of information. In this example, we're going to look at some of the filtering and selection options available when processing an incoming message.
To work with this flow, we're going to need an Exchange Online mailbox and a Microsoft Power Automate subscription. For this scenario, we're just going to look at messages we receive that could indicate they are related to an expense report. Follow these steps:
- The easiest way to start is to log into the Power Automate web portal (https://flow.microsoft.com), click + Create, and then select Automated flow.
- Enter a value for Flow name, and then search the triggers for When a new email arrives (V3). There are also triggers for When a new email arrives in a shared mailbox (V2) and When a new email arrives to a group. Any of them can be used, but you'll need to ensure the account you use as a credential has access to the shared mailbox or is a member of the group being monitored.
- Click Create.
- By default, the flow is going to connect to the Outlook mailbox for the currently logged in account. If you need to monitor a different mailbox, select the ellipsis (...) icon on the When a new email arrives (V3) step, and then click + Add new connection to provide the necessary mailbox name and credentials:
- Click Show advanced options.
- In the Subject Filter box, enter a value such as expense report:
The filtering options at this level are basic, but give you options to specify sender or recipient values that you can use as conditions to proceed with your flow. From here, you can add additional processing steps.
- When a new email mentioning me arrives (V2)
- When a new email mentioning me arrives (V3)
We'll take a look at some additional conditional filtering concepts in Chapter 8, Working with Conditions.
Handling attachments
The ability to save and manipulate attachments is a popular automating requirement. This type of request is frequently seen with mailboxes used to receive things such as purchase orders or expense reports. You can use Power Automate to save those items to OneDrive or a SharePoint Document Library.
In this example, we're going to put together a sample flow that does the following:
- Monitors a shared mailbox for messages that contain the words "expense report"
- Saves the attachment to a SharePoint Library called ExpenseReports
- Marks the email as Read
- Marks the email as Complete
To prepare for this example, you'll need the following:
- A SharePoint site to which you can save documents
- A shared mailbox to which you've been granted management permissions
With those requirements met, you can begin creating the flow.
Creating the flow
In order to configure this example, follow these steps:
- Log into the Power Automate web portal (https://flow.microsoft.com), click + Create, and then select Automated flow.
- Enter a value for Flow name, and then search the triggers for When a new email arrives in a shared mailbox (V2):
- In the Mailbox Address box, enter the address of the shared mailbox you wish to monitor:
- Click Show advanced options to expand the options.
- In the Only with Attachments box, select Yes. In the Include Attachments box, select Yes. These two options will configure the flow to only trigger if the message has an attachment and will make the attachment available for the next step:
- In the Subject Filter box, enter expense report. This will place a further constraint on the rule to only work if all three of the conditions are met.
- Click + New step.
- Search for Sharepoint Create file and select the Create file action for SharePoint:
- In the Site Address box, add the URL for the SharePoint site that will store the document.
- In the Folder Path box, click the folder icon to browse for a document library folder such as Shared Documents. In this example, we added a trailing forward-slash (/) and then used the dynamic content variable From to create a subfolder for each email sender.
- In the File Name box, select the dynamic content object Attachments Name.
- In the File Content box, select the dynamic content object Attachments Contents:
- Since Power Automate knows that an email message may have one or more items attached, it will wrap this action in an Apply to each function that will save each attachment separately.
- Click + New step.
- Locate Mark as read or unread (V3) (preview) and select it:
- In this step, select the Message Id box, and then browse to the dynamic content object Message Id. This will select the message that we're currently processing:
- In the Mailbox Address box, locate the shared mailbox.
- In the Mark as box, select Read. We want to mark this so that in the future if someone reviews the shared mailbox, they'll know this item has been read. However, this may not be enough, given that multiple people may access the mailbox and someone may accidentally change the status by just clicking on the message.
- Add another step. Search for Flag email (V2).
- In the Message ID box, select the Message Id dynamic content object.
- In the Mailbox Address box, locate the shared mailbox.
- In the Flag Status, select Complete. This will help verify that the message has indeed been processed:
- Click Save.
Time to test it out! Send a message to the shared mailbox meeting the requirements (an attachment and the subject of expense report). After you've submitted an email message, verify that the flow has completed successfully.
Verifying completion
To ensure that things have completed successfully, you'll want to review the output and verify it. You can use these steps to verify this particular flow:
- From the Power Automate web portal, you can select My flows, and then select the ellipsis next to the flow and click Run history:
- Select the run, and then look at the history. You can click on each step to expand and verify the values that Power Automate used during processing:
- To check the connected SharePoint site, navigate to the SharePoint site containing the document library that you specified when creating the flow. Verify that the folder structure and file exists:
- Next, you can open the shared mailbox that the flow was configured to use. Look for the message and verify that it has been marked as read and the flag column shows a checkmark:
At this point, you can be confident that your attachment handling flow is operating correctly. Next, we'll finish up the configuration by sending a confirmation email.
Sending email
Since sending things to a shared mailbox may seem like a bit of a black box, many organizations choose to send some sort of a confirmation email to let the sender know that the message has been received and processed. Sending an email is also a common task for Power Automate, and can be used in a number of circumstances, such as generating an email-based alert based on certain conditions or responding to a sender, like we're going to do in this example. The process is the same regardless.
For this task, we're going to re-use the Expense Report attachment flow and reply with a message that their file was received:
- Navigate to the Power Automate web interface (https://flow.microsoft.com) and select My flows.
- Select the Expense Report flow previously created, and then select the pencil icon to edit it:
- At the end of the flow, select + New step to add a new step.
- Since the Expense Reports flow is using a shared mailbox, select the Send an email from a shared mailbox (V2) action:
- In the Mailbox Address box, enter the value for the shared mailbox that will be generating the outbound message.
- In the To box, click the Add dynamic content + button:
- Under When a new email arrives in the shared mailbox (V2) section, select From to use the address of the original sender:
- In the Subject field, add a value that will be used as the subject of the outgoing message.
- In the Body field, add any applicable text. You can use dynamic content objects here, if desired, as well as the rich text editor to change fonts or text properties:
- If you want to set additional options such as fixed Cc or Bcc members, additional attachments, or message importance, select Show advanced options.
- Scroll to the bottom of the message and click Save.
At this point, the flow has been modified to send back a response message upon successful receipt and processing.
When modifying or performing iterative development on a flow, you may just want to use the option to resubmit or retry the last run with its parameters. You can use the My flows | <Flow> | Run history option to resubmit the last job and test it:
It's important to note that the run history shows the steps for the last completed run. When you re-submit, it will run the flow in its current state, so any new steps added will be included.
You can check the Sent items of the shared mailbox or the Inbox of the original sender's mailbox to see the email confirmation.