Messaging.renderStoredEmailTemplate() is rendering Visualforce email template without custom header.

I have a requirement to send an email with custom header. So created a visualforce email template with a custom header. But when I send get the rendered Email template using renderStoredEmailTemplate(). It is not returning with custom header.

Visualforce email template

<messaging:emailTemplate subject="Welcome" recipientType="Contact" relatedToType="Case">

<messaging:emailHeader name="customHeader">
<messaging:plainTextEmailBody >
Sample Visualforce Email Template.


Email Code

List<EmailTemplate> emailTemplate = [SELECT Id, Body, TemplateType  FROM EmailTemplate WHERE DeveloperName = 'Welcome_Letter_VF'];
String templateId = emailTemplate[0].Id;
String emailfooter = 'Message from salesforce'; // sample text to append at the end of email body.
Messaging.SingleEmailMessage rmail = Messaging.renderStoredEmailTemplate(templateId, null, null);
Messaging.SingleEmailMessage amail = new Messaging.SingleEmailMessage();
amail.saveAsActivity = false;
amail.setPlainTextBody(rmail.getPlainTextBody() + emailfooter); // I want to assign render content to the email. If I comment this line. custom header is coming fine. But why custom header is not coming when I use renderedemailtemplate

//Send Email Now
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {amail});


1 Answer 1


it looks like you can't change email body on the fly. Try to use this solution https://opfocus.com/blog/sending-emails-in-salesforce-to-non-contacts-using-apex/

  • 2
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – identigral
    Oct 26, 2021 at 8:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .