[SalesForce] How to Dynamically Embed Images from Files into Email Templates

I have a custom object with Files. The Files hold images. I am creating an email template and want the Files to embed in the email template. How do I even begin to go about that? I can't find anything online.

End State:
Form submitted (through form assembly) into the custom object.
Upon insert, a process builder is fired which triggers an email alert. The email alert grabs whatever and however many images are in the Files object for the record and embeds them be low the text of the email.

Best Answer

You can't really do what you'd want with typical email templates as they can only really display static images that you paste in or references to files that already exist. Since, in this case, the records/files will be created moments before (or be the trigger), it'll need to be able to dynamically pull them when the alert goes out. I can see one of two options:

  1. Using PB and email alert and Visualforce Email template Basically, the plan you had going on where the VF email template will handle the hard part which is getting the image from a file on the record.

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_templates_creating.htm

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_templates_with_apex.htm

The second link will be important. You'd essentially create a custom apex class that would pull the Attachment/file and then create a URL to access that file.

You'd have a custom component that just serves to display the image (if it needs to be displayed) using and passing in the url from the apex you built.

Then, you'd have a Visualforce email template that would include this component at whatever location you'd like around all the other text. You can access merge fields from the record and do normal things here as well (first document link).

  1. Complete Apex Solution

You could look to do this all in apex upon insert with separate methods for building the url to display in html, building the email body, and sending the email. However, whenever you get the chance to use the standard options (PB, email alerts, template), I'd say it's a better option for maintainability than going for completely custom. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm

Related Topic