Newer Version Available

This content describes an older version of this product. View Latest

Create a Visualforce Email Template

Use the Visualforce programming language to create email templates.
This example shows how you can define a Visualforce email template that displays all the cases associated with a contact. It uses an <apex:repeat> tag to iterate through all the cases related to a contact and incorporate them into the body of the template:

Notice the following about the markup:

  • The attributes recipientType and relatedToType act as controllers for the email template. With them you can access the same merge fields that are available to other standard controllers. The recipientType attribute represents the recipient of the email. The relatedToType attribute represents the record to associate with the email.
  • The <messaging:htmlEmailBody> component can include a mix of Visualforce markup and HTML. The <messaging:plainTextEmailBody> component can only include Visualforce markup and plain text.
  • To translate Visualforce email templates based on recipients’ or related objects’ languages, use the <messaging:emailTemplate> tag's language attribute (valid values: Salesforce supported language keys, for example, “en-US”). The language attribute accepts merge fields from the email template's recipientType and relatedToType attributes. You create custom language fields for use in the merge fields.

    The Translation Workbench is required to translate email templates.

    Note

    The example uses a merge field to obtain a language attribute for the contact receiving the email.
  1. Choose one of these options.
    • If you have permission to edit public templates, from Setup, enter Email Templates in the Quick Find box, then select Classic Email Templates.
    • If you don't have permission to edit public templates, go to your personal settings. Enter Templates in the Quick Find box, then select Email Templates or My Templates—whichever one appears.
  2. Click New Template.
  3. Choose Visualforce and click Next.
  4. Choose a folder in which to store the template.
  5. To make the template available for use, select the Available For Use checkbox.
  6. Enter an email template name.
  7. If necessary, change the Template Unique Name. This unique name refers to the component when you use the Lightning Platform API. In managed packages, this unique name prevents naming conflicts in package installations. This name can contain only underscores and alphanumeric characters, and must be unique in your org. It must begin with a letter, not include spaces, not end with an underscore, and not contain two consecutive underscores. With the Template Unique Name field, you can change certain components' names in a managed package and the changes are reflected in a subscriber's organization.
  8. If desired, choose a different character set from the Encoding dropdown list.
  9. Enter a description for the template. Both template name and description are for your internal use only.
  10. Enter a subject line for your template in Email Subject.
  11. In the Recipient Type dropdown list, select the type of recipient to receive email created from the template.
  12. If desired, in the Related To Type dropdown list, select the object from which the template retrieves merge field data.
  13. Click Save.
  14. On the View and Edit Email Templates in Salesforce Classic page, click Edit Template.
  15. Enter markup text for your Visualforce email template.

    If you are including an image, we recommend uploading it to the Documents tab to reference the copy of the image on our server. For example:

    Note

  16. To specify the version of Visualforce and the API used with this email template, click Version Settings. If you’ve installed managed packages from AppExchange , you can also specify which version of each managed package to use with this email template. Generally, use the default value for all versions, to associate the email template with the most recent version of Visualforce, the API, and each managed package. To maintain specific behavior, you can specify an older version of Visualforce and the API. To access components or functionality that differ from the most recent package version, you can specify an older version of a managed package.
  17. To view the details of the template, click Save. To continue editing your template, click Quick Save. Your Visualforce markup must be valid before you can save your template.

    The maximum size of a Visualforce email template is 1 MB.

    You can't send a mass email using a Visualforce email template. The {!Receiving_User.field_name} and {!Sending_User.field_name} merge fields work only for mass email and list email and are unavailable in Visualforce email templates.

    Note