Output of VisualForce Component inside VisualForce Email Template Gets HTML Encoded

I have a Visual Force component inside a Visual Force Email Template with the intention of producing an HTML email in which the component is reponsible for rendering some of the HTML. What seems to be happening is that the HTML string the component produces is being HTML encoded when it gets merged into the HTML email. In gmail, the message is indeed HTML but instead of seeing a nicely rendered table, I see a bunch of markup.

Is this possible? or should I use a different type of template?

The email template:

<messaging:emailTemplate subject="License Request" recipientType="User" relatedToType="License__c">
<messaging:htmlEmailBody >
<html>
<head>
<style type="text/css">
*{margin:0;padding:0;}
body{font-family:Verdana, Tahoma, Sans-Serif;font-size:10pt;margin:10px;}
.section{margin-top:10px;background-color:#ff6a07;color:#FFF;font-size:1.5em;border-left:1px solid grey;border-top:1px solid grey;border-right:1px solid grey;padding:6px;}
.propertyBlock{width:100%;border:1px solid grey;}
.odd{background-color:#f8f8f8;}
.even{background-color:#eaeaea;}
.name,.value{width:50%;padding:2px;}
</style>
</head>
<body>
<c:LicensePropertiesComponent licenseId="{!relatedTo.ID}" />
</body>
</html>
</messaging:htmlEmailBody>
</messaging:emailTemplate>

The component

<apex:component access="global" controller="LicensePropertiesComponentController">
<apex:attribute name="licenseId" description="Object ID of License__c instance whos properties will be rendered." type="ID" required="required" assignTo="{!id}" />
<apex:outputText value="{!MessageContent}" escape="false" />
</apex:component>

Note that where I have the “outputText” line above, I’ve separately tried both of these and in all cases the html string produced by getMessageContent seems to get encoded when merged into the page:

{!MessageContent}
<apex:outputText value="{!MessageContent}" />

The controller:

public String getMessageContent() {
    try {
        if (m_licenseId == null) {
            return 'Can\'t render as the LicenseId has not been set!';
        }
        String message = renderBody(m_licenseId);
        return message;
    } catch (Exception ex) {
        return 'An exception occurred rendering the license request as an Html email. The exception is: ' + ex;
    }        
}

In the renderBody function is a bunch of string handling to produce HTML. I used the w3c validator to make sure its well formed.

Any ideas?

I’ve tried roughly the same in a trigger using Messaging.SingleEmailMessage and whilst that works satisfactorily, I’ve run into the 10 email message limit in my development organisation and it doesn’t look like Salesforce are going to increase it.

The only option left seems to be to send a message to a .NET web service which will form the email and send it on behalf of Salesforce.

Considering there are 4 different types of template and Messaging.SingleEmailMessage in Salesforce, its frustrating that a simple requirement be so difficult to implement.

Answer

I hope this should work:

<apex:outputText value="{!MessageContent}" escape="false"/>

Have you tried this?

Attribution
Source : Link , Question Author : IanT8 , Answer Author : Patlatus

Leave a Comment