Dynamically create a list of e-mail attachments with Microsoft Flow : use Base64()

A few days ago I wanted to solve the following issue : we have a SharePoint document libraries containing a bunch of documents. And I needed to create a flow that read these documents and send them automatically by e-mail.

Si I did create a flow with the Send an e-mail action. One of the new option of this action is to be able to pass an array of attachments. By default the attachments are statically linked to the action unless you click the small icon next to the Attachments field:

attacht1

This allows you to pass an array of attachments :

attacht2

Now how can you define an array ? you can define an array variable, but before doing this we need to proceed to an experimentation :

Define a Compose before the send an en e-mail action. In the Compose add an array of object with a “Name” and a “ContentBytes” property , and pass this compose output to the send e-mail activity as illustrated below :

attacht3

Now run your flow and you will see that it fails !

attacht4

The reason is quite simple : you just have to encode you content with base64(), so let’s fix this.

Update the compose activity as described below:

attacht5

Test the flow again and it works ! Check your e-mail, you should have received the mail with the attachment.

Now let’s apply this knowledge to an array & a SharePoint document library.

attacht6

In your flow, let’s define a variable of type Array and name it myfiles:

attacht8

Add a (SharePoint) Get files action to fetch the documents from the SharePoint document library :

attacht9

Add an Apply to each action in the loop

attacht10

After the Apply to each pass your variable to the send an e-mail action:

attacht11

Your flow should look like this:

attacht12

Test your flow and check your e-mails.

 

Leave a comment