“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.
{
"Name": <DisplayName>,
"ContentBytes": <AttachmentContent>
}
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.
Summary
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, e.g. multiple email attachments, 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.
Zdravím – vyzkoušel jsem, vytvoří se mi pole, ale když ho dám do funkce “Poslat email V2”, tak to zkolabuje na nesmyslné chybě 400 (chybějící vstupy). Když pole příloh odstraním, email se odešle. Mohou vadit např. české znaky v názvu příloh?
its generating error. The request failed. Error code: ‘InvalidRequestContent’. Error Message: ‘The request content was invalid and could not be deserialized: ‘Could not find member ‘contentBytes’ on object of type ‘ApprovalsConnectorAttachment’. Path ‘attachments[0].contentBytes’, line 1, position 843.’.’.
Hello Rupesh,
I’d try to add the base64(…) expression when adding the attachment content bytes to the array as suggested here.
"ContentBytes": base64()
Hello Rupesh,
I have followed your step to set the flow, but if I added 3 attachments, the outlook will send 3 e-mails, the first e-mail contains the first attachments, the second e-mails contains the first and second attachments, and the third e-mail contains the whole three attachments, could give me some suggestions on how to solve this problems, thanks!
Hello Steven,
the ‘Send an email’ action must be after the ‘Apply to each’ action, based on your description it’s in the ‘Apply to each’ and that’s why it sends an email every time an attachment is added to the array.
This is very helpful! Thank you
Hi,
I tried this and it works very well but when the mail arrives with PDF’s attached the files cannot be opened. They look like perfectly nice PDF’s but when I open them they are corrupt and won’t work.
Do you know if there is a problem generating the PDFs from base64?
Regards
Henrik
Hello Henrik,
it’s hard to point in an exact direction as I don’t know what’s the source of the attachments and how you process them, but if you think it might be encoding you could try to apply base64(…) or base64ToBinary(…) on the ‘ContentBytes’.
The execution of template action ‘Apply_to_each’ failed: the result of the evaluation of ‘foreach’ expression ‘i@{variables(‘AllAttachments’)}’ is of type ‘String’. The result must be a valid array.
This is what happens when I try to implement this.
Hello kody,
check the input of the ‘Apply to each’ action, it seems wrong, it shouldn’t be a variable but the array with attachments.
Thank you so much for sharing your knowledge. I have been using this Power Automation, but it fails when no attachments are added. Is there a way to keep the flow from failing if NO attachments are present?
Hello David,
if you switch to the ‘Input entire array’ mode then it doesn’t matter if there’re attachments. If there’s no attachment it’ll input [] (empty array) and send the email.