Monday, November 24, 2014

Design a Visualforce Mail Template


Are you looking around on "how to design a Visualforce mail template?" Let’s see how we can design the template with images and styles.

    Steps to create a Visualforce Email Template:
  1.  Administer – Communication TemplatesEmail TemplatesNew Template
  2.  Select the type of email template as Visualforce.
  3.  Select the folder where you want to store your template.     
  4.  Select the Available for use check box to make it available to users when sending mail.
  5.  Name your email template.     
  6.  Select an Encoding setting to determine the character set for the template.
  7.   Enter the Email Subject(i.e., Here I entered it as Welcome)      
  8.  Select the Recipient type (Contact, Lead and User – One who receives the mail)
  9.  If you want to merge fields in your template then select the object you want in the  Related to  Type.      
  10.  Once you click save, click edit template to write Visualforce to design the template.
      Code Snippet:
      
<messaging:emailTemplate subject="Welcome" recipientType="Contact" >
  <messaging:htmlEmailBody>
      <html>
         <body>
           <table border="0" cellpadding="0" cellspacing="0">
              <tr>
               <td><apex:imageurl="https://instanceurl/servlet/servlet.ImageServer?id=yourimagedocumentid&oid=Organizationid"
width="38" height="38"/></td>
              </tr>
          </table>
       </body> 
     </html>
  </messaging:htmlEmailBody>
</messaging:emailTemplate>






For complete code visit the github link here.

Note:
 
Add an image to the documents tab. (To add image in the Visualforce mail template)
Once saved the image, get the image URL by right click on it.
Add that image URL to the following apex tag,
<apex:image url="https://instanceurl/servlet/servlet.ImageServer?id=your imagedocumentid&oid=Organizationid" width="38" height="38"/>



Screenshot for the Visualforce Mail Template


4 comments: