[SalesForce] Preview email content of email templates before sending

I am developing a feature to send a email from a Custom Visualforce page.
For this I am using the Messaging.SingleEmailMessage class.

I need to give users, the option to select email templates from a set of email templates in the Salesforce 'Email template'.

However i cannot find a way to preview emails which will be sent. My email templates will have data mapped from the respective record. How can i solve this problem of previewing the email templates with the values from email templates?

Please suggest.

Best Answer

In short, there is no simple 'preview' function available to you. You could work around it using Visualforce pages/components for the preview. Assuming you are using Visualforce Email Templates, you could do something like this:

  1. Copy the relevant content out of the Visualforce Email Template into a Visualforce Component
  2. In your Email Template, reference the component e.g. <c:EmailContent obj="{!relatedTo}"/> to render the content
  3. Create a new Visualforce page, also referencing the component (as #2)
  4. Use the Visualforce page just created as a preview

If you wanted to show your user a list of Email templates to choose from, you could simply query the EmailTemplate object with some valid criteria, e.g.

Select Name from EmailTemplate Where IsActive = true
Related Topic