How to make Email Template with html template use an image from a FileDescriptor

Hi

Just tested Email Template add-on and it looks great. I saw that samples uses html template, and that images are entirely encoded in the html template itself.

Would it be possible to use a FileDescriptor in some way for the image source ? This is what we do in all our reports and it works great, eventhough we use XLS.

Thinking it was due to a limitation of html template by itself, I got back to corresponding Reports documentation.

I saw FileDescriptor was mentionned (excerpt below), but I must admit I do not understand the sentence in bold:

“An image is added as a byte array within the src attribute. This approach allows you to use variables for the FileDescriptor attributes of the entities. The byte array can even be added directly to the template, even though this approach is not recommended”

Best Regards
Michael

Hi Michael,

There are two approaches to add a file descriptor to the email template. In a first approach, you can add an image file as an attachment in UI or via API and then in your template use following image link <img src="cid:your image file name">. Or you can use insert image as template parameter as described in report documentation, please check https://doc.cuba-platform.com/reporting-6.10/formatters.html and https://doc.cuba-platform.com/reporting-6.10/template_html.html.

Ok thanks Evgeny. But how can I do it in the email template Designer ?

image

Hi @evgeny.zaharchenko

On the sample email template “subscription expired”, which is design based (not report based), I tried your first approach, uploading a png file in file attachments.

image

It seems the email template UI does not allow to edit image source, when you click on the image in the designer, you have nothing on the right panels to do so.

So I tried to edit the html code directly using this button

image

Setting cid:your image file name as you recommended, but there is no way to save modification.

image

Any idea ?

Hi @michael.renaud

You can update image src by double click on the image
image

Great @evgeny.zaharchenko , but then the image is included as a byte array in the src attribute, whereas my goal is to have it point to a FileDescriptor, back to square one.

For instance the company logo is used widely across documents & reports, so my goal is to not duplicate the image data but just having one file descriptor that can be changed easily.

image

Hi @michael.renaud,

It is not a problem to address a picture using <img src=...> using html template as an email body. The problem is that FileDescriptor is covered by the security policies. You can try to define your own REST controller to provide direct access to your images with no auth. However, I would advise to think twice before it, as this will make a crack in the application security.

Regards,
Aleksey

Ok thanks Aleksey. So to conclude my original question, I understand it is not possible to use a file descriptor for an image in an email template ?

Hi @michael.renaud,

It is possible from what I see. Let’s address this question to @evgeny.zaharchenko.

Regards,
Aleksey

Hi @michael.renaud

It is possible to use file descriptors as images. You can add images as attachments in Attachments tab in the email template or you can add file descriptors via email templates API and then use the provided image in the template as <img src="cid:your image file name">.

Regards,
Evgeny

Hi @evgeny.zaharchenko, could you provide a sample project ? Because if you look earlier in the thread you already proposed that but I found no way to implement it.

How do you want to add images for templates, manually for all templates or for all templates via API?

We have different sets of images for each one of our customers registered as file descriptors that we use at several places (e.g documents).

My goal is to be able to define an email template that uses these file descriptors to pull images, we do not want to edit each template for each customer just to attach an image.

For instance each customer tenant instance has “company_logo.jpg” file that we use in document headers. In a report document template (we use XLS/PDF) I can use this file descriptor in a report band.

Which mean I have one single report template by document for every customer.

The interesting thing with this addon is the visual template designer, if it can use file descriptors that’s great. If not that’s ok, just want to know.

Hi, @michael.renaud I think this can be done with Email templates add-on. I will return to you with an example, how you can send tenant-specific emails with one template.

Hi @michael.renaud,

You can try use following code in your project

public void sendRegisteredUserNotification(User user){
        try {
            String tenantId = findTenantId(user);
            FileDescriptor tenantLogoImage = findTenantImage(tenantId,"company_logo");//file name is company_logo.jpg
            
            emailTemplatesAPI.buildFromTemplate("customer_registered")
                    .setBodyParameter("user",user)
                    .addAttachmentFile(tenantLogoImage)
                    .setTo(user.getEmail())
                    .sendEmail();
        } catch (Exception e) {
            //TODO handle exception
        }
    }

and following HTML code to paste image in email

<img src="cid:company_logo.jpg">

or you can create a template with ${companyLogoImage} parameter and update this parameter via API

emailTemplatesAPI.buildFromTemplate("customer_registered")
                    .setBodyParameter("user",user)
                    .setBodyParameter("companyLogoImage",imageInBase64)
                    .setTo(user.getEmail())
                    .sendEmail();

We have recently published Email template add-on webinar and described basic features and how to use and configure the add-on, please watch https://www.youtube.com/watch?v=JqRexrg4mAs

Regards,
Evgeny

2 Likes

hi @evgeny.zaharchenko,

I have a similar problem. I am trying to include an image attached to an entity via filedescriptor in an email. I insert the image next to a pdf file as an attachment, but only image files that do not have a space in the file name are displayed correctly. Do I have to pay attention to anything when using cid or can I only use files without spaces?
This part works:
<img alt="" src="cid:test.png" width="264">
This one not:
<img alt="" src="cid:test 2.png" width="264">