Home > Blockchain >  How to pass an embedded image in an include template
How to pass an embedded image in an include template

Time:10-25

Okay so I came upon a complicated problem to which I can't seem to find any solution online.

To put things simply, I have a twig file with an embedded picture Branding, which is used like that:

<img src="cid:Branding" alt='branding' />

That "Branding" picture doesn't have a fixed address and is passed to the twig file on email creation, with the embed() function as follows:

$email = (new Email())
                ->from(...)
                ->to(...)
                ->subject(...)
                ->embed($branding['content'], 'Branding', $branding['mimeType'])

The part above works. The "Branding" image is displayed through the twig file, and everything goes fine.

Now, I also need to use this image in the template used for the footer. My problem is the following:

When I use this:

{% include 'email/footer.html.twig' with {'Branding': Branding} %}

I get an error saying it doesn't know the "Branding" variable

When I use this:

{% include 'email/footer.html.twig' with {'Branding': cid:Branding} %}

I get an error saying it can't parse the ":" character.

I considered trying to set a variable for the image too, but I get the same parsing error.

{% set Branding = cid:Branding %}
{% include 'email/footer.html.twig' with {'Branding': Branding} %}

I'm not even sure what I'm trying to do is possible (in that case, would you suggest anything else?). The twig documentation about includes only covers text variables as examples so it's not very useful with what I'm trying to do.

CodePudding user response:

Okay so this is not exactly the solution I was looking for, but I managed to make it work by importing twice the image from the php file.

NOTE: Doing the embed twice from the same image didn't work for me and made both images to fail displaying

$email = (new Email())
                ->from(...)
                ->to(...)
                ->subject(...)
                ->embed($branding['content'], 'branding', $branding['mimeType'])
                ->embed($branding['content'], 'brandingFooter', $branding['mimeType'])

I managed to make it work by importing the image from two different places, like this:

$email = (new Email())
                ->from(...)
                ->to(...)
                ->subject(...)
                ->embed($branding['content'], 'branding', $branding['mimeType'])
                ->embedFromPath("{$this->projectDirectory}/media/logo.png", 'footerBranding')

I still don't understand clearly why I couldn't use the same image twice in the same email, but I managed to get a solution so I'll tag this as the answer.

  • Related