This use case was generated from an idea involving Xerox Docucentres, which enables end-users to email themselves a scanned document. Equally, this applies to regular emails with attachments, that you would like Power Automate to automatically save the attachment(s) to a specific (custom/bespoke/unique) folder and/or SharePoint site and therefore Document Library, based on the Subject Line OR File Name. This can be achieved relatively easily and would save you a lot of routine administration time.
The Solution
Using Power Automate, When a new email arrives (v3) trigger, we can be quite specific about the sender of the email and for this, I have assumed all emails will come from xerox@mydomain.com and the email must contain an attachment! Ideally, you want to limit the emails that this Flow will trigger by using the available parameters for that action.
To define the locations for saving files, I have used an array in a compose action (but don’t panic!). You might want to use a SharePoint list as somewhere to save the specific subject or file name keywords alongside the folder or SharePoint site path. The advantage of using an array is that only the owner(s) of the flow can define these and all of the parameters are in one place. Creating a list in SharePoint might give you and/or other colleagues the ability to easily define and visualise the locations.
The parameters for this flow are basic, a keyword for searching the Subject or Filename containing, which I have called Dept; and the Path, which can either be a folder or a Site Name. If you are looking to have a dynamic Folder and Site, you can define a third parament or key:value pair.
Finding our path
We must now use the filter array action, which will allow us to search the Subject or Attachment name to see if it contains one of the keywords from the Dept key. If we get a match, the array will return a value for the Path. Here I use a compose action to return the first object from the array result. Why do I use the first expression? As the result is returned as an array, it expects multiple elements and calling the path without either using the first expression or body(‘FilterFindPath’)?[0]?[‘Path’] to select the first element, you will receive an error “Array elements can only be selected using an integer index“. Using First() or calling the element by using an integer will allow you to simplify your Flows if you know the result will always be the first element.
Array elements can only be selected using an integer index
Saving the File(s) to a Custom Path
Using the Create File action for SharePoint, I am using the Attachments Name and Attachments Content dynamic expressions from the email trigger. By default, when you select these dynamic values, Power Automate will put your actions into an Apply to Each. Why? This is because the results for the attachment name and content are returned as an array. You can accept this and your solution will handle multiple attachments. For my solution, I am a stickler for efficiency and have used the first() expression described previously. You can accept the default Power Automate build.
I’ve two actions here, one to demonstrate how to save to a custom dynamic path, the other to a custom dynamic site. When manually supplying a custom path for the site or folder, you will have to select Enter Custom Value or Power Automate will try and resolve the address and error ‘Site Address’ is required or the folder path with try and delete any additional text that you supply.
The expressions I have used both using the integer selection for the first array element and the first expression are as follows:
File Name: triggerOutputs()?[‘body/attachments’]?[0]?[‘name’]
File Content: first(triggerOutputs()?[‘body/attachments’])?[‘contentBytes’]
To finish off my flow and this is personal preference, I have deleted the incoming email. Why keep it? The file attachment has been saved for me.
See it in Action
Here I have an incoming email from the Xerox mailbox. A user myself or a n other, has sent me an attachment from the Xerox Mailbox. This will trigger my flow.
The filter action will look for each of the Dept values in the subject line and return a filtered array. In the example below, we output the ICT Path based on the subject line “This file is for ICT thanks”.
What do the two Create File Actions look like? Here you can see the custom dynamic paths for both the Folder Path or Site Address. I am obviously saving the file twice here in order to demonstrate the two options. You might want to combine the custom site and folder with an additional parameter in your Array or SharePoint List.
Summary
Quickly file away those routine documents received via email. Maybe this would simplify uploading documents into SharePoint in general? Work on a file and email it to a managed mailbox and have the Flow decide where to file the attachment based on keywords? Let me know how you have used this.
Hello DAMOBIRD365,
I have a question about a power flow, I am trying to create a flow which when I receive an e-mail with a subject line containing a specific reference that includes the keyword CASE, eg. “CASE22-12345”, the attachments are then saved in a folder in SharePoint which has the name as the specific reference, i.e. the attachments of e-mail with subject including CASE22-12345 are saved in the folder CASE22-12345. I was reading your solution, but I simply cannot apply this to my case, do you have an idea on how to go about?
Thank you in advance
Best regards
Hi Rodrigo, if your subject is the folder name and there is no cleansing required of the string, you can probably skip the whole array idea and just construct a path. Where is it you have become stuck?
Hi DamoBird365,
In the very first dialog box for when a new email arrives, the final parameter ‘only with attachments’ is ‘no’. I’m struggling to understand how this parameter interacts with the ‘include attachments’ ‘yes’ one a few lines above it. I assume the first parameter means the automation (the search process in the next actions) will also apply to the attachments.
I also have the same question as Rodrigo where I want to save down documents from person A (if it meets certain criteria) to location A and those from person B to location B. These locations already exist.
I assume I’ll need two flows for this?
Also, fantastic site and resources by the way – I’ve been inspired to have a go so my questions are probably a bit rookie.
Hi Colin, the first parameter means only trigger if the email has an attachment, the second parameter includes the file content so that you don’t need to run the action to get email attachments.
Regarding the requirement to send an email to a different location depending on sender, you can use a switch but if the actions in the switch branch are the same, you might consider using a json object or a SharePoint list. Take a look at Efficient Power Automate: Replace Complex Switches with JSON Objects
https://youtu.be/PD980sKKx0E. You might want to use a filter array instead of an apply to each to repurpose the data. I have an older blog post here https://damobird365.com/if-elseif-else-in-power-automate-simplified/
Fantastic, thank you – I’ll take a look. Microsoft sells this as no-code but I’m quickly learning that really you need to progress to low-code to get the truly useful tasks done! Happily Json use here looks a lot more complicated than it actually is (or at least that’s what I’m finding) but maybe there’s a new blog post in there somewhere.
Damo this is awesome – I have a different spin on this. When an email is rcvd we need to save the attachment in a sub-folder where other files with the part of the name are already stored. For example our folder structure would be something like: /documents/ReqID/2631411/ -or- /documents/ReqID/3110945/ Then the files in the folder (regardless of file type) all contain the 5-digit (uniqueID) value as part of the file names. For example the file names might be: Rental_application_17855.pdf, 17855_PayStub.jpg, LoanDoc17855.pdf. So the flow would need to get the 5-digit code from the Subject content – and then search the starting file path /documents/ – and find say the file name “Rental_Application_17855.pdf” and then work back the path down to that folder, and then use that to then save the attachment(s) from email to that folder.
Hi, I’ve been looking for a solution like this for months, however, I can’t get it to work. I just want to copy an attachment from an email so that if it has a specific depot name in it, it will save the attachment to a folder on SharePoint. Maybe I am trying to apply the wrong bits of your flow from above or not doing something right. My parameters look a bit like this:
[
{
“Depot”: “Birmingham”,
“Path”: “/Birmingham Depot/09. Safety Checklists/2023″
},
{
” Depot “: “Somerton”,
“Path”: “/Somerton Depot/09. Safety Checklists/2023″
},
{
” Depot “: “Warrington”,
“Path”: “/Warrington Depot/09. Safety Checklists/2023″
}
]
But when I try to add the “FilterFindPath” action, “Depot” does not come up as an item from the dynamic content so I can find an attachment name that contains a depot name.
Can you help? I’m self-taught on all of this, using Google and videos from people like yourself to help me find solutions.
You would need to write the expression. Something like item()?[‘Depot’] or by using parse json.