You need to sign in to do that
Don't have an account?
SFDC 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
Any help is appreciated...
Thanks,
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,
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
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..
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
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?
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
Hope this helps someone out there, as it was driving me insane.
Need to remove the <![CDATA[ and ]]> from the html.
Could you please tell me how you have removed this from apex class while sending email
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
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?
message.setHtmlBody();
message.setSubject();
These are explicitly set by the template and if you set them in the apex code you will override them. Even if you only set the subject in apex it will also negate the use of the template for the htmlBody. So, don't set either of these for a plain text templated emails or it will not use the template.