Welcome again to another Logic Apps Best Practices, Tips, and Tricks. In my previous blog posts, I talked about some of the most essential best practices you should have while working with the Azure Logic App:
- #1 Logic App Naming Convention
- #2 Actions Naming Convention
- #3 Add comments
- #19 Protect your Logic Apps (Part I) – Using API Management to expose the Logic App and Implement IP Addressing Restriction
- #19 Protect your Logic Apps (Part II) – Secure data in the run history by using obfuscation
And some tips and tricks:
- #4 Using Scopes
- #5 Delete comments
- #6 Error handling configure run after settings
- #7 Learn from failures
- #8 Expressions nightmare
- #9 Control your connectors (API connections)
- #10 Fix connectors (or API connections)
- #11 Connectors naming convention
- #12 Choose your developer tools properly
- #13 Logic App (Standard) vs (Consumption)
- #14 Implement good governance policies
- #15 Get the error message
- #16 Roll back to a previous version of an Azure Logic App Consumption
- #17 Using other HTTP Methods then POST
- #18 Foreach Parallelism
- #20 Accessing Runtime Settings (Part I) – Logic App Consumption
- #20 Accessing Runtime Settings (Part II) – Logic App Standard
- #21 Moving or organizing shapes inside Visual Studio Logic App designer
- #22 The if() expression function
- #23 Debatching Messages
- #24 Configure correctly your Retry Policies
- #25 How to send a well-formatted custom HTML Email
- #26 Call Asynchronous child Logic Apps
Today I will speak about another useful Best practice, Tips, and Tricks that you must consider while designing your business processes (Logic Apps): How to embed HTML images into your email using Logic App Designer.
How to embed HTML images into emails using Logic App Designer
What we pretend to achieve is how we can, inside Logic Apps, embed an image in the body of the email and send it without the image being blocked by Outlook or Gmail. It seems simple, but it has some tricks on it.
Most of the times, people try to do is to add an image inside the body of the email like this:
However, that will not work since the HTML code you place will be converted as text, and the result will be this:
Workaround to fix this issue
A simple workaround to fix the issue above is to go to the Code view of your Logic App and fix the HTML code inside the body property. When you access the Code View, you will see that the image HTML code you added is expected to act as a string:
Go ahead and fix that to be:
And save your Logic App. If you try again, you will see that a picture will be presented in your email instead of an HTML text.
However, this workaround is a little bit weird since if you go back to Logic App Designer, you will not see any reference to the picture even though it will be there:
So, this workaround is difficult to read and maintain in the long run.
Approach 1: Using a variable with a link to the image
The first approach you can implement is to use a variable to contain the image HTML code, in this case:
- <img src=\”https://blog.sandro-pereira.com/wp-content/uploads/2017/01/sandro-pereira-blog-logo.png\”>
And in our email action on the body, we will make a reference to this variable:
If we save and test our Logic App, you will see that we end up receiving an email with the image inside:
This approach works, however many email clients may block your images or ask if you want to download them:
So the main question is: is there a better way? Yes, there is!
Approach 2: Using a variable with an image in bas64
This last approach, and for me the best approach, is to use the same strategy of the previous approach, but instead of having an image URL, we provide an image in base64.
If you don’t know how to convert an image in base64, you can use this website: Base64 Image Encoder to accomplish that, and use the data info to add in your <img> element
In my case, I now need to add the following code to my variable value:
<img src=”data:image/png;base64,iVBORw0KGgoAAAA … … … NSUhEUgRK5CYII= /> (it is a big base64 string)
And then keep the same code in the email action:
Now, if we save and test our Logic App, everything will work as expected, and the images will not be blocked in our email clients:
I hope you enjoy this developer tip and stay tuned for the following Logic App Best practices, Tips, and Tricks.