VF Email Templates in Salesforce – How to?

Salesforce provides email templates for sending an email from Salesforce to Contacts, User and Lead etc. There are three types of email templates:

  1. Text Template
  2. HTML Template.
  3. Custom (Without using Letterhead)
  4. Visualforce Template

Let’s explore Visualforce email template and go over its use cases. Visualforce email template allows developer to access and show the relational data like ( Parent to Child and Vice-Versa) in rows and column format. We can also include a Visualforce Component in a Visualforce email template which allows us to perform some actions before showing the data.

How we can add attachments in Visualforce email template ?

Developer might need to attach the documents while sending an email using Visualforce email template. Salesforce allows to add attachments in visualforce email template by using getContentAsPDF() method of PageReference class. This method was not available in asynchronous call before Winter’16. Now, this method is treated as callout in apex.

Apex Code:

public PageReference sendAttachment() {

PageReference pdf = Page.AccountPage;

//Setting AccountId to the id parameter

// content of attachment
Blob body;

// returns the output of the page as a PDF
body = pdf.getContentAsPDF();

Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
attach.Body = body;

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] { ‘test@test.com’ });
mail.setSubject(‘Account Details’);
mail.setHtmlBody(‘Please find the attachment’);
mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });

// Send the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });


Use Custom Controllers with Visualforce Email Template

Developer needs to perform some operation on data before sending them via email. We can add controller to the Visualforce component to perform business logic and this component will work as a normal visualforce page. Developer must to add the component on the Visualforce template using .

For e.g. We want to show the list of Contacts for an Account in a Visualforce template with rows and column and one column should show the name of related Account per contact.This example will show accessing data from Parent to Child.

It requires controller because we need to fetch the name of Account from Contact.(Child to Parent).

  1. We need to create a component which contains the UI part of the template:
  2. We need to create controller which handles SOQL and relationship query to show the data on component.
  3. Visualforce email template holds the component markup.

Visualforce Component:-

<apex:attribute name="accId" assignTo = "{!accId}" type="String" required="true"/>


 public class AcountController {
 private final List cons;
 public Id accId {get;set;}
 public AcountController() {
 cons = [ Select Id,FirstName, LastName, Email, Phone From Contact Where AccountId =: accId ];

 public List getContacts() {
 return cons;

Visualforce Template:



Senior Salesforce Architect
In Connect me on LinkedIn
twitter Follow me on Twitter @apexcoder
blogger Subscribe to my blog https://apexcoder.com
email Email me ajomon@apexcoder.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s