[SalesForce] Issue Sending Image in Rich Text Field in Email through apex

I have a Visual force page to display certain fields in a certain format. As part of this the page also has a Rich Text Field. There are two buttons on the page, one to send the page as email and the other to download the page as PDF.

When a image is uploaded into the rich text field below is the behavior.

  • The PDF looks good with the image in it and i was able to download without any issues.

  • Everything looks good on the email except there is no image on the email from the RTF. It displays a little broken place holder icon rather than a image

I see some similar explanation found in a SFSE thread that is little close to what i need.
How do I insert an image from a Rich Text field in an html email template

Is there a workaround for the issue or am I missing something because i was able to download the PDF with the image without any issues ?

Below is relevant code:

VFCODE:

<apex:pageBlock rendered="{!displaydetails}">
    <div style='font-size:10pt;font-weight:bold;'>Meeting Details:</div>
    <hr width="25%" align="left"></hr>
<!-- details is the apex string for the RTF used here after nullcheck on server -->
<apex:outputText style="font-size:10pt;" value="{!details}" escape="false"/>
<br/>
<br/>
</apex:pageBlock>

APEX METHODS:

// This method is called to send an email
public PageReference sendEmail() {
    system.debug('SEND EMAIL PRESSED');
    PageReference pdf = Page.SLA_HTMLCallReport;
    pdf.getParameters().put('id',(String)model.id); 
    pdf.setRedirect(true);
    Blob b = Blob.valueOf('');
    if(!Test.isRunningTest()){
        b = pdf.getContent();
    }
    String[] toAddresses = new List<String>();
    for (String item : recipients.split(';') ) {
        toAddresses.add(item);
    }
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
    email.setSubject( subject );
    email.setToAddresses( toAddresses );
    email.setUseSignature(true);
    email.setHtmlBody( body + '<br/><br/>' + b.toString() );
    Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email}); 
    emailSent = true;      
    return null;
}
//This method is called for saving as pdf
public PageReference savePDF() {
    PageReference pdf = Page.SLA_PDFCallReport;
    pdf.getParameters().put('id',(String)model.id); 
    return pdf;
}

Best Answer

I'm fairly sure that the problem here is that when you try to access the image on the email you are using the Salesforce internal link (the one that requires authentication).

When rendering PDFs you don't get this issue because the images are embedded into the file, so no authentication is required to view them.

Similar issue faced here, with rendering MS Word documents.


You could also:

  1. Make the Visualforce publicly available, and give the guest user access to the data displayed (careful with this, you might want to provide some kind of authentication for your users to see the data).
  2. Host the images in another service, and refer to their links in your field. Since the Rich Text Area field is just plain HTML, you can modify the image link to point to, say, Imgur or a AWS S3 bucket instance.
Related Topic