Normally to send an email with a template this is how you would do it.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setTargetObjectId(contact); mail.setTemplateId(templateID); mail.setWhatId(orderID);
- You set the TargetObjectId which then is used to get an recipient email address.
- You tell it which template to use by ID.
- You tell it which object instance to reference by ID.
It seems that you cannot override the contact/targetobject email by manually setting the email to address:
This is a problem when you don't have an existing contact and you don't want to create one just to send an email.
So what is the best way to send an email using a common template that uses fields from an object to generate a dynamic html email body?
I know one way is to set a static resource with HTML and dynamically manipulate that HTML upon referencing it.