Send an email with multiple (0 – n) item attachments in Power Automate
Posted On November 11, 2020
“I need to send all item attachments in a single email, but Power Automate flow is sending multiple emails, each with a single attachment.”
‘Get attachments’ action in Power Automate returns all attachments for given item in an array. The ‘array’ part is important. Every time you use data stored in an array, Power Automate will add automatically ‘Apply to each’ action around it. That means, once you try to add the attachment into an email, it’ll add ‘Apply to each’ around the ‘Send an email’ action. The result: one email per attachment.
To send only one email, the ‘Send an email’ action must be outside of the ‘Apply to each’. And to achieve that, you can’t use the output from ‘Get attachments’ directly, you need a variable. A single array variable that will contain all attachments.
Adding the attachments
First step is to initialize the array variable.
The next step is to process the attachments. First you get all of them using ‘Get attachments’, then you ‘Get attachment content’ for each of them. These two actions give you all you need for the array variable. You build the variable using ‘Append to array variable’ action with the following code.
Warning: make sure you don’t add any character nor white space after the <AttachmentContent> dynamic content. Otherwise you’ll end with ‘AttachmentContentNotValidBase64String’ error.
The last step is to send the email…
…with the variable as the attachments array.
The most important part of this post is to get an idea how arrays work. When you get action output in form of an array, Power Automate will process its content one by one. It’ll add the ‘Apply to each’ action and repeat all the actions inside for each item in the array. If you keep ‘Send an email’ action inside ‘Apply to each’, it’ll repeat the action.
You can’t avoid the ‘Apply to each’ action when processing multiple attachments. But you can use it to convert the array into different format. And then use the variable instead of the original dynamic content. The principle is the same, no matter if it’s multiple attachments in an email or multiple attachments in a task.