Let's POWER Automate

From no-code to low-code

Menu
  • Expressions
  • Filters
  • General
  • Triggers
  • Application specific solutions
    • Dataverse
    • Excel
    • Forms
    • Planner
    • Outlook
    • SharePoint
    • Teams
  • Resources
  • Get help with flow
Menu

Send an email with multiple (0 – n) item attachments in Power Automate

Posted on November 11, 2020January 6, 2022 by Tom

“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.

Power automate sending 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.

Power Automate Initialize 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…

Power Automate send multiple attachments in an email

…with the variable as the attachments array.

Power Automate add array variable to email attachments

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, 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.


Do you struggle with the various expressions, conditions, filters, or HTTP requests available in Power Automate?

I send one email per week with a summary of the new solutions, designed to help even non IT people to automate some of their repetitive tasks.

All subscribers have also access to resources like a SharePoint Filter Query cheat sheet or Date expressions cheat sheet.

Zero spam, unsubscribe anytime.

12 thoughts on “Send an email with multiple (0 – n) item attachments in Power Automate”

  1. Tomáš Iránek says:
    May 19, 2021 at 9:06 pm

    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?

    Reply
  2. rupesh says:
    July 15, 2021 at 10:51 am

    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.’.’.

    Reply
    1. Tom says:
      July 19, 2021 at 2:13 pm

      Hello Rupesh,
      I’d try to add the base64(…) expression when adding the attachment content bytes to the array as suggested here.
      "ContentBytes": base64()

      Reply
  3. Steven says:
    August 30, 2021 at 5:02 am

    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!

    Reply
    1. Tom says:
      September 1, 2021 at 7:43 pm

      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.

      Reply
  4. Allie says:
    January 18, 2022 at 10:09 am

    This is very helpful! Thank you

    Reply
  5. Henrik says:
    February 23, 2022 at 4:54 pm

    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

    Reply
    1. Tom says:
      February 27, 2022 at 7:39 pm

      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’.

      Reply
  6. kody says:
    March 26, 2022 at 6:27 pm

    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.

    Reply
    1. Tom says:
      April 3, 2022 at 7:39 pm

      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.

      Reply
  7. David Wright says:
    June 27, 2022 at 6:41 pm

    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?

    Reply
    1. Tom says:
      July 6, 2022 at 6:44 pm

      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.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Do you know what to do, but not how to do it?

Get The Ultimate Power Automate expressions cheat sheet and translate your thoughts into flows with ease!


NEW! Master the HTTP requests to SharePoint with a new cheat sheet!

Do you struggle with the various expressions, conditions, filters, or HTTP requests available in Power Automate?

I send one email per week with a summary of the new solutions, designed to help even non IT people to automate some of their repetitive tasks.

All subscribers have also access to resources like a SharePoint Filter Query cheat sheet or Date expressions cheat sheet.

Zero spam, unsubscribe anytime.

Hello and welcome!

My name is Tom and I'm a business process automation consultant and Microsoft MVP living in the Czech Republic. I’ve been working with Microsoft technologies for almost 10 years, currently using mainly Power Automate, SharePoint, Teams, and the other M365 tools.

I believe that everyone can automate part of their work with the Power Automate platform. You can achieve a lot by "clicking" the flows in the designer, but you can achieve much more if you add a bit of coding knowledge. And that's what this blog is about.

To make the step from no-code Power Automate flows to low-code flows: using basic coding knowledge to build more complex yet more efficient flows to automate more of your daily tasks.

  • Use Power Automate to forward Outlook events upon registrationJanuary 29, 2023
  • Why the condition is false for the same numbers (Power Automate)January 25, 2023
  • How to forward event invitation to other calendar (Power Automate)January 22, 2023
  • Run ‘For selected item’ flow from non-default environment (Power Automate)January 18, 2023
  • Hide button in SharePoint list after Power Automate flow startedJanuary 15, 2023

Power Automate blogs worth visiting

Damien Bird
Dennis (Expiscornovus)
Paul Murana

©2023 Let's POWER Automate | Theme by SuperbThemes