Find your content:

Search form

You are here

How to send VisualForce Email using a template from a trigger


I am trying to send an email from trigger using SingleEmailMessage.

As of now, I am composing my email body using the method setHTMLBody inside the trigger itself and the email recipients are: user, contact and some hard-coded email address also.

My email has many recipients and I have some complex logic so I have to go for visualforce email template.

While I tried, I found the following points:

  1. I have to use singleemailmessage only as I have to use VisualForce email template
  2. More than one id cannot be added in the setTargetObjectId method so I cannot mail to more than one person.
  3. Also if I use an email template, I have to specify the recipient type as User/Lead/Contact but in my case I have to send email to user & contact & some email address. As of now, I am doing it using setToAddresses method.

My Question: Is it possible to replace the email body using VisualForce email template instead of composing it using setHTMLBody method?

Attribution to: Priyanka

Possible Suggestion/Solution #1

In order to use VF Email templates, you have to send an email to a Lead, Contact, or User via setTargetObjectId with the associated merge fields.

I don't believe you can freeform an email address.

Also be aware sending single emails you max out at 10 emails per request, so you'll have to make sure that your trigger doesn't send anymore than 10 emails.

If it does, you'll need to use 'Bulk email' which doesn't allow templates either.

Attribution to: jordan.baucke

Possible Suggestion/Solution #2

I wrote a blog on how to approach through workflows and visualforce template .Hope this may help in alternate designing

Attribution to: Mohith Shrivastava
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content