I have to replace some contents in HTML body but when I user HTML with letterhead,it does not work and if I use email template without letter head then it works but when i receive an email,it always display in text format even after I have have added HTMl syntax in it. HTML Template Format:

<table border="0" cellpadding="5" width="600" cellspacing="5" height="400">
    <tr valign="top" height="50">
        <td tEditID="c1r1" style=" background-color:#FFFFFF; color:#000000; bEditID:r3st1; bLabel:main; font-size:12pt; font-family:arial;" aEditID="c1r1" locked="0">
    <tr valign="top" height="300">
        <td tEditID="c1r2" style=" background-color:#FFFFFF; color:#000000; bEditID:r3st1; bLabel:main; font-size:12pt; font-family:arial;" aEditID="c1r2" locked="0">
            <![CDATA[<font face="arial"><span style="color: rgb(0, 0, 0); font-family: arial; font-size: 12pt;">Dear&nbsp;</span>{!Receiving_User.Name}<span style="font-size: 12pt;">,</span></font><div style="color: rgb(0, 0, 0); font-family: arial; font-size: 12pt;"><br><div>Please note that {!Opportunity.OwnerFullName}&nbsp;has been deactivated as a Salesforce User hence all of its Open Opportunities has been assigned to you if any.</div><div><br></div><div><div style="background-color: rgb(255, 255, 255);"><font face="arial">Kind regards,</font></div><div style="background-color: rgb(255, 255, 255);"><font face="arial"><br></font></div><div style="background-color: rgb(255, 255, 255);"><font face="arial"> SFDC Support</font></div></div></div><div style="color: rgb(0, 0, 0); font-family: arial; font-size: 12pt; background-color: rgb(255, 255, 255);"><font face="arial"><br></font></div><div style="color: rgb(0, 0, 0); font-family: arial; font-size: 12pt; background-color: rgb(255, 255, 255);"><font face="Verdana, Helvetica, sans-serif" color="#ff0000">Please do not reply to this email, as it is not monitored.</font></div>]]>
    <tr valign="top" height="50">
        <td tEditID="c1r3" style=" background-color:#FFFFFF; color:#000000; bEditID:r3st1; bLabel:main; font-size:12pt; font-family:arial;" aEditID="c1r3" locked="0">

Below is the apex code part:

    templateId=[Select Id,DeveloperName from EmailTemplate where DeveloperName=:'New_Owner_Opportunities'].Id;
        EmailTemplate templ=[Select Id,body from EmailTemplate where Id=:templateId];

     for(User u1:u){ 
      for(Id managerId: managerIds) {
        OrgWideEmailAddress owa=[Select Id,Address,DisplayName from OrgWideEmailAddress where Address='support@test.com'];
        message.setSubject('Opportunities transferred to you');
          templ.body=templ.body.replace('{!Opportunity.OwnerFullName}', u1.Name);
          templ.body=templ.body.replace('{!Receiving_User.Name}', u1.Manager.Name);
  Messaging.sendEmail(messages, false);

I want to know if EmailTemplate.body.replace() works for HTML with letterheadtemplates?

If not Can someone suggest me what i am missing in my apex code? In my apex

  • Do you have the Letterhead defined in the Header or in the Body of the email? It sounds as though you have it defined in the Body which is causing your issue. If you want your new body to be in HTML, then you need to provide text that's formatted as HTML to replace it with. See SingleMessagingMethods in the Apex Documentation. – crmprogdev Jan 3 '17 at 13:10
  • @crmprogdev I have updated HTML Template code format. – Dhananjay Patil Jan 3 '17 at 13:24
  • @crmprogdev You meant,for HTMl with letterhead,I have to define its Letterhead in apex class? – Dhananjay Patil Jan 3 '17 at 13:26
  • If the letterhead is in the Body and you're REPLACING the Body with what you define in your class, where else is the letterhead going to come from when you replace it? – crmprogdev Jan 3 '17 at 13:30
  • @crmprogdev Letterhead is not defined in my apex class.Also one more thing,When I receive an email,it always goes to Junk folder.What could be the possible reason behind this? – Dhananjay Patil Jan 3 '17 at 13:42

Here's what I think is happening. If you have an HTMLBody string and you use Replace() on it, that would seem to disturb the escapeHtml4() that's been performed on the HTMLBody string. I'm not entirely certain if you've first need to do something like HTMLBody.unescapeHTML4() before performing the replace() operation, but at a minimum, it would seem that from the results you're observing where everything is being printed as text, that you need to escape it again afterward by using escapeHtml4() after performing the Replace() on it.

I found several documents that may also be of interest and relevant to what you're doing depending on how your templates were created. The first is KB 000230852 Email content body missing when sending email via console where

Instead of location.replace('/email/author/emailauthor.jsp?retURL=...............)

use this: location.replace('/_ui/core/email/author/EmailAuthor?retURL=................)

But that only applies to email sent from the console following Winter 16, which I don't think applies to your situation.

Another one is Tips for Using HTML Email Templates which shows the Logo being created in the Header of the document.

You may also want to look at [Creating a Visualforce Email Template][3] and the pages that follow in the Visualforce Developer Guide.

Finally, there's KB 000193418 How can I change my Email Template Letterhead? may be of interest along with several related links in the document as well.

I don't think you want to recreate the HTML templates from scratch if you're doing this at random for a number of different templates. Instead, you want to troubleshoot this to determine why your changes are causing the emails to be sent as plain text and why your logos are disappearing. I believe this is either because your HTMLBody is being unescaped or else because you need to be performing these operations on HTMLBody, not on Body. Unfortunately, I don't have the time to test this out myself.

| improve this answer | |
  • 1
    I have got the solution.. if you can see in email template query I am only using two fields that is ID and body but I'm not using HtmlValue field which is needed for HTML templates. So I have put HTML value field in my query and replace templ.body by template.HtmlValue.now I am receiving an email in proper HTML format.. – Dhananjay Patil Jan 4 '17 at 17:15
  • but I receive blank HTML email in my outlook.If I open the same email in browser then I can see the content.Do you have any idea why HTML template is not visible in outlook? – Dhananjay Patil Jan 5 '17 at 10:16
  • I would guess that the headers are either incorrect or missing for sending the html as an email body. – crmprogdev Jan 5 '17 at 13:36
  • @ crmprogdev I talked to salesforce.the isssue is not from salesforce side.its an issue from microsoft side or my internal IT teams has blocked the contents that restricts me to view the HTML content.Right now the only way to avoid this issue is to use Text version of Email template.If you can see the link that you are looking into it :) salesforce.stackexchange.com/questions/154879/… i have pasted the full code,I am using setPlainText Body there to avoid the issue – Dhananjay Patil Jan 6 '17 at 18:28

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.