4

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>
<body>
    <center>
        <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>
            PRODUCT_ID
            <br />
            <font style="color:#ffffff; "><b>Size: </b>
            CONDITIONAL_TEXT_1
            <br />
            <font style="color:#ffffff; "><b>Expiration Date: </b>
            DATE
            <font style="color:#989b9f ;font-size:11px;line-height: 16px;">
            EMAIL_FOOTER
            <br />
        </div>
    </center>
</body>
</html>

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:

mail.setHTMLBody(emailBody);

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.

3
  • 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, 2015 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. Dec 30, 2015 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, 2015 at 10:38

2 Answers 2

1

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 ! :)

0

This worked for me when using VisualForce Page Email Template (as of Winter '21). I had the issue that the email body is the plain text version even though VF Page templates doesn't have plain text version.

EmailTemplate emailTemplate = [SELECT Id, Markup FROM EmailTemplate WHERE DeveloperName = :System.Label.TEMPLATEID LIMIT 1];
String emailBody = emailTemplate.Markup;
emailBody.replace('PRODUCT_ID',ObjectId);

The Markup field has the entire email body that still keeps the formatting.

The catch is that merge fields won't work. So you will have to replace each of the dynamic values in your template without relying to merge fields.

You must log in to answer this question.

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