Add Image in Visualforce Email Template

In this post, we will implement the functionality to Add Image in Visualforce Email Template. Email template can contain the images in the body of email template like Logo of the company. We will add the image dynamically such that there will be no post deployment steps for higher environments. Let’s get into the implementation.

Implementation

We will create a Visualforce Email Template which will have the Visualforce Component. The Apex Controller of the component will fetch the image from the Documents and create a dynamic URL for the image. This URL will be used to display image in the component.

First, go to Documents Tab and add image in a particular folder. Make sure the Externally Available Image checkbox is checked.

Add Image in Visualforce Email Template
Add Image in Visualforce Email Template

Right click on Image that is uploaded in the document and click on Copy Image Address. The address will look something like below;

"https://niksdev-dev-ed--c.documentforce.com/servlet/servlet.ImageServer?id=0152x000000Yq64&oid=00D2x000004vuk4&lastMod=1593803974000"

Note: You might see ‘&‘ replaced with ‘&‘ in the code snippet.

This is a global URL to access the image anywhere. This Url contains the id of Document and Org Id (oid). We can ignore the lastMod parameter.

Then, create a Apex Class that will query the Document using the Name of the Document. We have to create above link dynamically using getSalesforceBaseUrl() and UserInfo.getOrganizationId() along with Id of the Document.

ImageController.apxc

public class ImageController {
    
    // Getting the Document Id and creating a Document Url.
    public String getStrLogoUrl(){
        list<Document> lstDocument = [SELECT Id FROM Document WHERE DeveloperName = 'Salesforce_Logo'];
		String strOrgId = UserInfo.getOrganizationId();
		String strLogoUrl = System.URL.getSalesforceBaseUrl().toExternalForm() + 
            				'/servlet/servlet.ImageServer?id='+lstDocument[0].Id +
            				'&oid=' + strOrgId;
        return strLogoUrl;
    }
}

Note: You might see ‘&‘ replaced with ‘&amp;‘ in the code snippet.

Then, we have to create a Visualforce Component and use apex:image tag to display the image with url attribute that is returned from our Apex Controller. This component will have the content of the email along with the image.

SalesforceLogoCmp.vfc

<apex:component access="global" controller="ImageController">
    
    Hello Salesforce Developer, <br/><br/>
    Please find requested Salesforce Image below: <br/><br/>
    <apex:image id="Logo" width="250px" height="250px"
                url="{!strLogoUrl}" />
</apex:component>

Finally, we need to create a Visualforce Email Template and include our Visualforce Component SalesforceLogoCmp inside messaging:htmlEmailBody tag of Email template.

VIsualforce Email Template

<messaging:emailTemplate subject="Email with Image" recipientType="User" relatedToType="Account">
<messaging:htmlEmailBody >
    <c:SalesforceLogoCmp />
</messaging:htmlEmailBody>
</messaging:emailTemplate>

Add Image in Visualforce Email Template

Once the email is sent, it should look like below:

Add Image in Visualforce Email Template
Add Image in Visualforce Email Template

It is not recommended to use Static Resource to add image in Visualforce Email Template. Check this Salesforce document to know more about it.

This is how we can add Image in Visualforce Email Template. You can check more Salesforce implementations here.

If you don’t want to miss new posts, please Subscribe. See you in the next post.

5 thoughts on “Add Image in Visualforce Email Template”

  1. Hi Nikhil
    I tried this and the image is not appearing. after I upload the image through documents I got the image address and for me & was not replaced by & so accordingly updated the imageController apex class.

    Image URL obtained from documents
    https://udayhome-dev-ed–c.documentforce.com/servlet/servlet.ImageServer?id=0155w000003HLXC&oid=00D5w000004sGIe&lastMod=1595385288000

    URL generated from apex class
    https://udayhome-dev-ed.my.salesforce.com/servlet/servlet.ImageServer?id=0155w000003HLXCAA4&amp;;oid=00D5w000004sGIeEAM

    is the issue because of difference in salesforce base url or 15 digit id vs 18 digit id

  2. Hi Nik,

    I’m using the above code in VF Email Template. It’s succedded in my personalOrg but when I’m trying to use it in my Dev Org it’s throwing an error called “List out of bound Index: 0”
    Could you please help me with the issue. It would be helpful to me.

    • Hi Sivanandini, please make sure you have the image in Dev Org that you are querying in SOQL. “List out of bound Index: 0” error occurs when you try to access list elements that have no records. Ideally, after the SOQL query, we should have the IF condition to check the list size.

Comments are closed.