How to create Visualforce Email Template in Salesforce

In this Salesforce tutorial, we will learn how to create visualforce email template in salesforce lightning and salesforce classic.

One day, while working for a sales organization that makes use of salesforce, I was asked to send an email containing information depending on a condition.

After doing some research, I discovered that salesforce offers us a visualforce email template that renders information conditionally.

You’ve come to the right place if you use salesforce and want to learn how to send conditional information-rendering emails to clients. The topics that we will cover in this Salesforce tutorial are as follows.

  • Visualforce email template
  • Visualforce Vs HTML email template
  • Visualforce Important Points
  • How to create visualforce email template in salesforce lightning
  • How to create visualforce email template in salesforce classic

Visualforce email template

Every salesforce administrator and developer is familiar with the ideas behind email templates. And visualforce email template is one kind of email template that we have in salesforce.

When you need to render information conditionally or display a list of related records, Visualforce templates are useful. Sending emails with dynamic content and styling is made possible by this email template also.

And by executing some logic inside the Visualforce controller before sending the email, we may display specific data within the email. In simple this template, helps the users to send targeted and personalized emails.

Visualforce Vs HTML email template

When compared to normal HTML email templates, utilizing Visualforce gives us the ability to execute advanced operations on data that are sent to a recipient.

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

Before You Start

Before, we start learning to create visualforce email template in salesforce. Let’s first learn to create a new folder in the salesforce classic email template.

Step 1: Search the email template in the Quick Find box.

Step 2: Select the Classic Email Templates.

create new folder in the salesforce classic email template
Classic Email Template in Salesforce Lightning Or Salesforce Classic

Step 3: Click the Create New Folder button.

create new folder classic email template salesforce
Create New Folder in Classic Email Template in Salesforce Lightning Or Salesforce Classic

Step 4: Provide an Email Template Folder Label, change Public Folder Access to Read/Write and make this folder accessible to all users.

Step 5: Finally, click the Save button.

create new folder in the salesforce classic email template
Fill up the classic email template new folder details in salesforce lightning Or salesforce classic

With this, we have learned to create a New Folder in the classic email template in salesforce. Now let’s move ahead and learn to create visualforce email template in salesforce.

How to create visualforce email template in salesforce lightning

If you want to learn how to create visualforce email template in salesforce lightning, follow the below-given steps.

Step 1: Search the email template in the Quick Find box.

Step 2: Select the Classic Email Templates.

create new folder in the salesforce classic email template
Classic Email Template in Salesforce Lightning

Step 3: Then, click the New Template button.

How to create visualforce email template in salesforce lightning
Select New Template Button

Step 4: Click the VisualForce radio button and then click on Next.

create visualforce email template in salesforce lightning
Select Visualforce Email Template in Salesforce Lightning

Step 5: Select the Folder in which you want to create your new template. Note: If you have already selected the folder, there is no need to select it again.

Step 6: If you want to make it available for use, tick the checkbox.

Step 7: Set the Email Template Name, and it automatically set the Template Unique Name according to it.

Step 8: Next, set the Encoding to Unicode (UTF-8).

Step 9: Add the Description and Subject if you want as both fields are not mandatory.

Step 10: Then, choose wisely the Email Subject, Recipient Type, and Related To Type.

Step 11: 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.

Step 12: Finally, click on the Save button.

create visualforce email template in salesforce lightning
Enter Email Template Information and Attributes in Salesforce Lightning

Step 13: Click on the Edit Template button to create a Visualforce component and a controller.

create visualforce email template in salesforce lightning example
Edit Template in Classic Email Template Salesforce Lightning

Step 14: 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>

Step 15: Click on the Save button.

How to create visualforce email template in salesforce lightning example
Visualforce Email Contact Template Code in Salesforce Lightning

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.

create visualforce email template in salesforce example
Visualforce Classic Email Template View in Preview Email

With this, we have learned to create visualforce email template in salesforce lightning.

How to create visualforce email template in salesforce classic

If you want to learn how to create visualforce email template in salesforce classic, follow the below-given steps.

Step 1: Click on avatar and select Switch to Salesforce Classic.

How to create visualforce email template in salesforce classic
Switch to Salesforce Classic

Step 2: Click on Setup.

create visualforce email template in salesforce classic
Select Salesforce Classic Setup

Step 3: Search the email template in the Quick Find box.

Step 4: Select Classic Email Templates under Communication Templates.

create visualforce email template in salesforce classic example
Select Classic Email Templates in Salesforce Classic

Step 5: Next, select New Template from the menu.

visualforce email template in salesforce classic
Select New Template in Salesforce Classic

Step 6: Choose VisualForce from the radio buttons, then select Next.

visualforce email template in salesforce classic example
Choose Visualforce type of email template

Step 7: Choose the Folder where you want to save the new template. It is not necessary to pick the folder again if you have previously done so.

Step 8: Tick the checkbox to make it usable if you want to.

Step 9: Enter the Email Template name; the template unique name will be set automatically based on it.

Step 10: Next, select Unicode as the Encoding (UTF-8).

Step 11: You can optionally fill up the Description and Subject fields.

Step 12: Choose wisely the Email Subject, Recipient Type, and Related To Type.

Step 13: I’ll be designing an Account and Contact Information template. Name it Contact Information, and make the Subject of the email Account and Contact Information. The Contact will be the Recipient Type.

Step 14: Finally, press the Save button.

create visualforce email template in salesforce classic
Setup Visualforce Email Template in Salesforce Classic

Step 15: Click on the Edit Template button to add Visualforce code.

How to create visualforce email template in salesforce classic example
Edit Template in Salesforce Classic

Step 16: Paste the following code in the Email Content box.

<messaging:emailTemplate subject="Account and Contact Information {!recipient.Name}" recipientType="Contact" >
<messaging:htmlEmailBody >
    <html>
    <head>
    </head>
    <body>
        Dear {!recipient.Name},<br/>
        Please find your account details:<br/>
        <table border="1">
        <tr>
            <th>Account Name</th>
            <th>Account Number</th>
            <th>Account Ownership</th>
            <th>Account Ticker Symbol</th>
        </tr>
        <apex:repeat value="{!recipient.account}" var="account">
        <tr>
            <td>{!account.Name}</td>
            <td>{!account.AccountNumber}</td>
            <td>{!account.Ownership}</td>
            <td>{!account.TickerSymbol}</td>
        </tr> 
        </apex:repeat>
        </table>
    </body>
</html>
</messaging:htmlEmailBody>
</messaging:emailTemplate>

Step 15: Click on the Save button.

create visualforce email template using salesforce classic
Add Visualforce Email Content in Salesforce Classic

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.

create visualforce email template using salesforce classic example
Visualforce Email Template View

With this, we have learned to create visualforce email template in salesforce classic.

You may also like to read the following Salesforce tutorials.

Conclusion

In this tutorial, we have learned about visualforce email template and its important points. We have also learned the difference between Visualforce and HTML email templates.

Additionally, we have learned to create visualforce email templates in salesforce lightning and salesforce classic step by step.

  • Visualforce email template
  • Visualforce Vs HTML email template
  • Visualforce Important Points
  • How to create visualforce email template in salesforce lightning
  • How to create visualforce email template in salesforce classic