+ Start a Discussion
SFDC coderSFDC coder 

blank email rendered when using sethtmlbody method

hi all,

i have a scenario where in i query an html email template and populate its merge fields in my apex class itself.
however,when i use the sethtmlBody() method,it renders an empty email.
So i have to comment the sethtmlbody() and go with setplaintextbody().

What can be the reason for this?
Also how do i use the setHtmlBody() along with setplainTextBody()?

Below is my code
 
public static void sendingEmail(String cid,String cname,String eventId)
    {
        String plainTxtBody='',startDate='',endDate='',scheduledDate='';
        String hbody='';
        Date sdate;

        //get the email template
        EmailTemplate template=[SELECT HtmlValue,Body FROM EmailTemplate WHERE          DeveloperName='Meeting_Reminder'];
        
        Event eve=[select subject,StartDateTime,EndDateTime,Owner.name,Owner.Email,Owner.Phone 
                    From Event where Id=:eventId];
                    
        hbody=template.HtmlValue;
        
        //setting the merge fields
        hbody=hbody.replace('{!Contact.Name}',cname);
        hbody=hbody.replace('{!Event.Subject}',eve.Subject);
         
        plainTxtBody=template.Body;
        plainTxtBody=plainTxtBody.replace('{!Contact.Name}',cname);
        plainTxtBody=plainTxtBody.replace('{!Event.Subject}',eve.Subject);
        
        //Construct the email message
        Messaging.SingleEmailMessage mail=new Messaging.SingleEmailMessage();
        mail.setTargetObjectId(cid);
        mail.setSaveAsActivity(false);
        mail.setSubject('Meeting Reminder');
        //mail.setHtmlBody(hbody);
        mail.setPlainTextBody(plainTxtBody);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});     
    }
Also can anyone explain why do we have to set the plaintextbody() even when the email template is of type html?

Any help is appreciated...

Thanks,
 
Best Answer chosen by SFDC coder
SFDC coderSFDC coder
hi sonam,

i solved this issue by changing the email template type from HTML with Letterhead to custom HTML template and now its working fine.
However i still dont understand as to why its not working with the letterhead type.Do you have any clue?
 

All Answers

SonamSonam (Salesforce Developers) 
I have used the same code(triggered via trigger on Contact object) in my test ORG and am able to get the email with content with both :

public class Emailsend
{

public static void sendingEmail(String cid,String cname,String eventId)
    {
        String plainTxtBody='',startDate='',endDate='',scheduledDate='';
        String hbody='';
        Date sdate;

        //get the email template
        EmailTemplate template=[SELECT HtmlValue,Body FROM EmailTemplate WHERE Name='Meeting_Reminder'];
        
        Event eve=[select subject,StartDateTime,EndDateTime,Owner.name,Owner.Email,Owner.Phone 
                    From Event where Id=:eventId];
                    
        hbody=template.HtmlValue;
        
      
        
        //setting the merge fields
        hbody=hbody.replace('{!Contact.firstName}',cname);
        hbody=hbody.replace('{!Event.Subject}',eve.Subject);
         
        plainTxtBody=template.Body;
        plainTxtBody=plainTxtBody.replace(Con.firstName,cname);
        plainTxtBody=plainTxtBody.replace(Event.Subject,eve.Subject);
        
        //Construct the email message
        Messaging.SingleEmailMessage mail=new Messaging.SingleEmailMessage();
        mail.setTargetObjectId(cid);
        mail.setSaveAsActivity(false);
        mail.setSubject('Meeting Reminder');
        mail.setHtmlBody(hbody);
        mail.setPlainTextBody(plainTxtBody);
        //mail.setTemplateId(template.id);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});     
    }
    }

Please try to deliver the email to any other email client(say gmail/yahoo) might be that the HTML content is being blocked at the email server level and not appearing in the inbox..
 
SFDC coderSFDC coder
hi sonam,

i tried the same code.still getting a blank body.However when i receive an email,a small email notification pops up in the bottom right corner of the window whre the contents are visible but when i click and open it up everything disapperas.

Also when i comment out the setHtmlBody() method,the plain text is delivered sucessfully.
What can be the problem?
i have created an email template of type HTML letterhead where email layout is of type free form layout.
Can you please help??
its a long time now since i am stuck with this issue.  :( Its little urgent.. 

Thanks
 
SFDC coderSFDC coder
hi sonam,

i solved this issue by changing the email template type from HTML with Letterhead to custom HTML template and now its working fine.
However i still dont understand as to why its not working with the letterhead type.Do you have any clue?
 
This was selected as the best answer
Jerrod Knapp 11Jerrod Knapp 11
I am having the same issue when Apex generates an HTML letterhead email.  The email will come in as blank.  I cannot use HTML w/o letterhead because we use letterhead.  Any help on this would be awesome!!  URGENT!  Thanks to everyone in advance.
abhik dey 1abhik dey 1
Hi Everyone, Just wanted to check if there is a solution to this. ?

I have to use template with letter head and if I use sethtmlbody, it blanks out the entire email and customers receiving empty emails.

Thanks
David Roberts 4David Roberts 4
Could you not include the letter head design (as html) in your template?
Mahesh AdiMahesh Adi
Did anyone solved this? I got ended with same issue.
Mahesh AdiMahesh Adi
It worked for me when I remove below lines, no need to set them but make sure set WhatId()
mail.setSubject('Meeting Reminder');
mail.setHtmlBody(hbody);
mail.setPlainTextBody(plainTxtBody);

 
Dewald Steenkamp 4Dewald Steenkamp 4
The Messaging.sendEmail returns a result of List<Messaging.SendEmailResult>, that can tell you why an email was not sent.  In my case, it was having an issue because I was using the .setWhatId method and did not need to.
 
Messaging.SingleEmailMessage mess = new Messaging.SingleEmailMessage();
        mess.setTemplateId(et.Id);
        mess.setTargetObjectId(cbsQuoteRef.Contact__r.Id);
        mess.setToAddresses(new String[]{cbsQuoteRef.Contact__r.Email});
        mess.setFileAttachments(new Messaging.EmailFileAttachment[]{mailAtt});
        List<Messaging.SendEmailResult> result = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mess},false);
        System.debug('emailresult:' + result);

Hope this helps someone out there, as it was driving me insane.
Hai HaHai Ha
Figured out the issue. The html contains <![CDATA[ .... ]]>
Need to remove the <![CDATA[ and ]]> from the html.
Vijay LaxmiVijay Laxmi
Hi @Hai Ha 
Could you please tell me how you have removed this from apex class while sending email
Prashant Kshirsagar 10Prashant Kshirsagar 10
Hi,

This worked for me when I removed the CDATA from html as suggested by @Hai Ha. Below is the link to remove CDATA
https://stackoverflow.com/questions/25874571/how-to-remove-cdata-and-from-a-string-using-apex-in-salesforce
Ankit Agrawal 28Ankit Agrawal 28

I also faced the same issue, Body was not getting rendered only in Outlook when Replacing any content, but thanks to @Hai Ha, and @Prashant, removing these tags works for me. But, Letterhead is not coming, whether we are openng in web mail or in outlook.

Can someone help on it?