I did something similar for previewing an email before it's being sent. The idea is to set up two emails in a list, one correctly and the other one not, try and send them and retrieve the generated email body in the catch statement:
Messaging.SingleEmailMessage[] previewEmails = new Messaging.SingleEmailMessage[]{}; Messaging.SingleEmailMessage firstPreviewEmail = new Messaging.SingleEmailMessage(); firstPreviewEmail.setUseSignature(false); firstPreviewEmail.setSaveAsActivity(false); firstPreviewEmail.setTemplateId(templateId); firstPreviewEmail.setTargetObjectId(UserInfo.getUserId()); firstPreviewEmail.setWhatId(dealId); previewEmails.add(firstPreviewEmail); Messaging.SingleEmailMessage secondPreviewEmail = new Messaging.SingleEmailMessage(); previewEmails.add(secondPreviewEmail); try { List<Messaging.SendEmailResult> previewResult = Messaging.sendEmail(previewEmails); } catch (Exception e) { generatedEmailBody = firstPreviewEmail.getHtmlBody(); // Email body generated }
Messaging.SingleEmailMessage[] previewEmails = new Messaging.SingleEmailMessage[]{};
Messaging.SingleEmailMessage firstPreviewEmail = new Messaging.SingleEmailMessage();
firstPreviewEmail.setUseSignature(false);
firstPreviewEmail.setSaveAsActivity(false);
firstPreviewEmail.setTemplateId(templateId);
firstPreviewEmail.setTargetObjectId(UserInfo.getUserId());
firstPreviewEmail.setWhatId(dealId);
previewEmails.add(firstPreviewEmail);
Messaging.SingleEmailMessage secondPreviewEmail = new Messaging.SingleEmailMessage();
previewEmails.add(secondPreviewEmail);
try {
List<Messaging.SendEmailResult> previewResult = Messaging.sendEmail(previewEmails);
}
catch (Exception e) {
generatedEmailBody = firstPreviewEmail.getHtmlBody(); // Email body generated
}