I'm attempting send a Text email from Apex code, Email is sending without body.
I have created a new LWC button and Placed in custom Object Detailed page. On Clicking of the button this Apex method will be called and Send email to the User.( Email address are stored in Same Custom Object).
I'm successfully receiving the email, subject is coming across as I expect, but the body has some dynamic value.(Now the body coming as plain text)
public static void sendEnrollmentForm(Id EnrollmentId){
//Get Email Address
Enrollment__c Enrollment = [select Id, Family_Email__c from Enrollment__c WHERE Id=:EnrollmentId];
// get an email template
EmailTemplate tpl = (EmailTemplate)[select Id, Subject, Body FROM EmailTemplate
WHERE Name = 'Enroll Email' limit 1];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {Enrollment.Family_Email__c};
mail.setTemplateId(tpl.Id);
mail.setToAddresses(toAddresses);
mail.setReplyTo('support@safarikid.com');
mail.setSenderDisplayName('Safari Kid');
mail.setSubject(tpl.Subject);
mail.setPlainTextBody(tpl.Body);
mail.setBccSender(false);
mail.setSaveAsActivity(false);
mail.setUseSignature(false);
//send the email
List<Messaging.SendEmailResult> results = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
// we can then check for success
if (results[0].success) {
System.debug('The email was sent successfully.');
} else {
System.debug('The email failed to send: ' + results[0].errors[0].message);
}
}
body
field from EmailTemplate object. Second you are not settingsetHtmlBody
orsetPlainTextBody
in your code either. – Mahmood Mar 19 '20 at 10:49