I want to send an email in my logic. I have created an Email Template.

Here is what I want to achieve:

Email Template:

<html width="600px">
<style>body{font-family:"Helvetica Neue",Helvetica,Arial,sans-serif;color: #31343c;font-size:14px;line-height:21px;}a{text-decoration:none;color:#03a2d6;}</style>
        <div style="width:600px">
            <div style="height:80px;background-color:#17191d;text-align:left;"><img src="TEST" style="margin-top:15px !important;" /></div>
            <div style="padding-top:15px"></div>
            <font style="color:#ffffff; ">
            <b>Product: </b>
            <br />
            <font style="color:#ffffff; "><b>Size: </b>
            <br />
            <font style="color:#ffffff; "><b>Expiration Date: </b>
            <font style="color:#989b9f ;font-size:11px;line-height: 16px;">
            <br />

Now, I am querying the Email Template Body in my Code, and replacing the necessary labels with the values that I want to put.

The method being used is:

public static String createEmailBody(List<Object__c> Object){
    EmailTemplate tempID = [SELECT Id,Name,Body FROM EmailTemplate WHERE Id =: System.Label.TEMPLATEID];

    String emailBody = tempID.Body;
    String ObjectId= String.valueOf(Object[0].Product_Id__c);
    Integer ObjectSize= Integer.valueOf(Object[0].Size_GB__c);
    String expD = EC_Constants.BLANK;

    emailBody = emailBody.replace('PRODUCT_ID',ObjectId);

    if(Object[0].Size_GB__c != null) {
        emailBody = emailBody.replace('CONDITIONAL_TEXT_1',String.valueOf(ObjectSize));

    if(Object[0].Time_Expires__c != null) {
        expD = (Object[0].Time_Expires__c.year() == 2038)?EC_Constants.PERP:Object[0].Time_Expires__c.Date().format();

    emailBody = emailBody.replace('expD',expD);
    emailBody = emailBody.replace('EMAIL_FOOTER',System.Label.EMAIL_FOOTER);

    return emailBody;

This method sets the body of the email. I am sending the mail in another method, where I call the above method and get the body.

I am using Messaging.SendEmail method. When I set the body of the mail, I am using:


However, when I send the mail, all the HTML Tags are getting stripped, and the Plain Text version is being displayed. I can see no formatting.

WHere am I going wrong? Please help.

  • It looks fine. But I suspect at Integer.valueOf(String). Try to add debug and check what are you getting after replacing things. And if it is urgent, you can assemble body directly in apex, without going to EmailTemplate. – Ysr Shk Dec 30 '15 at 9:47
  • @YsrShk : In my company, we have been asked to remove all the Hard Coding of the email bodies, and create email templates. Hence, I have removed the already assembled body and put it in a template. :P Debug Values are fine. It's just that I don't see the formatting. – Adhish Thite Dec 30 '15 at 9:57
  • Can you share Email Sending code? Because if you are getting intended body in debug, then there must be problem in calling code only. Either me or any other can help you out, after more details. – Ysr Shk Dec 30 '15 at 10:38

Sorry for the late response but I have finally got the answer for this. I was fetching ONLY THE BODY from the template, and not the HTML Body.

The appropriate query should have been this:

EmailTemplate tempID = [SELECT Id,HTMLValue,Name FROM EmailTemplate WHERE Id =: System.Label.TEMPLATEID];
String emailBody = tempID.HTMLValue;

This worked wonders.

Thanks a lot ! :)

| improve this answer | |

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.