Visualforce Email Template Sample

As of the Winter '09 release you can take advantage of Visualforce Email Templates. The following example describes how you can create a such an email template and pull in related list information.

First we create the top level element <messaging:emailTemplate>. Using attributes we declare two different Visualforce standardControllers used by this email template. First, the recipientType also called the WhoID and secondly the relatedToType also refered to as the whatID in regular email templates. In addition, a replyTo address can be defined, so that anyone replying to the email can reply back to this address.

<messaging:emailTemplate recipientType="Contact"
    relatedToType="Account"
    subject="Case report for Account: {!relatedTo.name}"
    replyTo="support@acme.com" >

Next we define the html content for email template via

<messaging:htmlEmailBody >        

Next we open the html and body tags and include some stylesheets to format the fonts and tables.

    <html>
        <body>
        <STYLE type="text/css">
            TH {font-size: 11px; font-face: arial;background: #CCCCCC; border-width: 1;  text-align: center } 
            TD  {font-size: 11px; font-face: verdana } 
            TABLE {border: solid #CCCCCC; border-width: 1}
            TR {border: solid #CCCCCC; border-width: 1}
         </STYLE>
         <font face="arial" size="2">

Now let's start with the content of our email, adding in merge fields. You might notice below that the merge field notation is {!recipient.name} and not contact.name. The reason for this is that you have two standard controllers and could pass in a contact id for both the whoID and whatID.

<p>Dear {!recipient.name},</p>
<p>Below is a list of cases related to the account: {!relatedTo.name}.</p>

Now to the really interesting part of using Visualforce Email Templates - leveraging related lists. You can use an apex repeat tag to loop through all the cases associated with the Account passed to the email template. We can reference certain fields on the cases and output them in the email in a tabular fashion.

<table border="0" >
    <tr > 
        <th>Action</th><th>Case Number</th><th>Subject</th><th>Creator Email</th><th>Status</th>
    </tr>
    <apex:repeat var="cx" value="{!relatedTo.Cases}">
        <tr>
            <td><a href="https://na1.salesforce.com/{!cx.id}">View</a> |  
            <a href="https://na1.salesforce.com/{!cx.id}/e">Edit</a></td>
            <td>{!cx.CaseNumber}</td>
            <td>{!cx.Subject}</td>
            <td>{!cx.Contact.email}</td>
            <td>{!cx.Status}</td>
        </tr>
    </apex:repeat>                 
</table>
<p />

Finally we close some tags from above.

            </font>
        </body>
    </html>
</messaging:htmlEmailBody> 

Next we include the text version of the email template for email clients unable to display html format. We start by defining the plainTextEmailBody tag and then loop through all the cases but without all the html tags.

<messaging:plainTextEmailBody >
Dear {!recipient.name},
 
Below is a list of cases related to Account: {!relatedTo.name}

[ Case Number ] - [ Subject ] - [ Email ] - [ Status ]

<apex:repeat var="cx" value="{!relatedTo.Cases}">
[ {!cx.CaseNumber} ] - [ {!cx.Subject} ] - [ {!cx.Contact.email} ] - [ {!cx.Status} ]
</apex:repeat>

For more information login to http://www.salesforce.com
</messaging:plainTextEmailBody>    

Finally we end the email template with the closing tag.

</messaging:emailTemplate>

Here is a sample of the output for this email template

VisualforceEmailTemplatePreview.jpg

And here again the full template

<messaging:emailTemplate recipientType="Contact"
  relatedToType="Account"
  subject="Case report for Account: {!relatedTo.name}"
  replyTo="support@acme.com">
  <messaging:htmlEmailBody>
    <html>
      <body>
        <STYLE type="text/css">
          TH {font-size: 11px; font-face: arial;background: #CCCCCC;
               border-width: 1;  text-align: center } 
          TD  {font-size: 11px; font-face: verdana } 
          TABLE {border: solid #CCCCCC; border-width: 1}
          TR {border: solid #CCCCCC; border-width: 1}
        </STYLE>
        <font face="arial" size="2">
          <p>Dear {!recipient.name},</p>
          <p>Below is a list of cases related to the account: {!relatedTo.name}.</p>
          <table border="0" >
            <tr > 
               <th>Action</th>
               <th>Case Number</th>
               <th>Subject</th>
               <th>Creator Email</th>
               <th>Status</th>
            </tr>
            <apex:repeat var="cx" value="{!relatedTo.Cases}">
              <tr>
                <td><a href="https://na1.salesforce.com/{!cx.id}">View</a> |  
                <a href="https://na1.salesforce.com/{!cx.id}/e">Edit</a></td>
                <td>{!cx.CaseNumber}</td>
                <td>{!cx.Subject}</td>
                <td>{!cx.Contact.email}</td>
                <td>{!cx.Status}</td>
              </tr>
            </apex:repeat>                 
          </table>
          <p />
        </font>
      </body>
    </html>
  </messaging:htmlEmailBody> 
  <messaging:plainTextEmailBody >
    Dear {!recipient.name},
    
    Below is a list of cases related to Account: {!relatedTo.name}
    
    [ Case Number ] - [ Subject ] - [ Email ] - [ Status ]
    
    <apex:repeat var="cx" value="{!relatedTo.Cases}">
      [ {!cx.CaseNumber} ] - [ {!cx.Subject} ] - [ {!cx.Contact.email} ] - [ {!cx.Status} ]
    </apex:repeat>
    
    For more information login to http://www.salesforce.com
  </messaging:plainTextEmailBody>    
</messaging:emailTemplate>