How To Dynamically Send Multiple Documents As Attachment In Mail Using Power Automate (MS Flow)

Introduction

 
In this article, we will learn how we can get multiple documents from the SharePoint document library and send them in attachments in the mail using Power Automate.
 
Scenario
 
In this article, I have one list with the name Sendmail, in which there is a column where I will specify comma-separated document IDs which I want to send in the mail.
 
Furthermore, in the list, I have other columns like MailToAddress, Email Subject, and Email Body.
 
I need to send a mail with attachments when items created in this list.
 

Setup MS Flow

 
Step 1
 
Create an automated flow and select the 'when an item is created' template.
 
Step 2
 
Select site and Sendmail list.
 
 
Step 3
 
Now add a new action for initializing the variable. Give a variable name and select type as a string. In value, select the DocumentsIDs column from the list which will have comma-separated ids of documents that you want to send in the mail.
 
 
Step 4
 
Again, add initialize variable action. Give the variable name and select type as array. Keep value blank.
 
 
Step 5
 
Now, we will use Apply to each action which will loop through all documents ids and get documents. So, add apply to each action and we will split with comma our variable which we used for store document ids.
 
Step 6
 
In the Select an output from previous steps textbox, add below line in Expression and click on OK button, as shown in below screenshot.
  1. split(variables('DocumentIDs'),',')   
 
Step 7
 
Now in Apply to each, add Get file properties action. Select site URL and document library in which all your documents placed which will be sent in an email And in the ID field, select the below expression:
  1. items('Apply_to_each')  
 
Step 8
 
Now add a Get file content action. Select Site Address and in the File Identifier field, select the identifier returned by the Get file properties action.
 
 
Step 9
 
Now add Append to array variable action. In the Name field, select the array variable which we initialize above.
 
Step 10
 
In a Value field of Append to array variable action, we need to pass an array of the file name and file content in the below format:
  1. {  
  2. "Name":   
  3. "ContentBytes":  
  4. }  
Step 11
 
In a Name property of array, select a File name with extension from the Get file properties, as shown in the below screenshot:
 
 
Step 12
 
Add a comma after selecting the value for the Name property. Now In a ContentBytes property of array, select the File Content from Get file content, as shown in the below screenshot:
 
 
Step 13
 
The value of Append to array variable action will be as shown below once you select all values.
 
 
Step 14
 
Now add a new action to Send an email out of Apply to each action. In a To, Subject, and Body field select the column from the list in which we trigger this flow.
 
 
Step 15
 
Now in the attachment section, click on Switch to input entire array icon.
 
 
Step 16
 
Now in the Attachments value, select the array variable in which we have appended the content of files.
 
 
Step 17
 
This send email action will be as shown below after selecting all parameters.
 
 
 
Step 18
 
Now in the Sendmail list, create an item as shown below:
 
 
In the document library, there are below documents and the highlighted documents will be sent via email.
Step 19
 
The email which was sent is as shown in the below screenshot:
 
 

Conclusion

 
This is how we can read multiple documents from the SharePoint document library and send them in the mail using Power Automate. I hope this article will be helpful to you!


Similar Articles