In this article, we will see how we can filter the incoming email based on subject or body content and extract the attachments in the mail for archiving or extending it for use with other lines of business by importing it to OneDrive.
The business use case is such that the Procurement team will send multiple invoices to the Finance and Marketing team which will have the Subject line in the Format – ‘Department: Finance’ Or ‘Department: Marketing’. Based on the received mail, the attachments must be moved to the corresponding Finance or Marketing folder for future processing
To implement the business use case, we will follow the below high-level steps
- Add the trigger when a new email arrives so that the flow gets started on the arrival of a new email. To filter the emails on which the flow must be run, we will be adding additional trigger conditions that checks for the presence of the specific keywords in the Subject line so that flow does not get triggered for the unwanted mails
- We will then check for the appropriate subject keywords and branch the logic and in each of the branches, we will get the attachments from the current mail using the Get Attachments action. We will then use the Create File action to upload the document to one drive
As the first step, we add the trigger for the flow and set a few basic conditions to run the flow only if the incoming mail has attachments as well as specify the Incoming mail address
We also add additional trigger conditions by going to the above actions setting and adding the below condition in the trigger field so that the flow will be triggered only if the subject has the Text Department AND contains the department text Finance OR Marketing. So the logical filter is Department AND(Finance OR Marketing) which is written as :
|@and(or(contains(triggerBody()?[‘subject’], ‘Finance’),contains(triggerBody()?[‘subject’], ‘Marketing’)),contains(triggerBody()?[‘subject’], ‘Department’))|
Thus, we have set up the trigger and the trigger conditions. Now we move on and add the condition check on the subject text. If the subject contains Finance, the logic would traverse to the left block or else to the right block which handles the marketing attachments
Within the Yes block in the left branch, we add the Apply Each loop and specify the Attachments output from the Trigger so that we can loop through all the attachments in the email. Within the loop, we also add the Get Attachment action and mention the below inputs as parameters :
- Message-ID: Dynamic Content ‘Message ID’ from the trigger output
- Attachment ID: Dynamic Content ‘Attachment ID’ from the trigger output
Followed by this, we add the Create File action and specify the Finance Folder Location so that the attachments can be created as new files here.
In the Create File action, we will mention the below input parameters for creating the file:
- File Name: Dynamic Content Output ‘Name’ from the Get Attachment action
- File Content: Dynamic Content Output ‘Content Bytes’ from the Get Attachment action
Same way, we can replicate the above steps in the Right block (No Block) where the marketing attachments will be handled. To easily transfer the steps, we can copy the loop to the clipboard and paste it AS-IS in the Right Block.
Within the Right block, click on add action, and from “My Clipboard”, select the previously copied apply each action
This will copy over the steps from the left block, and we have to just update the location to the right Marketing folder in one drive. All other steps remain AS IS.
Thus the overall flow will look as below:
Testing the Flow
So as to test the implementation let’s send 2 emails, one with Subject Line Department: Finance and another one with Department: Marketing.
The flow has run successfully and now if we head over to the respective One Drive locations, we can see that the corresponding invoice attachments have been extracted and placed in the Finance and Marketing folders
Finance Invoices :
Marketing Invoices :
Thus, we saw how to filter the trigger conditions and target specific mails for starting a flow and we also saw how we can extract the attachments conditionally and upload them to a business line of system like One Drive for archival or for using the documents for further processing