1

I have a running VisualForce Email Template for 'Case', and wish to include the Case.Owner profile picture.

I am reading (here) that one can use either FullPhotoUrl or SmallPhotoUrl, but this doesn't seem to be working for me - <apex:image value="{$User.smallphotourl}"/>.

Can you please assist?

Here is my current version of the VF Email Template:

<messaging:emailTemplate subject="[Internal] Case #{!relatedTo.CaseNumber}: {!relatedTo.Subject}" recipientType="User" relatedToType="Case">
    <messaging:htmlEmailBody >
        <html>
            <style type="text/css">
                body {font-family: arial; size: 12pt;}
            </style>
            <body>
                <br />• Case Owner: {!relatedTo.Owner.Name} <apex:image value="{$User.smallphotourl}"/>
            </body>
        </html>
    </messaging:htmlEmailBody>   
</messaging:emailTemplate>
2

The PhotoUrl is not available with $User global variable. You need to query it in order to use with your template. So, you would need a controller along with the Visualforce template.

Since, the photo is internal to Salesforce, you may not be able to access the image as URL from external source directly. You would need to convert you image to base64 code and use it in your template.

Custom Component (UserImg):

<apex:component controller="TemplateController" access="global">
    <img src="{!currentUserImgURI}"/>
</apex:component>

Template:

<messaging:emailTemplate subject="[Internal] Case #{!relatedTo.CaseNumber}: {!relatedTo.Subject}" recipientType="User" relatedToType="Case">
    <messaging:htmlEmailBody >
        <html>
            <style type="text/css">
                body {font-family: arial; size: 12pt;}
            </style>
            <body>
                <br />• Case Owner: {!relatedTo.Owner.Name} <c:UserImg/>
            </body>
        </html>
    </messaging:htmlEmailBody>   
</messaging:emailTemplate>

Controller:

public class TemplateController {
    public String currentUserImgURI {public get; private set;}

    public TemplateController() {
        User currentUser = [SELECT SmallPhotoUrl FROM User WHERE Id =: UserInfo.getUserId() LIMIT 1];
        currentUserImgURI = getDataUriFromUri(currentUser.SmallPhotoUrl);
    }

    public static String getDataUriFromUri(String url) {
        HttpRequest req = new HttpRequest();
        req.setEndpoint(url);
        req.setMethod('GET');
        Http binding = new Http();
        HttpResponse res = binding.send(req);
        Blob image = res.getBodyAsBlob();
        return 'data:'+res.getHeader('Content-Type')+';base64,'+EncodingUtil.base64Encode(image);
    }
}
5
  • Thank you. I added the Controller and altered the template as suggest, but am receiving the following error when trying to save the VF Email Template: Error: Unknown property 'core.email.template.EmailTemplateComponentController.currentUserImgURI'
    – iDog
    Nov 13 '16 at 15:47
  • updated the answer...the get method for curren‌​tUserImgURI should be public...
    – ipradhansk
    Nov 13 '16 at 17:45
  • thanks. I now receive 'Unsupported attribute controller in <messaging:emailTemplate>'. Shouldn't the Controller be a standalone component?
    – iDog
    Nov 13 '16 at 18:42
  • modified answer again...you need to use VF Component to use controller in a VF Email Template...
    – ipradhansk
    Nov 13 '16 at 20:04
  • Thanks @SanTosh. Can you perhaps assist with a test class for 'TemplateController' ?
    – iDog
    Nov 14 '16 at 8:11

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.