Introduction
Ever wanted to attach a file or files to an email? In this section we will cover how to attach 1 or more files to an email from a SharePoint list. To start with make sure you have a SharePoint list and a file to attach to it.
Attach File to Email From SharePoint List
Within Power Automate we will trigger the flow on ‘When an item is created’ as we can’t filter out whether an attachment has been added or not within the trigger we may wish to add condition to check if an attachment has been added or not, if an attachment has not been added we wouldn’t want to flow to procced in this scenario.
Now that we have handled whether the list item contains an attachment or not we then want to get attachments, because SharePoint lists can hold multiple attachments within the attachment column we first want to use the ‘Get attachments’ connector
For this we need to specify the SharePoint Site, List name and the ID which comes from the dynamic content on trigger in the scenario
If we add the ‘Send an email (V2)’ connector and go to advanced options we will see that we need to present both ‘Attachment Name’ and ‘Attachment Content’
We can use the ‘Display Name’ from Get attachments to give the file a name with the file extension. Once we add this dynamic content, you will notice that Power Automate autotmatically puts ‘Send email (V2)’ into an apply to each, this is because the SharePoint list record can have multiple attachments. However when we run this it will send an email for every attachment uploaded in the SharePoint list.
Now we need to get the file attachment content, inside the ‘Apply to each’ but above the ‘Send email (V2)’ we need to add ‘Get attachment content’ connector. Again this will ask for the ‘Site Address’ and ‘List Name’ along with 2 other parameters:
- Id – ‘ID’ From the when an item is created
- File Identifier – id from ‘Get Attachments’
Now if we go back to the ‘Send email (V2)’ we can get the dynamic content ‘Get attachment content’
If we do a test, can see an email with the attachment that was uploaded to the SharePoint List
Attach Multiple Files to Email From SharePoint List
Now lets say a user didn’t just 1 attachment but they added more, this would work in the previous example but they would have all been sent in separate emails, we want to get all attachments within 1 email.
within the ‘Send email (V2)’ if we click on the ‘T’, this will then show us the input for an array. Highlighted is red is the record for the array, we can use this to create an array of files and input this into the email.
To do this we first need to initilise a variable as an ‘Array’
Within the ‘Apply to each’ we can add a connector ‘Append to array variable’ we in the same format we saw in the ‘Send email (V2)’ we can pass the array contents that the outlook connector expects:
{
"Name": @{items('Apply_to_each')?['DisplayName']},
"ContentBytes": @{body('Get_attachment_content')}
}
This will now loop through every attachment and them into an array in the format we have set. Finally we take the ‘Send email (V2)’ and put it outside the ‘Apply to each’ and add the attachment array to the outlook Attachments
If we run a Test this will now output an email with all the attachments within 1 email.