Last Updated on January 21, 2022

With Visualforce email templates, users can send targeted and personalized emails. Template creators can harness the powerful Visualforce programming language to create templated electronic mail content.

Developers and administrators can apply Visualforce to create e-mail templates. The reward of using Visualforce over standard HTML email templates is that Visualforce gives you the ability to perform avant-garde operations on data that is sent to a recipient.

All Visualforce e-mail templates must be independent within a single <messaging:emailTemplate> tag. This is analogous to regular Visualforce pages being defined inside a unmarried <apex:folio>tag.

  • The <messaging:emailTemplate> tag must contain either a single <messaging:htmlEmailBody> tag or a unmarried <messaging:plainTextEmailBody> tag.
  • Several standard Visualforce components are non bachelor for use inside <messaging:emailTemplate>. These include <apex:detail>,<apex:pageBlock> and all related pageBlock components, and all input components such equally <noon:class>. If y'all attempt to save a Visualforce email template with these components, an fault message displays.

Do one of the following:

  • If you lot have permission to edit public templates, from Setup, enter Email Templates in the Quick Find box, then select Archetype Email Templates.
  • If yous do non have permission to edit public templates, get to your personal settings. Enter Templates in the Quick Find box, then select Electronic mail Templates or My Templates—whichever 1 appears.
  • i. Click New Template.
  • 2. Choose Visualforce and click Next.
  • 3. You cannot transport a mass email using a Visualforce email template.
  • 4. Choose a folder in which to store the template.
  • 5. To make the template bachelor for use, select the Available for Use checkbox.
  • 6. Enter a name in E-mail Template Name.
  • seven. If necessary, change the Template Unique Proper noun. This unique name refers to the component when you use the Lightning Platform API. In managed packages, this unique name prevents naming conflicts in package installations. This name can contain merely underscores and alphanumeric characters, and must be unique in your org. It must begin with a alphabetic character, not include spaces, non end with an underscore, and not incorporate two consecutive underscores. With the Template Unique Name field, yous can change certain components' names in a managed package and the changes are reflected in a subscriber'south organization.
  • 8. If desired, choose a different graphic symbol set from the Encoding dropdown list.
  • 9. Enter a description for the template. Both template proper name and description are for your internal use only.
  • x. Enter a subject line for your template in Electronic mail Subject.
  • 11. In the Recipient Type dropdown list, select the type of recipient to receive email created from the template.
  • 12. If desired, in the Related To Blazon dropdown list, select the object from which the template retrieves merge field data.
  • xiii. Click Save.
  • 14. On the View and Edit Email Templates in Salesforce Classic page, click Edit Template.
  • 15. Enter markup text for your Visualforce email template.

NOTE: If y'all are including an image, nosotros recommend uploading it to the Documents tab to reference the copy of the image on our server. For example:

<apex:image id="Logo" value="https://yourInstance.salesforce.com/servlet/servlet.ImageServer?
id=015D0000000Dpwc&oid=00DD0000000FHaG&lastMod=127057656800″ />

  • 1. To specify the version of Visualforce and the API used with this email template, click Version Settings. If yous've installed managed packages from the AppExchange, y'all can also specify which version of each managed package to apply with this e-mail template. Mostly, utilise the default value for all versions, to associate the e-mail template with the almost recent version of Visualforce, the API, and each managed package. To maintain specific behavior, you can specify an older version of Visualforce and the API. To access components or functionality that differ from the most recent package version, you can specify an older version of a managed package.
  • two. To view the details of the template, click Save. To continue editing your template, click Quick Save. Your Visualforce markup must be valid before you tin can save your template.

hire salesforce developers

/********************* Electronic mail Template 1 *************/
Instance 1: Contact Cases
Recipient

<messaging:emailTemplate subject field="Account and Cases Information {!recipient.Name}" recipientType="Contact" > <messaging:htmlEmailBody >     <html>     <head>     </caput>     <body>         Dearest {!recipient.Name},<br/>         Delight find your all open cases:<br/>         <table>         <tr>             <thursday>Case Number</th>             <th>Case Origin</th>             <th>Example Status</th>         </tr>         <apex:echo value="{!recipient.cases}" var="case">         <tr>             <td>{!case.CaseNumber}</td>             <td>{!case.Origin}</td>             <td>{!case.Status}</td>         </tr>          </apex:echo>         </table>     </torso> </html> </messaging:htmlEmailBody> <messaging:plainTextEmailBody > Congratulations! This is your new Visualforce Email Template. </messaging:plainTextEmailBody> </messaging:emailTemplate>            

/******************** Email Template 2 ***************/
Example 2: Account Cases
Recipient and RelatedTo

<messaging:emailTemplate subject="Account Cases as Related To : {!relatedTo.Name}" recipientType="Contact" relatedToType="Account"> <messaging:htmlEmailBody >     <html>     <caput>     </head>     <body>         Dear {!relatedTo.Proper name},<br/>         Please find your all open cases:<br/>         <table>         <tr>             <th>Case Number</thursday>             <th>Example Origin</th>             <thursday>Case Status</th>         </tr>         <apex:repeat value="{!relatedTo.cases}" var="case">         <tr>             <td>{!case.CaseNumber}</td>             <td>{!example.Origin}</td>             <td>{!case.Status}</td>         </tr>          </apex:repeat>         </tabular array>     </trunk> </html> </messaging:htmlEmailBody> <messaging:plainTextEmailBody > Congratulations! This is your new Visualforce Email Template. </messaging:plainTextEmailBody> </messaging:emailTemplate>            

Case 3 : Student Fees Details Using Component

<messaging:emailTemplate subject area="Submission Of Student Fees" recipientType="Contact" relatedToType="Student__c"> <messaging:htmlEmailBody >     <c:StudentFeesDetails stuId="{!relatedTo.Id}"></c:StudentFeesDetails> </messaging:htmlEmailBody> <messaging:plainTextEmailBody > Congratulations! This is your new Visualforce Email Template. </messaging:plainTextEmailBody> </messaging:emailTemplate>            

/*************** End Electronic mail Templates ************/

/******************* Component ******************/

<apex:component controller="StudentFeesController" access="global">     <Style>     table{border:1px solid #000;}         td,th {edge: 1px solid #000}     </way>     <apex:attribute name="stuId" assignTo="{!studentId}"  type="Id" clarification="Student Id" />     <h1>Your Fees Deails</h1>     <table >     <tr>         <th>Date</thursday>         <th>Amount</thursday>     </tr>     <apex:repeat value="{!feesDetails}" var="fee"     >         <tr>             <td><apex:outputField value="{!fee.date__c}" /></td>             <td>{!fee.amount__c}</td>         </tr>     </apex:repeat>     </tabular array> </noon:component>            

/************** Class ******************/

public class StudentFeesController{     public Id studentId {go;ready;}     public List<Fees__c> getFeesDetails(){         return [select id, amount__c, date__c from fees__c where student_Id__c =: studentId];     } }            

/******************* Page **********************/

<apex:page controller="SendVFEmailTemplateController">     <apex:form>         <apex:pageBlock>             <noon:pageBlockButtons>                 <apex:commandButton value="Ship Contact Cases" activeness="{!sendCases}"/>                 <noon:commandButton value="Send Account Cases" action="{!sendAccountCases}"/>                 <noon:commandButton value="Ship Fees Details" action="{!sendFeeDetails}"/>                              </apex:pageBlockButtons>         </noon:pageBlock>     </apex:class> </apex:page>            

/******************* Grade ********************/

public grade SendVFEmailTemplateController{     EmailTemplate et;     public SendVFEmailTemplateController(){          }          public void sendCases(){         et = [select Id, Name from EmailTemplate where DeveloperName = 'Account_Cases'];         Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage ();         semail.setTargetObjectId('00328000005cwjF');         semail.setTemplateId(et.Id);         Messaging.sendEmail(new Messaging.SingleEmailMessage[] {semail});     }          public void sendAccountCases(){         et = [select Id, Name from EmailTemplate where DeveloperName = 'Account_s_Cases'];         Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage ();         semail.setTargetObjectId('00335672305cwjF');         semail.setTemplateId(et.Id);         semail.setWhatId('00123400006azlA');         Messaging.sendEmail(new Messaging.SingleEmailMessage[] {semail});     }          public void sendFeeDetails(){         et = [select Id, Name from EmailTemplate where DeveloperName = 'Student_Fees_Info'];         Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage ();         semail.setTargetObjectId('00335672305cwjF');         semail.setTemplateId(et.Id);         semail.setWhatId('a002320080DGNjx');         Messaging.sendEmail(new Messaging.SingleEmailMessage[] {semail});             } }            

We hope this blog helped you in learning how to use Visualforce email templates in Salesforce. If you require salesforce development services then become in touch with our salesforce consulting squad.