In this Salesforce tutorial, we will learn how to create visualforce email template in Salesforce.
In Salesforce, we can create text templates to send birthday wishes, monthly summaries, etc., to various people. However, these templates are restricted while using those templates. We cannot attach any object we want and use all its fields. For this purpose, we have a Visualforce Email Template in Salesforce.
The topics that we will cover in this Salesforce tutorial are as follows.
- Visualforce email template in Salesforce
- Visualforce Important Points
- How to create a visualforce email template in Salesforce Lightning
- Salesforce Visualforce Email Templates Adding Merge Fields
- Modify a Visualforce Email Template for Guest Users
Visualforce email template in Salesforce
The Visualforce email template is a kind of Email template we have in Salesforce. Visualforce templates are useful when we have to render information conditionally or display a list of related records. Visual email templates allow us to send emails with dynamic content and styling.
With the help of the Visualforce controller, we can display specific data within the email. A VisualForce controller is a set of instructions for users to interact with the components in a VisualForce page.
Below are some standard actions available in controllers.
- save– saves a record and returns to the object page
- quicksave -saves the record and stays at same page
- edit — takes the page to edit mode
- delete — deletes a record
- cancel — cancel the ongoing process
- list — returns a page reference object of the standard list page.
Visualforce Important Points
- A single <messaging:emailTemplate> tag should include the Visualforce email template.
- A Visualforce email template can only be 1MB in size.
- The access level for every <apex:component> tag used in a Visualforce email template needs to be global.
- Either a single <messaging:htmlEmailBody> or <messaging:plainTextEmailBody> tag should be present in the <messaging:emailTemplate> tag.
- A Visualforce email template cannot be used to send a mass email.
- There are certain common Visualforce components that cannot be used in the <messaging:emailTemplate>. For instance, all input components and all related page block components.
- The <messaging:htmlEmailBody> component can contain a combination of HTML and Visualforce markup.
- The <messaging:plainTextEmailBody> component can only contain plain text and Visualforce markup.
Read How to use Classic Email Template in Salesforce
How to create a Visualforce email template in Salesforce
In the steps below, we will discuss creating a Visualforce Email template in Salesforce Lightning. The same steps must be followed to create a Visualforce Email template in Salesforce Classic.
Before, we started learning to create a Visualforce email template in Salesforce. Let’s first learn how to create a new folder for the Visualforce email template.
Create a folder for a Salesforce Visualforce email template
Follow the below steps to create a folder.
1. Navigate to Salesforce Lightning’s setup page, and in Quick Find, search for and select Classic Email Templates.

2. Click the Create New Folder button.

3. Enter the Email Template Folder Label, change Public Folder Access to Read/Write, and make this folder accessible to all users.
At last, click the Save button.

After creating the folder, we will learn how to create a Visualforce email template in Salesforce.
Create a visualforce email template in Salesforce lightning
To create a Visualforce email template in Salesforce Lightning, open the existing Classic Emiail template folder or the one that we created above, then follow the below steps.
1. In the Classic Email Templates folder where you want to create the template, click the New Template button.

2. Click the Visualforce radio button and then click on Next.

3. To configure the new Visualforce template, enter the below fields.
- In Folder, select the Folder in which you want to create your new template.
- To make it available for use, click the checkbox Available for Use.
- Set the Email Template Name, and it automatically sets the Template Unique Name according to it.
- Next, set the Encoding to Unicode (UTF-8).
- Add the Description and Subject if you want, as both fields are not mandatory.
- Then, choose the Email Subject, Recipient Type, and Related To Type.
- I’ll be designing a Contact Alert template. Name it Contact Alert and make the Subject of the email Contact Details. The User will be the Recipient Type, Related to Type is Contact.
- Finally, click on the Save button.

4. In the section Emial Template, click on the Edit Template button to create a Visualforce component and a controller.

5. Paste the following code in the Email Content box.
<messaging:emailTemplate subject="New Account Added" recipientType="User" relatedToType="Contact">
<messaging:plainTextEmailBody >
Hello {!recipient.Firstname}
Here, Is the {!relatedTo.salutation} {!relatedTo.firstname} {!relatedTo.lastname} deatils.
DOB: {!relatedTo.birthdate}
Department: {!relatedTo.department}
Email: {!relatedTo.email}
Phone: {!relatedTo.phone}
Thank You.
</messaging:plainTextEmailBody>
</messaging:emailTemplate>6. Click on the Save button.

If you want to use this email template, follow the tutorial guide on How to use a classic email template in salesforce. The following is the output.

With this, we have learned to create a visualforce email template in Salesforce Lightning.
Salesforce Visualforce Email Templates Adding Merge Fields
We can also modify the Visualforce Email Templates by merging fields into it. To merge fields, open the Email Template and follow the below steps.
1. In the section, Email Template Detail click on the button Edit Properties.

2. In the Available Merge Fields section, select the Field Type, then select the Field that you must merge in the template.
Copy the generated Merge field value.

3. Return to the template setup and click the Edit Template button.

4. In the Email Content, enter the Field Name, and after that, paste the Merge Field Value and click on the Save button.

The Merge field will be added to the Email Template as we click the Save button.
Modify a Visualforce Email Template for Guest Users
To protect access to organization data while sending emails that use Visualforce Classic email templates to guest users, we need to modify the access of Visualforce Email Templates for Guest Users.
Add the Author Apex user permission
To create or change Classic Visualforce email templates, we need to give the Author Apex access to the user and to do that, navigate to the setup and follow the below steps.
1. On the Setup page of Salesforce, search and select Profiles in the Quick Find box.

2. In the profile setup window, create a new profile or click on the Edit button of the existing profile.
3. In the section Administrative Permissions, activate the Author Apex checkbox and click Save button.

With this, the user will have access to Modify the Visualforce Email Template.
Modify and Edit Visualforce email template
To modify the Visualforce email template for guests, navigate to the setup page and follow the below steps.
1. On the Setup page, search and select Classic Email Templates in the Quick Find box.

2. Click the Name of a Visualforce-type template that you have to modify for Guest User access.

3. Click Edit Template.

4. In the Email content, add the below code to the email Template tag.
renderUsingSystemContextWithoutSharing="True"After that, click on the Save button.

When we add renderUsingSystemContextWithoutSharing=”True”, the email template runs in system mode, which bypasses user permissions, field-level security, and organization-wide defaults.
Visualforce Email Template Conditional Rendering
In Salesforce, Visualforce email templates allow us to create personalized emails based on specific conditions using Visualforce markup. Conditional rendering in these templates can be done using Visualforce components like <apex:if> and <apex:rendered>
Let’s consider a scenario, where we want to send a personalized email to contacts that includes a special offer if they are a VIP customer. If the contact is not a VIP, the email will be send with general information.
According to the scenario, I have created a custom Checkbox field VIP customer in the Account object.
Now to create a Visualforce email template conditional rendering, navigate to Classic Email Tempalte setup and follow below steps.
1. On the Classic Email Templates setup page, click on the New Template button.

2. Enter the Email Template Name and Template Unique Name will be auto-filled.
According to the scenario, I have entered Email Subject as Exclusive VIP offer, Receipient Type as Contact, and Related to type as Account.
After this, click on the Save button.

3. In the Email template section, click on Edit Tempalte button.

4. In the below code, we have rendered If-else conditionals to display a message if the Contact field VIP Customer is checked(true).

<messaging:emailTemplate subject="Exclusive VIP Offer Just for You!" recipientType="Contact" relatedToType="Account">
<messaging:htmlEmailBody>
<html>
<body>
<h1>Dear {!recipient.Name},</h1>
<p>We have some exciting news for you!</p>
<!-- Conditional rendering with IF for VIP status -->
<apex:outputPanel rendered="{!relatedTo.Is_VIP__c}">
<h2>Exclusive Offer for VIP Customers!</h2>
<p>As a valued VIP customer, you are entitled to a special discount on your next purchase.</p>
</apex:outputPanel>
<apex:outputPanel rendered="{!NOT(relatedTo.Is_VIP__c)}">
<h2>Thank You for Being with Us!</h2>
<p>We appreciate your loyalty. Stay tuned for upcoming offers and news.</p>
</apex:outputPanel>
<p>Best regards,</p>
<p>Your Company Team</p>
</body>
</html>
</messaging:htmlEmailBody>
</messaging:emailTemplate>In the above code, we have used the <apex:if> tag to check if the related Account record’s Is_VIP__c field is true. If the condition is true, the email includes a special section for VIP customers.
The <apex:else> tag handles the case when the Is_VIP__c field is false and will display a different message for non-VIP customers.
If you face any error, regarding the conditional field then you can get the API Name of field from the Merge fields feature.
This way, we can do conditional rendering in Visualforce Email Templates.
You may also like to read the following Salesforce tutorials.
- How to Test Email Deliverability in Salesforce
- How to Setup Email Deliverability Settings in Salesforce
- Enhanced Letterhead in Salesforce Lightning
- How to send email in Salesforce
- How to Mass Change Email Templates Folder with Salesforce Data Loader
- How to Use Apex Controllers with Visualforce Pages?
- Create Forms using Visualforce Page in Salesforce
Conclusion
In Salesforce, using the Visualforce Email template allows one to perform advanced operations on data that are sent to a recipient. In this Salesforce tutorial, we have learned the process of Creating a Visualforce email template in Salesforce and about Merging the fields in a Visualforce email template.
In addition, we learned about Modifying a Visualforce Email Template for Guest Users and rendering conditionals in Visualforce email templates.
By following the above steps, you can create and modify the Visualforce Email template in your Salesforce instance.
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.