function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
l.trincil.trinci 

How to display image from document folder in a email?

I have no problem on displaing an image on a visualforce page from a controller, but when i try to do the same thing on a email (A mass email in my case) it seems don't work (the document is externally available)

 

Here is my code:

 

public string getDocumentLogoUrl()

{

List<Document> lstDocument = [Select Id,Name,LastModifiedById from Document where Name = 'Header' limit 1];
string strOrgId = UserInfo.getOrganizationId();
string strDocUrl = 'https://'+ApexPages.currentPage().getHeaders().get('Host')+ '/servlet/servlet.ImageServer?id='+lstDocument[0].Id+'&oid=' + strOrgId;

return strDocUrl;

}

 

 

 

public static void sendMail(string email, string first_name, string last_name) {


Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {email};
mail.setToAddresses(toAddresses);

mail.setSubject('xxx');

mail.setHtmlBody('<img url="{!DocumentLogoUrl}" styleClass="photo"></apex:img><br/><br/>Salve '+First_Name+' '+last_name+');



Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}

(Edited after Doomsday reply)

 

Like always, thanks in advance

 

Best Answer chosen by Admin (Salesforce Developers) 
DoomsdayDoomsday

The last my code works. I tested it on my org with sending email. But I do not understand why it does not work for you

 

public static string getDocumentLogoUrl()
{
List<Document> lstDocument = [Select Id,Name,LastModifiedById from Document where Name = 'Header' limit 1];
string strOrgId = UserInfo.getOrganizationId();
string orgInst = URL.getSalesforceBaseUrl().getHost();
orgInst = orgInst.substring(0, orgInst.indexOf('.')) + '.content.force.com'; //orgInst = 'na14.content.force.com' in my org
string strDocUrl = URL.getSalesforceBaseUrl().getProtocol() + '://c.' + orgInst + '/servlet/servlet.ImageServer?id=' + lstDocument[0].Id + '&oid=' + strOrgId;
return strDocUrl;
}

 

Maybe in your org URL.getSalesforceBaseUrl().getHost() returns 'c.na14.salesforce.com'...

 

apex:image doesn't work because used only for visualforce email templates.

All Answers

DoomsdayDoomsday

Hi.

Maybe problem in that:
"<img url="{!DocumentLogoUrl}" styleClass="photo"></apex:image> " 

l.trincil.trinci

Nop, it was only a mistake on writing it here.

 

 

 

DoomsdayDoomsday

Try this:
   mail.setHtmlBody('<apex:image value="{!DocumentLogoUrl}" styleClass="photo"></apex:image><br/><br/>Salve '+First_Name+' '+last_name+');

should work.

l.trincil.trinci

Doesn't work :smileysad:

aballardaballard

What is the strDocUrl generated by your expression?

DoomsdayDoomsday

Try this:

 

string strDocUrl = URL.getSalesforceBaseUrl().getProtocol() + '://c.' + URL.getSalesforceBaseUrl().getHost() + '/servlet/servlet.ImageServer?id=' + lstDocument[0].Id + '&oid=' + strOrgId;
l.trincil.trinci

it seems it doesn't work

DoomsdayDoomsday

public string getDocumentLogoUrl()

{

List<Document> lstDocument = [Select Id,Name,LastModifiedById from Document where Name = 'Header' limit 1];
string strOrgId = UserInfo.getOrganizationId();
return  '/servlet/servlet.ImageServer?id='+lstDocument[0].Id+'&oid=' + strOrgId;

}


sendMail:
mail.setHtmlBody('<apex:image value="{!DocumentLogoUrl}" styleClass="photo" /><br/><br/>Salve '+First_Name+' '+last_name+');  

DoomsdayDoomsday

If and this doesn't work, try:
 

public static string getDocumentLogoUrl()

{

List<Document> lstDocument = [Select Id,Name,LastModifiedById from Document where Name = 'Header' limit 1];
string strOrgId = UserInfo.getOrganizationId();
return  '/servlet/servlet.ImageServer?id='+lstDocument[0].Id+'&oid=' + strOrgId;

}


sendMail:
mail.setHtmlBody('<apex:image value="' + getDocumentLogoUrl() + '" styleClass="photo" /><br/><br/>Salve '+First_Name+' '+last_name+');  

l.trincil.trinci

No one of this work, it seems that i can only invoke an url by the full url, like: 

<img src="https://c.na14.content.force.com/servlet/servlet.ImageServer?id=015d0000000ghFM&oid=00Dd0000000coIy&lastMod=1336986000000"

 

This work in both visualforce page and in the email, but if i try to invoke the class in the html body text doesn't appear on the email, but in a visualpage yes.

It's really weird..

 

Thanks anyway for answers

DoomsdayDoomsday

This works with emails, but I'm not sure this is correct though use of the document

 

public static string getDocumentLogoUrl()
{
	List<Document> lstDocument = [Select Id,Name,LastModifiedById from Document where Name = 'Header' limit 1];
	string strOrgId = UserInfo.getOrganizationId();
	string orgInst = URL.getSalesforceBaseUrl().getHost();
	orgInst = orgInst.substring(0, orgInst.indexOf('.')) + '.content.force.com';
	return URL.getSalesforceBaseUrl().getProtocol() + '://c.' + orgInst + '/servlet/servlet.ImageServer?id=' + lstDocument[0].Id + '&oid=' + strOrgId;
}
mail.setHtmlBody('<img src="' + getDocumentLogoUrl() + '" />');

 

l.trincil.trinci

Try it this too, i really don't understand why all this try don't work:

 

mail.setHtmlBody

1)  <apex:image value="' + getDocumentLogoUrl() + '" styleClass="photo" />
2) <apex:image value="{!DocumentLogoUrl}" styleClass="photo"></apex:image>

3) <img src="'+getDocumentLogoUrl()+'"/>

 

The document is external avaible, but with the invoke it don't appear, on the last case on the email now appear a broken image while with: <apex:image value="{!DocumentLogoUrl}" styleClass="photo"></apex:image> appear only an empty space then i think that image src is the only way to make it, but i don't understand how to put it..

 

 

Thanks anyway for all your help Doomsday!

DoomsdayDoomsday

The last my code works. I tested it on my org with sending email. But I do not understand why it does not work for you

 

public static string getDocumentLogoUrl()
{
List<Document> lstDocument = [Select Id,Name,LastModifiedById from Document where Name = 'Header' limit 1];
string strOrgId = UserInfo.getOrganizationId();
string orgInst = URL.getSalesforceBaseUrl().getHost();
orgInst = orgInst.substring(0, orgInst.indexOf('.')) + '.content.force.com'; //orgInst = 'na14.content.force.com' in my org
string strDocUrl = URL.getSalesforceBaseUrl().getProtocol() + '://c.' + orgInst + '/servlet/servlet.ImageServer?id=' + lstDocument[0].Id + '&oid=' + strOrgId;
return strDocUrl;
}

 

Maybe in your org URL.getSalesforceBaseUrl().getHost() returns 'c.na14.salesforce.com'...

 

apex:image doesn't work because used only for visualforce email templates.

This was selected as the best answer
l.trincil.trinci

OOOH now it reaaally really work ^-------------^

 

The problem was here:

 

public static string getDocumentLogoUrl()
{
List<Document> lstDocument = [Select Id,Name from Document where Name = 'Header' limit 1];
string strOrgId = UserInfo.getOrganizationId();
string orgInst = URL.getSalesforceBaseUrl().getHost();
orgInst = orgInst.substring(0, orgInst.indexOf('.')) +  '.content.force.com'; //orgInst = 'na14.content.force.com' in my org
string strDocUrl = URL.getSalesforceBaseUrl().getProtocol() + '://c.' + orgInst + '/servlet/servlet.ImageServer?id=' + lstDocument[0].Id + '&oid=' + strOrgId;
return strDocUrl;
}

 

Don't know why..

I modify it with 'na14.content.force.com';

And now it work perfectly!!

 

Thank you very much Doomsday, u save my day and my work  ;)

DoomsdayDoomsday

Glad to help :)