You need to sign in to do that
Don't have an account?
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
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
Hi.
Maybe problem in that:
"<img url="{!DocumentLogoUrl}" styleClass="photo"></apex:image> "
Nop, it was only a mistake on writing it here.
Try this:
mail.setHtmlBody('<apex:image value="{!DocumentLogoUrl}" styleClass="photo"></apex:image><br/><br/>Salve '+First_Name+' '+last_name+');
should work.
Doesn't work :smileysad:
What is the strDocUrl generated by your expression?
Try this:
it seems it doesn't work
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+');
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+');
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
This works with emails, but I'm not sure this is correct though use of the document
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!
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.
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 ;)
Glad to help :)