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">
<![CDATA[]]>
</td>
</tr>
<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 </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} 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>]]>
</td>
</tr>
<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">
<![CDATA[]]>
</td>
</tr>
</table>
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');
message.setTargetObjectId(managerId);
message.setOrgWideEmailAddressId(owa.Id);
message.setSaveAsActivity(false);
message.setTemplateId(templateId);
templ.body=templ.body.replace('{!Opportunity.OwnerFullName}', u1.Name);
templ.body=templ.body.replace('{!Receiving_User.Name}', u1.Manager.Name);
}
message.setHtmlBody(templ.body);
messages.add(message);
}
}
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
Best Answer
Here's what I think is happening. If you have an
HTMLBody
string and you useReplace()
on it, that would seem to disturb theescapeHtml4()
that's been performed on theHTMLBody
string. I'm not entirely certain if you've first need to do something likeHTMLBody.unescapeHTML4()
before performing thereplace()
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 usingescapeHtml4()
after performing theReplace()
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
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 onBody
. Unfortunately, I don't have the time to test this out myself.