send email using standard Email Templates using Apex Salesforce
At times we need to send email alerts using standard email templates using Apex, where these templates are used in Workflow email alerts.
Salesforce provides an Class SingleEmailMessage
to send emails using Apex
Please check following code statements on how to initiate class & invoke for sending emails
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// email Template Id
mail.setTemplateId('00X90000000kRhe');
/* Target Id can be Contact, Lead & User, template Id can be queried using following query please add 'WHERE' condition to your needs
[SELECT Id, Name, DeveloperName, OwnerId, IsActive, TemplateTypeFROM EmailTemplate] */
mail.setTargetObjectId('0039000001HkChu');
mail.setSenderDisplayName('Phanindra Mangipudi');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
NOTE: setTargetObjectId
is
Required if using a template, optional otherwise. The ID of the contact, lead, or user to which the email will be sent. The ID you specify sets the context and ensures that merge fields in the template contain the correct data.
Looking for Send email using Custom Apex click here
For more information on singleMailMessage Methods click here