6

I know this issue is out there and I went through most of the "image not being displayed in email" questions, but nothing is helping me so far.

I am sending some images in my email.

In my controller I am generating the image url like this:

URL.getSalesforceBaseUrl().toExternalForm()+'/servlet/servlet.FileDownload?file='+imageId

These images are associated to the object record using Attachments.

The URL comes in properly in the email but the image is not shown. When I copied the url from the email and put it into another tab, it went to Salesforce login screen instead of showing the image.

I even tried to change the url to this:

URL.getSalesforceBaseUrl().toExternalForm()+'/servlet/servlet.ImageServer?id='+imageId+'&oid='+UserInfo.getOrganizationId()

But it still does not show my image. When I copy that url in a new tab and hit enter it gives me following error:

Unable to Access Page The value of the "id" parameter contains a character that is not allowed or the value exceeds the maximum allowed length. Remove the character from the parameter value or reduce the value length and resubmit.

So I removed the last 3 characters of both ids to make them of length 15 but it still did not worked.

Am I doing something wrong. Any help and suggestions would be appreciated.

8
  • 1
    When using servlet.FileDownload to retrieve files that are added to a record as Notes & Attachments, you have to make sure that the user making the request has at least read permissions on the record that the file is attached to. This is why you got the Salesforce login screen using this method. Question: What are your requirements around relating the image files to various records? Are other options available to you, eg. Static Resources, or even externally hosted images, then adding URL links as a related list? Dec 4, 2013 at 15:14
  • Well no, I only have the option to access the images from Notes and Attachments. Is there no way to make these images public in an email? Dec 4, 2013 at 16:40
  • The email is also sent to a non salesforce user so I dont know how the read permission takes into effect in this case. Dec 4, 2013 at 16:45
  • 1
    That's the main problem: Because the email is sent to non-salesforce users, they will not have read permissions, and so they will not be able to download the images from Notes & Attachments. I don't see any way around that, unfortunately. Dec 4, 2013 at 20:06
  • Ok, so we need to 'paste' image in the email so that it becomes part of the email body & not just a link. Dec 5, 2013 at 5:01

4 Answers 4

8

Attachments are not public hence anybody receiving the email would need a login for your org to be able to see the images you're trying to send. One alternative would be to host the images elsewhere which will likely be faster, or you could upload the images as public documents in Salesforce as you can generate a public URL for documents so that they can be viewed externally.

  1. Upload and store the image file in salesforce Documents
  2. Be sure to mark the document/image as "Externally Available Image"
  3. Once the image file has been stored, follow the syntax below to reference the image in your HTML:

https://{SALESFORCE-INSTANCE}.salesforce.com/servlet/servlet.ImageServer?oid={ORG-ID}&id={DOC-ID}

{SALESFORCE-INSTANCE} = Salesforce.com instance (ex. NA1, NA2, NA3, etc.). This can be found by looking at the address field in your browser window. {ORG-ID} = Organization ID. Can be copied from Setup | Administration Setup | Company Profile | Company Information {DOC-ID} = Document ID. This is the 15-digit character string at the end of the URL when viewing the document detail in salesforce.

2
  • 1
    I did explored the documents solution but the client is insisting on using notes and attachment for images. I read the body from attachment and convert it to base64 and attach it in the img source. it works in this js fiddle: jsfiddle.net/HnYvG but it does not work when the actual email is sent. Upon researching I found out that gmail does not support the base64 format for images: stackoverflow.com/questions/16242489/… Dec 13, 2013 at 17:57
  • I tried with the File object. I inserted the public link in the img tag. It was still not working. Dec 30, 2019 at 12:10
1

Image should be added in Sales-force Documents that URL should be added in your Email Template. Steps:- Go to Documents tab click on New Button Enter Document Name Check check box Externally Available Image Choose file Click on save Copy the image URL .

Image should be visible on your Mobile

0

This seems to continue working, even in 2022. The key is to use Documents in this way, not Files, which behave quite differently.

Note that you cannot access Documents from Lightning - switch to Classic first.

0

I wanted to know how does this work while shifting the template to other Org? Do we need to go in there and change Org id and doc ID manually?

New contributor
Snehal is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • 1
    Welcome to Salesforce Stack Exchange (SFSE). Please see the notes about "commentary..." responses and "asking another, different question" in Why and how are some answers deleted?, and be aware that your "answer" will most likely be deleted. Please contribute to SFSE in other ways so that you gain reputation - you only need 15 rep to upvote a question and only 50 rep to comment under someone else's question. Or you could possibly ask a new question.
    – Moonpie
    Aug 3 at 12:01

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.