Power Automate: Add attachment to e-mail dynamically

Adding attachments to an email looks relatively straightforward, but if you want to do it dynamically, it can be tricky to understand how to do it. It’s a question that I see all the time showing up in the Power Automate’s Community and want to address here in some detail so that you can understand how it works.

Template:
If you want to see an example, I have a template in my cookbook section that you can import and adapt to your needs. 

The issue

The main issue here is that you don’t know beforehand how many attachments you may have, and, therefore, you need to fill in the attachments dynamically. You cannot map them to the “Send Email.”

Entering the advanced mode

To achieve this, you need to tap and go into the advanced mode. It can look intimidating since it becomes a text box, so we need to know what we’re doing.

The syntax is straightforward, but when you don’t know how to build something, I recommend filling in the primary mode with some data and switch to the advanced mode, and you’ll see the structure needed. Something like this:

The syntax

It may look strange to you, but if you’re familiar with JSON, this will make sense.

[
  {
    "Name": "File1",
    "ContentBytes": @{outputs('Get_file_content_using_path')?['body']}
  },
  {
    "Name": "File2",
    "ContentBytes": @{outputs('Get_file_content_using_path')?['body']}
  }
]

Let’s break it down.

1. The “[“ and the “]” indicate that you’re defining an array.
2. The “{” and “}” indicate a key: value object. 
3. The key and object say to SharePoint that you have a field that contains the “Name” of the file and another with the ”ContentBytes” or the actual file.
4. Separate each object by , to indicate that they are multiple rows in the array.

Sounds ok? So what happens if you want to add the 3rd value. Easy, add another key/object like this:

[
  {
    "Name": "File1",
    "ContentBytes": @{outputs('Get_file_content_using_path')?['body']}
  },
  {
    "Name": "File2",
    "ContentBytes": @{outputs('Get_file_content_using_path')?['body']}
  },
  {
    "Name": "File3",
    "ContentBytes": @{outputs('Get_file_content_using_path')?['body']}
  }
]

The template

Let’s say that you query a folder in your OneDrive and want to send all files to a colleague by email. Here’s what you can do.

  1. Define a variable
  2. Fetch the files in the folder
  3. Append the syntax to the variable
  4. Use the contents of the variable in the advanced mode.

Here’s what it looks like:

Correction:
Previously, I converted it to a JSON format before adding it to the “Attachments” part of the action, but it’s unnecessary. You can add the array, and the Flow will convert and send the files correctly.

The only thing we’re missing is building the email itself. Here’s how to do it:

As you can see, we’re adding the files dynamically to the “Attachments” section without defining beforehand how many we’re sending.

One thing you should be aware of. If you’re using your connection to send emails, all of them will be saved in your “Sent” folder since it’s you who are sending them. If not necessary, I recommend taking a look and deleting them or build a Flow that does that for you.

Final thoughts

It looks complicated, but once you start to understand how things work and all boils down to JSON formatted strings in the background, it becomes simpler to build these advanced cases.

Have a suggestion of your own, or disagree with something I said? Leave a comment or interact on Twitter and be sure to check out other Power Automate-related articles here.

Photo by Thanhy Nguyen on Unsplash

Manuel Gomes

I'm a Project Manager with experience in large projects and companies. I've worked in the past for companies like Bayer, Sybase (now SAP) and I'm currently working for Pestana Hotel Group.

View all posts by Manuel Gomes →

10 thoughts on “Power Automate: Add attachment to e-mail dynamically

  1. well, short and simple question here. Can I drop a file in my one drive, at any time and have that file be sent?

    I think the answer is yes with this post. But, please advise.

    1. Hi John,

      Yes, you can. You can even do it automatically. You can use the “When a file is created” trigger. Fetch it and send it off.

      If you have questions about building the Flow, please let me know, and we’ll work on a template.

      Cheers
      Manuel

      1. You know my power automate was having trouble doing this and originally showed an error. I would love to see a template to figure out what I am doing wrong.

        1. Hi John,
          Sure, I added a new one that you can use as a reference. It contains comments, and I simplified the process to make it more understandable.
          It’s in my Cookbook Section but you can download it directly here
          Any questions or other issues, please let me know.
          Cheers
          Manuel

  2. Manuel, your tutorial is clear and easy to follow! I tried your steps to send files from One Drive by appending to an array variable but it fails when sending the email “‘Attachment Content’ cannot be null or empty.”. The output for appending is —
    {
    “Name”: “2021-04-30-090412.jpg”,
    “ContentBytes”: null
    }

    Flow got to the correct jpg file but the file size is 450kb on One Drive. Any ideas?

    1. Hi Yogi Ch

      Thanks a lot. I’m glad the articles are helpful.

      Regarding your problem, I would check the variable you’re using to fill in the “ContentBytes”. Also, check if in the Flow History if the file is fetched correctly from OneDrive. You can see this by expanding the OneDrive action and see if you have the information there.

      If you have, then it’s an issue with associating that to the “Content Bytes”. If not, then there’s something wrong with fetching the file.

      Can you please check if and let me know if you have any questions?

      Cheers
      Manuel

  3. Thank you Manuel. How do I connect the template with the Power Automate program? How does it work?

  4. Hi;

    Can this be done by parsing an email for URL's, fetching the files (images) the links point to, creating a new email (or same doesn't matter) and adding the files as attachments before emailing them

    1. I like the idea, but I have to think about it. Parsing text in emails is tricky, as well as fetching stuff from URLs.
      Give me a couple of days to try to come up with a template for you :).
      Cheers
      Manuel

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: