Image of the action in Power Automate that creates an email attachment.

Step-by-Step Guide: How to Use Power Automate to Create and Send Emails with Attachments in Dynamics 365/Dataverse

Introduction

In this post, we look at how to use Power Automate to create and send emails with attachments in Dynamics 365/Dataverse. When working with Dynamics 365 or Dataverse, one of the key advantages is tracking emails, and if you use another email connector like Outlook, you cannot do this. This post will show how to create an email, add an attachment and send an email so that it’s tracked inside D365/Dataverse.

Table of Content

Prerequisites

  • Familiar with Dataverse
  • Familiar with Dynamics 365
  • Familiar with Power Automate
  • An environment and a queue/system user that is configured for sending emails

How to Create and Send Emails with Attachments Using Power Automate

In this example, we use the essence of the flow from the Power App Award Generator blog post, but instead of sending the email with the Outlook connector, we’ll use Dataverse. This allows email tracking in Dataverse/Dynamics. The flow looks like and we’ll go through each of the steps below the image.

An overview of the Power Automate flow we are creating in the post.
An overview of the Power Automate flow we are creating in the post.

Step 1: The trigger used in this example is the Manually trigger a flow with the same parameters as in the blog post, Power App Award Generator. The values in these parameters is used to populate the Word Template later in the post.

Image of the "Manually trigger a flow" trigger with parameters.
Image of the “Manually trigger a flow” trigger with parameters.

Step 2: In this step, we’ll create a compose action that creates the naming convention for the documents generated.

Image of the action containing the naming convention for the generated documents.
Image of the action containing the naming convention for the generated documents.

The expression in the compose action outputs the following 2022.12.10_Chef_of_the_Year_John_Doe. The naming convention is a combination of the following expression.

// Formats the date in the parameter DateOfCompletion to be like this, 2022.12.10
formatDateTime(triggerBody()['text_2'],'yyyy.MM.dd')

// Replaces spaces in the parameter TypeOfCertification with underscore, like this Chef_of_the_Year
replace(triggerBody()['text_5'],' ','_')

// Replaces spaces in the parameter FullName with underscore, like this John_Doe
replace(triggerBody()['text_3'],' ','_')

// When putting it all together it looks like this 2022.12.10_Chef_of_the_Year_John_Doe
formatDateTime(triggerBody()['text_2'],'yyyy.MM.dd')_replace(triggerBody()['text_5'],' ','_')_replace(triggerBody()['text_3'],' ','_')

Step 3: In this step, we’ll create the email activity pointer used to send emails. This is the configuration of who is the sender and receiver of the email. In the image below we can send an email without tracking it to a contact by only referencing the addressused field. Later, we see how we can track the email to a contact and send it from a system user instead of a queue.

How to speficy email sender and receiver without tracking it to a contact in Dataverse when sending the email to an email address.
How to specify email sender and receiver without tracking it to a contact in Dataverse when sending the email to an email address.

Below, we can see the email activity pointer for sending it from a queue and to an email address.

[
  {
    "participationtypemask": 1,
    "partyid@odata.bind": "queues(d9da8b52-8378-ed11-81ac-000d3ab79606)"
  },
  {
    "participationtypemask": 2,
    "addressused": "@{triggerBody()['email']}"
  }
]

NOTE: In the reference to system users and contact you usually add dynamic values from a for instance a Dataverse trigger or list rows and not hard coded values. The hard-coded values are for testing purposes only.

In the next example, we look at how we can track the email to a contact and send it from a system user instead of a queue.

How to speficy email sender and receiver and what email to use when sending the email to a contact.
How to speficy the email sender and receiver and what email to use when sending the email to a contact.
[
  {
    "participationtypemask": 1,
    "partyid@odata.bind": "systemusers(d9da8b52-8378-ed11-81ac-000d3ab79606)"
  },
  {
    "participationtypemask": 2,
    "partyid@odata.bind": "contacts(f5973462-768e-eb11-b1ac-000d3ae92b46)",
    "addressused": "@{triggerBody()['email']}"
  }
]

Step 4: Next step, create a file that we are going to attach to the email. In this case, we are resuing the template from the post mentioned earlier, the difference is that we are using another trigger parameter. To see more in detail how the generation of the document looks like, see the post Power App Award Generator.

How to populate a Word Template and convert it to a PDF using the OneDrive Connector.
How to populate a Word Template and convert it to a PDF using the OneDrive Connector.

Step 5: Furthermore, we need to create an email message that the attachment is related to.

How to create an Email Message in Dataverse.
How to create an Email Message in Dataverse.

Step 6: Further on, we’ll create the generated document in OneDrive as an attachment in Dataverse, we’ll do this by following the steps below.

Things to keep in mind and also commented on in the images below.

First thing, when looking for the attachment table, select the first in the list.

Second, is when selecting the Entity value. Do the following, select the dropdown and choose Enter custom value > Enter email in lowercase letters otherwise it won’t work, and you’ll get an error regarding object type.

How to select the correct Entity when creating a new Attachment in Dataverse.
How to select the correct Entity when creating a new Attachment in Dataverse.
Image of which Attachment table to select when creating a new attachment in Dataverse.
Image of which Attachment table to select when creating a new attachment in Dataverse.
How to create an attachment in Dataverse.
How to create an attachment in Dataverse.

Lastly, we’ll send the email message with the attached attachment.

How to send an email using the Dataverse Perform a bound action.
How to send an email using the Dataverse Perform a bound action.

In the next part, we look at how it looks when the flow runs and how it looks in Dynamics.

How it Looks When Sending Email With Dataverse

This part shows a GIF of how it looks like when the flow is running and the result in Dynamics.

GIF showing how it looks like when an email is sent with an attachment using Dataverse.
GIF showing how it looks like when an email is sent with an attachment using Dataverse.

Conclusion

And that’s it, how to create attachments on emails via the Dataverse connector.

Image of how the email with an attachment looks like in the timeline in Dynamics 365.

Resources


For More Content See the Latest Posts