Symfony 5 Mailer embedded images with liip imagine_filter

843 views Asked by At

In a Symfony 5 project E-Mails are created using the Mailer components TemplatedEmail() function. In the used twig template ($templatedEmail->htmlTemplate(..)) an image is embedded using

<img src="{{ email.image('@v_images/user-'~user.id~'/v.jpg') }}">

to include a user-specific image. This works fine. When trying to pass that image to liip filters by

<img src="{{ email.image('@v_images/user-'~user.id~'/v.jpg') | imagine_filter('v_watermark') }}">

the image deosn't render anymore. I was thinking about the resolver settings to be applied to make liip able to read from the non-standard target path - but didn't find any sensible setting. Documentation didn't lead me to any helpfull information. For any hints on how to make liip work with Mailer embedded images would help me a lot.

EDIT (thanks @ArleighHix):

I also tried nesting the filter inside the call like

<img src="{{ email.image('@v_images/user-'~user.id~'/v.jpg' | imagine_filter('v_watermark')) }}">

which resulted in the following error:

Unable to find template "@v_images/user-1https:/127.0.0.1:8000/media/cache/resolve/v_watermark/v.jpg" (looked into: [SENSIBLE_LOCAL_PATH_IN_PROJECT]).

I am unsure what exactly happens but the path seems to be concatenated weirdly...

1

There are 1 answers

1
Vincent PHILIPPE On

I think you should use absolute_url() combine with asset().

Base on this doc

If you need absolute URLs for assets, use the absolute_url() Twig

<img src="{{ absolute_url(asset('images/logo.png')) }}" alt="Symfony!"/>

<link rel="shortcut icon" href="{{ absolute_url('favicon.png') }}">

Solution 1

I think this one should work :

<img src="{{ email.image((absolute_url(asset('@v_images')) ~ /user-'~user.id~'/v.jpg) | imagine_filter('v_watermark')) }}">

Solution 2

If it don't work, you should add a gloabl variable with base url.

twig.yaml

twig:
  globals:
    app_base_url: '127.0.0.1:8000' // Put your base url here

And use it as :

<img src="{{ email.image(app_base_url ~ asset('@v_images') ~ /user-'~user.id~'/v.jpg) | imagine_filter('v_watermark')) }}">

Of course as you'll publish your website on the web, the base_url would change. You may use a service parameter '%app.base_url%'

Define it in you configuration files :

services_dev.yaml

parameters:
   app.base_url: 'https://127.0.0.1:8000/'

services_prod.yaml

parameters:
   app.base_url: 'https://your.website.url/'

And then update your twig.yaml as

twig:
  globals:
    app_base_url: '%app.base_url%'