Skip to content

Instantly share code, notes, and snippets.

Created January 28, 2015 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noeticpenguin/5ab44f6b899210ce5ee9 to your computer and use it in GitHub Desktop.
Save noeticpenguin/5ab44f6b899210ce5ee9 to your computer and use it in GitHub Desktop.
public class mailUtils {
public class mailUtilsException extends exception {}
public Boolean useSig {get; private set;}
public Boolean saveActivity {get; private set;}
public String senderDisplayName {get; private set;}
public mailUtils(Boolean useSig, Boolean saveActivity, String senderDisplayName){
this.useSig = usesig;
this.saveActivity = saveActivity;
this.senderDisplayName = senderDisplayName;
// Derived from:
public Messaging.SingleEmailMessage MergeTemplateWithoutSending(Id targetObjectId, Id templateId) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// Intentionally set a bogus email address.
String[] toAddresses = new String[]{''};
// create a save point
Savepoint sp = Database.setSavepoint();
// Force the merge of the template.
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
// Force a rollback, and cancel mail send.
// Return the mail object
// You can access the merged template, subject, etc. via:
// String mailTextBody = mail.getPlainTextBody();
// String mailHtmlBody = mail.getHTMLBody();
// String mailSubject = mail.getSubject();
return mail;
public static Map<String,String> getMergedTemplateForObjectWithoutSending(Id targetObjectId, Id templateId, Boolean useSig, Boolean saveActivity, String senderDisplayName) {
Map<String,String> returnValue = new Map<String,String>();
mailUtils mu = new mailUtils(useSig, saveActivity, senderDisplayName);
Messaging.SingleEmailMessage mail = mu.MergeTemplateWithoutSending(targetObjectId, templateId);
returnValue.put('textBody', mail.getPlainTextBody());
returnValue.put('htmlBody', mail.getHTMLBody());
returnValue.put('subject', mail.getSubject());
return returnValue;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment