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 email content.

Developers and administrators can use Visualforce to create email templates. The advantage of using Visualforce over standard HTML email templates is that Visualforce gives you the ability to perform advanced operations on data that is sent to a recipient.

All Visualforce email templates must be contained within a single <messaging:emailTemplate> tag. This is analogous to regular Visualforce pages being defined within a single <apex:page>tag.

  • The <messaging:emailTemplate> tag must contain either a single <messaging:htmlEmailBody> tag or a single <messaging:plainTextEmailBody> tag.
  • Several standard Visualforce components are not available for use within <messaging:emailTemplate>. These include <apex:detail>,<apex:pageBlock> and all related pageBlock components, and all input components such as <apex:form>. If you attempt to save a Visualforce email template with these components, an error message displays.

Do one of the following:

  • If you have permission to edit public templates, from Setup, enter Email Templates in the Quick Find box, then select Classic Email Templates.
  • If you do not have permission to edit public templates, go to your personal settings. Enter Templates in the Quick Find box, then select Email Templates or My Templates—whichever one appears.
  • 1. Click New Template.
  • 2. Choose Visualforce and click Next.
  • 3. You cannot send a mass email using a Visualforce email template.
  • 4. Choose a folder in which to store the template.
  • 5. To make the template available for use, select the Available for Use checkbox.
  • 6. Enter a name in Email Template Name.
  • 7. If necessary, change the Template Unique Name. 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 only underscores and alphanumeric characters, and must be unique in your org. It must begin with a letter, not include spaces, not end with an underscore, and not contain two consecutive underscores. With the Template Unique Name field, you can change certain components’ names in a managed package and the changes are reflected in a subscriber’s organization.
  • 8. If desired, choose a different character set from the Encoding dropdown list.
  • 9. Enter a description for the template. Both template name and description are for your internal use only.
  • 10. Enter a subject line for your template in Email 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 Type dropdown list, select the object from which the template retrieves merge field data.
  • 13. 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 you are including an image, we 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 you’ve installed managed packages from the AppExchange, you can also specify which version of each managed package to use with this email template. Generally, use the default value for all versions, to associate the email template with the most 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.
  • 2. 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 can save your template.
hire salesforce developers

/********************* Email Template 1 *************/
Example 1: Contact Cases
Recipient

<messaging:emailTemplate subject="Account and Cases Information {!recipient.Name}" recipientType="Contact" >
<messaging:htmlEmailBody >
    <html>
    <head>
    </head>
    <body>
        Dear {!recipient.Name},<br/>
        Please find your all open cases:<br/>
        <table>
        <tr>
            <th>Case Number</th>
            <th>Case Origin</th>
            <th>Case Status</th>
        </tr>
        <apex:repeat value="{!recipient.cases}" var="case">
        <tr>
            <td>{!case.CaseNumber}</td>
            <td>{!case.Origin}</td>
            <td>{!case.Status}</td>
        </tr> 
        </apex:repeat>
        </table>
    </body>
</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>
    <head>
    </head>
    <body>
        Dear {!relatedTo.Name},<br/>
        Please find your all open cases:<br/>
        <table>
        <tr>
            <th>Case Number</th>
            <th>Case Origin</th>
            <th>Case Status</th>
        </tr>
        <apex:repeat value="{!relatedTo.cases}" var="case">
        <tr>
            <td>{!case.CaseNumber}</td>
            <td>{!case.Origin}</td>
            <td>{!case.Status}</td>
        </tr> 
        </apex:repeat>
        </table>
    </body>
</html>
</messaging:htmlEmailBody>
<messaging:plainTextEmailBody >
Congratulations!
This is your new Visualforce Email Template.
</messaging:plainTextEmailBody>
</messaging:emailTemplate>

Example 3 : Student Fees Details Using Component

<messaging:emailTemplate subject="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 Email Templates ************/

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

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

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

public class StudentFeesController{
    public Id studentId {get;set;}
    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>
            <apex:pageBlockButtons>
                <apex:commandButton value="Send Contact Cases" action="{!sendCases}"/>
                <apex:commandButton value="Send Account Cases" action="{!sendAccountCases}"/>
                <apex:commandButton value="Send Fees Details" action="{!sendFeeDetails}"/>
                
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

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

public class 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 get in touch with our salesforce consulting team.

Author

With a decade of experience in eCommerce technologies and CRM solutions, Virendra has been assisting businesses across the globe to harness the capabilities of information technology by developing, maintaining, and improving clients’ IT infrastructure and applications. A leader in his own rights his teammates see him as an avid researcher and a tech evangelist. To know how the team Virendra can assist your business to adopt modern technologies to simplify business processes and enhance productivity. Let’s Talk.