Email scheduler unable to include the letterhead template while sending emails through scheduler. The header and footer not displayed on emails recieved. Only the dynamic htmlValues can be seen in the emails recieved. Checked all the settings of header logo. checked Available for external use, Kept the images in shared folders in Document and externally available as well. Still it didn't work.

How can I include a letter head in the template for sending emails through scheduled jobs?

Below is my sendEmail function in utilityClass which send multiple emails :

public static List < Messaging.Singleemailmessage > sendEmail(List < EmailMessageWrapper > listEmailMessageWrapper, String emailTemplateName) {
    List < Messaging.SendEmailResult > listEmailResult = null;
    List < Messaging.Singleemailmessage > listSingleEmailMessages = new List < Messaging.Singleemailmessage > ();
    EmailTemplate emailTemplate = [Select Id, BrandTemplateId, Subject, HtmlValue, Body, IsActive from EmailTemplate where developerName = : emailTemplateName
    AND IsActive = TRUE
    for (EmailMessageWrapper emailMessageWrapper: listEmailMessageWrapper) {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        if (emailMessageWrapper.FromAddress != null && emailMessageWrapper.FromAddress.length() > 0)

        if (emailMessageWrapper.ToAddress != null && emailMessageWrapper.ToAddress.length() > 0)
            mail.setToAddresses(new String[] {

        if (emailMessageWrapper.BccAddress != null && emailMessageWrapper.BccAddress.length() > 0)
            mail.setBccAddresses(new String[] {
        String subject = null;

        if (emailMessageWrapper.Subject != null && emailMessageWrapper.Subject.length() > 0) {
            subject = emailMessageWrapper.Subject;
        } else
            subject = emailTemplate.Subject;

        for (String key: emailMessageWrapper.ParameterSubjectMap.keySet())
            subject = subject.replace(key, (emailMessageWrapper.ParameterSubjectMap.get(key) == null ? '' : emailMessageWrapper.ParameterSubjectMap.get(key)));

        String htmlBody = emailTemplate.HtmlValue;
        String plainBody = emailTemplate.Body;
        for (String key: emailMessageWrapper.ParameterBodyMap.keySet()) {
            htmlBody = htmlBody.replace(key, (emailMessageWrapper.ParameterBodyMap.get(key) == null) ? '' : emailMessageWrapper.ParameterBodyMap.get(key));
            plainBody = plainBody.replace(key, (emailMessageWrapper.ParameterBodyMap.get(key) == null) ? '' : emailMessageWrapper.ParameterBodyMap.get(key));

        plainBody = '<html>' + plainBody + '<html>';
        htmlBody = htmlBody.replaceAll('<!\\[CDATA\\[', ''); // replace '<![CDATA['
        htmlBody = htmlBody.replaceAll('\\]\\]>', ''); // replace ']]'
    listEmailResult = Messaging.sendEmail(listSingleEmailMessages);
    return listSingleEmailMessages;
  • From the way you've phrased your question, it sounds as though you have a separate letterhead template. I see where you're calling the email template, but I'm not certain whether that would necessarily call the header template or could be an indication of a problem with the path between the two. That might be worth exploring. Might also want to try combining them to see if it solves your problem. If it does, that would narrow it down. – crmprogdev Dec 16 '14 at 15:10
  • I am trying to get a single HTML document consisting of both the BrandTemplate and EmailTemplate values. Somehow I am unable to get brandtemplate along with my emailtemplate. Is there any way where we can get the letterhead along with the email template or rather a way where we can use mail.setTemplateId(template.Id) along with mail.setHtmlBody(htmlBody) ? – Surgaya Khundrakpam Dec 17 '14 at 9:20
  • But if i use mail.setTemplateId(template.Id) only then I am able to recive the proper email template along with the header in the emails but the body and subject data doenot contain the dynamic values as I am not using mail.setHtmlBody(htmlBody). So What i want to know is if there is any way where I can use both mail.setTemplateId(template.Id) and mail.setHtmlBody(htmlBody). – Surgaya Khundrakpam Dec 17 '14 at 9:31

Yes, you can use both the setTemplateId and setHtmlBody methods in your class. In fact, you would need to in order to merge the content with the template as inferred by the documentation.

From the Apex Code Docs, under the Email Class (Base Email methods) section:

setTemplateID(ID) The ID of the template to be merged to create this email. You must specify a value for setTemplateId, setHtmlBody, or setPlainTextBody. Or, you can define both setHtmlBody and setPlainTextBody.

  • I tried using only setTemplateId, setHtmlBody and commented setPlainTextBody but it gives me error -- FATAL_ERROR System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_FIELD_WHEN_USING_TEMPLATE, When a template is specified the plain text body, html body, subject and charset may not be specified : [] – Surgaya Khundrakpam Dec 19 '14 at 6:02
  • As you'll note in the documentation I've quoted above, when using both, it expects you to merge the contents of the HTMLbody with the template. Otherwise you can only use message instance contructors and methods which don't appear to include support for templates. – crmprogdev Dec 19 '14 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.