Could someone look into this code, and let me know if they see anything wrong that may be causing a problem? For some reason my email is being successfully send with HTML version, but the text version shows only a blank email. The code is essentially the same for both HTML and Text, also in my template the value I am replacing is correct as well.

EmailTemplate emailTemplate = [select Id, DeveloperName, HtmlValue, Body, BrandTemplateId From EmailTemplate where DeveloperName = 'LTRO_Additional_Documents_Required'];

        //Specify the text content of the email.
        String plainBody = emailTemplate.Body;
        plainBody = plainBody.replace('LTRORequireAdditionalDocumentsCon.apxc', emailBody + 
                                      '<br><br><a href="https://'+SandboxName+'redcross.'+sfdcBaseURL+'force.com/DisasterAssessment/ltrodamageuploadstep?ret=1&cid='+caseid+'">Red Cross Disaster Documentation Upload</a>');


        // Specify the content of the HTML email        
        String htmlBody = emailTemplate.HTMLValue;
        htmlBody = htmlBody.replace('LTRORequireAdditionalDocumentsCon.apxc', emailBody + 
                                    '<br><br><a href="https://'+SandboxName+'redcross.'+sfdcBaseURL+'force.com/DisasterAssessment/ltrodamageuploadstep?ret=1&cid='+caseid+'">Red Cross Disaster Documentation Upload</a>');
        //!LTRO_Document__c.Type__c   NewEmail

        // Send the email you have created.
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

You can specify one of the following combinations in an email:

  1. HTMLBody + Template Id
  2. PlainTextBody + Templated Id
  3. HTMLBody + PlainTextBody
  • Ok, so I did test with removing the setTemlateId for both setPlainTextBody and setHtmlbody, but still having the same issue. That should satisfy point 3, right?
    – Ivo Dimov
    Oct 23 '18 at 17:59
  • This really depends on the mail client you're using, and basically is down to the client's implementation of RFC 821 and its successors. The client may choose to send an HTML-only version without text, in which case the plainTextBody will indeed be blank/null. You'll need to look at the original source to verify if there was a plain text body. Oct 23 '18 at 18:04
  • I tested that by commenting out the HTML portion of the code and only sending the Text version. In that case, the text version was showing as expected. I checked in hotmail, and outlook. Then I enabled both HTML and Text, and in hotmail was showing the HTML version but in outlook was blank again. This is where I am confused, does HTML take preference over the Text version of the email?
    – Ivo Dimov
    Oct 23 '18 at 18:09
  • I have idea of gmail. In gmail there is option Show original. By this I can see plain text also. There may be such type of option in outlook and Hotmail. Oct 23 '18 at 18:15
  • I got the way in outlook for you. In outlook, open the email, click the down arrow next to the reply button, then select View message source. Check out this link: help.campaignmonitor.com/plain-text-versions-of-html-emails Oct 23 '18 at 18:17

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.