Custom Action – Post Email Attachment to Teams

summer animal cute grass

The built-in Teams actions of Power Automate do not enable you to post a message to Teams with an attachment from OneDrive or SharePoint. If you receive emails with attachments or generate regular documents, you might want to post them direct to Teams. With a custom connector, I will show you how this is possible. I have covered how to build the custom connector in my recent post How To? Build It! Custom Connector. In this post, I will use this connector and share with you my flow.

Teams message with email attachment saved to SharePoint

The flow is triggered by when an email arrives and is subject to the email containing a specific subject and also an attachment. This is to prevent unnecessary triggers. In order to get the team and channel GUIDs/IDs we must first use the Get a Team action, followed by the List Channels. You will note that I have chosen to save the file to my DamoBird365 site. On the result of the List Channels Action, I then perform a filter on the Array for a Channel Name, EmailAttachments. This could of course be the general channel or any channel of your choosing.

Retrieving the team and channel Guid / id

Next, we must save the attachment to SharePoint or OneDrive. In this instance, I have chosen SharePoint. I have also assumed that the email will only contain one attachment. This is to prevent an unnecessary Apply to Each loop. I have therefore used the first() expression in order to retrieve the first object from the name attachments array. These expressions are as per below.

File Name first(triggerOutputs()?['body/attachments'])?['name']
File Content first(triggerOutputs()?['body/attachments'])?['contentBytes']

Then using the custom connector, the complete build of which is demonstrated here, the attachment is posted to the Channel. To this custom connector, we must pass the Team ID retrieved from the Get a Team action. Then the first and therefore assumed only channel ID from the filter array. And finally, the attachment ID, retrieved from the attachments array. This the Etag value “{901B48BA-3D2E-41C5-88C2-BF588C9A3322},1” with the curly brackets, comma, and value removed. Hence the expression. Again I have used the first() expression in order to achieve this. Expressions for these two parameters are included below.

Channel-ID first(body('Filter_array'))?['ID']
Attachment ID first(split(replace(outputs('Create_file')?['body/Etag'],'"',''),','))

Also included in the action is your chosen message (content) and the encoding of the message (content-type). Most of these details can be explained by the Microsoft Documentation here. Because we must include the attachment id as HTML, we must use the HTML contentType.

The attachments contentUrl needs to be constructed using the SharePoint site URL and the Path dynamic value from the Create File action. The attachment’s name is retrieved from the SharePoint action too.

Whilst I did not test this myself, it would technically be possible to extend the custom connector to include mentions if need be.

Please let me know if you have made use of this idea or what your custom connector requirements are?

Share