However, I have faced more and more scenarios, where Flows are created using a service account with elevated permissions that only a select number of users have access to, typically IT. The content of the email, however, may be set by someone else, such as a marketing or communications person. In this scenario, changing the email template would require contacting the IT person and requesting for specific changes to be made - not ideal.
In this article, I am discussing the concept of using tokens inside an email template to allow easy formatting without any access to the Flow itself.
Creating the Email Template
We begin again with the email template. In this example, we will create a template that has a few tokens incorporated in it. I will create my email template in HTML format, but that is not a strict requirement. Here's my template
<p>I have to say, your last name "<--LASTNAME-->" is very nice.</p>
Not the best example, but I hope you get the point. I am storing this example in my email template list in SharePoint.
Building the Flow
The flow is composed of four steps
- Get the email template
- Get list of recipients
- Replace tokens with desired text
- Send the emails
Get the email template
Start by retrieving the email message using the Get items. To limit the results returned, use the Filter Query option.
Make sure that you only have one email with the same title. Otherwise, you may get the wrong template. This can be set by making the Title field in the SharePoint list unique.
Get list of recipients
Similarly to getting the template, use the Get items action to retrieve the list of users who should receive an email. In my case, I have a field called SendEmail to indicated whether a user should receive an email or not.
Replace tokens with desired text
Here's where the fun begins. I used the Compose action for each token to replace the token placeholder with the actual value.
Before updating the tokens, I store the email template in a variable. This way, I can always retrieve the original template for the next email being generated. You may recall that I have two tokens in this example - <--FIRSTNAME--> and <--LASTNAME-->. Each one is being replaced separately, although it's possible to do all the updates in a single action.
To replace the <--FIRSTNAME--> token with the name from the user in the SharePoint list, I use the following expression in the Compose action.
This action effectively looks up the <--FIRSTNAME--> token in my Email Template variable declared above and replaces it with the First Name of the user from the SharePoint list item.
Next, I proceed with replacing the <--LASTNAME--> token with the Last Name of the user from the SharePoint list item.
In the final step, I am sending the email to the users by looping through the list of all recipients from the Get Email Recipients action. The body of the email is Output, which is the output of the Replace Last Name Compose action.
Make sure that if you're structuring your email template as HTML (as I did), that you indicate in the Send an email action that it's HTML format. Otherwise, you'll get a lot of tags in the body of your email.
This approach works well, but there are two caveats to consider:
- if the tokens are incorrectly written, then they will not be updated in the email itself
- if you want to add additional tokens, then IT will need to add rules for parsing it and replacing it.
Keep it Flow-ing...