Visualforce Email Templates
We can also use Visualforce to create email templates. The advantage of using Visualforce over standard HTML email templates is that Visualforce offers you the power to perform advanced operations on data that's sent to a recipient
- Visualforce email template should be within a single <messaging:emailTemplate> tag.
- The <messaging:emailTemplate> tag should contain either a single <messaging:htmlEmailBody> or <messaging:plainTextEmailBody> tag.
- Some standard Visualforce components are not available for use within <messaging:emailTemplate>. For Example <apex:detail>, <apex:pageBlock> and all related pageBlock components, and all input components such as <apex:form>.
- All <apex:component> tags used within a Visualforce email template should have an access level of global.
- The max size of a Visualforce email template is 1MB.
- You can’t send a mass email using a Visualforce email template.
NOTE:-
1) The <messaging:htmlEmailBody> component can include a mix of Visualforce markup and HTML.
2)The <messaging:plainTextEmailBody> component can only include Visualforce markup and plain text
Step by Step process:-
Step 1:- Click on Email Template from start menu.
Step 2 :- Then click on new "New Template" button and select "VisualForce" radio button
Step 3 :- Then Enter all below detail for email template
Step 4 :- Then click on your "Edit Template" button to edit VF page.
Visual force Email Template code :-
<messaging:emailTemplate
subject="Case report for Account: {!relatedTo.name}"
recipientType="Contact"
relatedToType="Account">
<messaging:htmlEmailBody >
<html>
<body>
<p>Dear {!recipient.name},</p>
<table border="0" >
<tr>
<th>Case Number</th><th>Origin</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}">{!cx.CaseNumber}
</a></td>
<td>{!cx.Contact.email}</td>
</tr>
</apex:repeat>
</table>
</body>
</html>
</messaging:htmlEmailBody>
</messaging:emailTemplate>
NOTE:-
In Above example First we created the top level element <messaging:emailTemplate>. Using attributes we declare two different VF standardControllers used by this email template. First, the recipientType also called the WhoID and 2nd the relatedToType also refered to as the whatID in regular email template. We can also set a replyTo address, so that anyone replying to the email can reply back to this address
Using Custom Controllers within Visualforce Email Templates
Visualforce email templates can leverage custom controllers to render highly customized content. To do so, include a custom component in a Visualforce email template that uses that custom controller
<messaging:emailTemplate subject="Apex Code" recipientType="Contact" relatedToType="Opportunity">
<messaging:htmlEmailBody>
<c:cutomComponentName/>
</messaging:htmlEmailBody>
</messaging:emailTemplate>
NOTE:- Sharing settings are applied if your email templates use a standard controller. If your organization-wide default (OWD) for the user object is Private and you need to access user information in your Visualforce email template, you can use a custom component or custom controller with the without sharing keywords
Thanks,
Amit Chaudhary