How to embed an image in email body when data URI method does not work?
Use Case
The simple data URI method of embedding image in an email body does not work when image size is more than 1 MB. I did some research and found an alternative approach using graph API. The only catch is that it requires a premium connector ‘HTTP with Microsoft Entra ID (preauthorized)‘.
Scenario
We have a gif image of ~3 MB in SharePoint ‘Site Asset’ document library:
In case you want to try this example, you can download the same gif from the following location: Animated GIF – Find & Share on GIPHY:
Solution
I will demonstrate both approaches to embed an image in email body and show the comparison in the output section.
1.Firstly, get the content of the image using “Get file content using path” action. Set the site URL & image path in that action:
METHOD 1:
2. Next, add “Compose” action to create image html tag for the email body. Use expression to create the src property of image tag. Expression needs to be added in the expression box as shown below:
<img src="@{dataUri(body('Get_file_content_using_path'))}">
Please copy & paste the above statement directly in the textbox of compose action.
3. Post creating image tag, add “Send an email” action and pass the output of compose action in the body of the email as shown below:
METHOD 2:
4. For second approach, add another compose action. In the value, we will create an image tag with content Id (cid) as the name of the image. The content Id will be used later in the graph API when we will embed the image in the email body:
<img src='cid:giphy.gif' />
5. Finally, add “Invoke an HTTP request” action present under ‘HTTP with Microsoft Entra ID (preauthorized)‘ connector:
To make connection, enter “https://graph.microsoft.com” URL in both parameters i.e. ‘Base Resource URL’ & ‘Microsoft Entra ID’. This action allows us to perform operations using graph APIs with Delegated permissions. This is preauthorized and we don’t need to create Azure AD for the same:
Select the user from ‘Sign in’ pop up if it appears.
Configure the action’s input parameters as mentioned below:
Method: POST
Url of the request:
https://graph.microsoft.com/v1.0/me/sendMail
Headers:
Key: Content-type
Value: application/json
Body of the request:
{
"message": {
"subject": "Email With Embedded Image (size > 1 mb)",
"body": {
"contentType": "HTML",
"content": "@{outputs('Compose_2')}"
},
"toRecipients": [
{
"emailAddress": {
"address": "msolanki@pslgrp.onmicrosoft.com"
}
}
],
"attachments": [
{
"@odata.type": "#microsoft.graph.fileAttachment",
"name": "giphy.gif",
"contentType": "@{body('Get_file_content_using_path')?['$content-type']}",
"contentBytes": "@{body('Get_file_content_using_path')?['$content']}",
"isInline": true
}
]
}
}
Output
METHOD 1: Gif image is missing in the email body
METHOD 2: Image embed inside email body
Conclusion
I have demonstrated the steps to embed an image into email body using graph API. This works for the image whose size is more than 1 MB when data URI method doesn’t give the appropriate output.
Code & Solution Package
You can download unmanaged solution package from PnP sample gallery:
Embed Image In Email Body (Sample Solution Gallery) – Microsoft Adoption