Visualforce Email Template in Salesforce

by Rijwan Mohmmed

Hello friends, today we are going to discuss the Visualforce Email Template in Salesforce. Visualforce Email Templates are your secret sauce to crafting stunning and highly customizable email content directly within Salesforce.

Visualforce Email Templates empower you to create compelling email communications, strengthening your customer relationships and streamlining your processes. Whether it’s nurturing leads, sending out invoices, or any email need, this feature amplifies your Salesforce toolkit.

Also, check this: Governor Limits in Salesforce

Key Highlights :

  1. Dynamic Content: Merge data from records, creating personalized and engaging emails.
  2. Custom Styling: Leverage CSS to design emails that align with your brand.
  3. Complex Logic: Implement intricate logic and render different content based on conditions.
  4. Rich Media: Embed images, links, and dynamic components for interactive emails.
  5. Personalized Engagement: Tailor your messages for specific recipients, enhancing user experience.
  6. Consistency: Ensure brand consistency in your communications.
  7. Versatility: Visualforce templates work for various use cases, from sales proposals to customer support emails.

Process & Code :

Before we jump into the Visualforce code, there are a few steps we have to follow to create a Visualforce Email Template.

Step 1: Go to Setup => search email template quick find box => click Classic Email Templates => click New Template

Step 2: type of email template option choose Visualforce.

Step 3: On the template page we can set the email subject, email template name, and the Template Unique Name.

Step 4: Select the Recipient Type, in this example, we are going to send the email to contacts.

Step 5: Checked the checkbox Available For Use to make the email template available.

Step 6: If you want to use merge fields you can set the Related To Type to the desired object. We have set it to account for this example. Then click Save.

After the Visualforce email template is created, we can edit the template. To make things easier we will create a Visualforce component that we will call in our email template.

Now we create a VF Component and a controller for it. We can pass merge fields to our Visualforce component from the email template. This lets us perform more advanced logic than the email template allows as we can perform additional queries if needed in the controller.

<apex:component controller="VFEmailController" access="global">
    <!-- Get values from email template -->
    <apex:attribute name="accountId" assignTo="{!accId}" description="This is the account Id passed from the email template." type="Id"/>
    <apex:attribute name="accountName" description="" type="String" />
        Hi {!accountName}, Here is Contact list of your Account.
    <table border="1">
            <apex:repeat value="{!Contacts}" var="contact">
public class VFEmailController {
	//account id passed in from email template
    public static Id accId { get; set; }

    //Return a list of contacts for the associated account
    public static List<Contact> getContacts(){
        return [SELECT Id, Name, Email, Phone FROM Contact WHERE AccountId =: accId];

Now we need to call our Visualforce component from the email template. We can do this by using the c namespace and the name of the Visualforce component.

Click Edit Template and make the changes.

<messaging:emailTemplate subject="Account related contacts" recipientType="Contact" relatedToType="Account">
    <messaging:htmlEmailBody >
        <c:VFEmailComponent accountId="{!relatedTo.Id}" accountName="{!relatedTo.Name}"/>

Here we are referencing our Visualforce component and passing the account ID and Account Name to the component.

After we have finished editing the template click Save.

Thankfully, Salesforce provides us with a way to easily preview our Visualforce template.

Click Send Test and Verify Merge Fields.

Select the records you’d like to test with select the send email preview option and enter your email if you want to receive the email as a test.

After you select OK, you can see the email show up on the page.

Output :


Reference :

  1. Creating a Visualforce Email Template
What’s your Reaction?

You may also like


WindBoy September 6, 2023 - 11:59 pm

This is nice advice
now you have 1/3 => 2/3 coffee 🙂
See the next time with coffee

I think part is important too
becouse i used and get “xxx”
anyway have a nice day

WindBoy September 7, 2023 - 12:01 am

omg maybe tag does not works in comment
I used “messaging:plainTextEmailBody” tag and I get HTML Tags like h1, div, p lol


Leave a Comment