I want to use images in my mail template - they should be sent with the mail. How do I embed them in twig template (a special path or setting to address them?)? Do I attach them as an attachment or are there some special settings?
Asked
Active
Viewed 1.3k times
6
-
Have you tried something already before going to ask something here? – j0k Apr 04 '14 at 10:37
-
I'm in process now :) – jeff Apr 04 '14 at 10:57
2 Answers
14
You can embed images into a Twig template by doing the following:
(Below assumes you've already setup the relevant Swift_Message
as $message
and $data
may contain other information that you're passing through to the Twig template)
$data['image_src'] = $message->embed(Swift_Image::fromPath('path/to/image.jpg'));
$message->setBody(
$this->templating->render('path/to/twigtemplate.html.twig', $data)
);
Then, inside your Twig template, you can use:
<img src="{{ image_src }}" alt="Image Description">
This will embed the image inline in the email to be sent out.

sjdawson
- 141
- 1
- 4
2
You can embed elements while you are creating a body

abdulklarapl
- 184
- 1
- 9
-
I think this documentation is just about embedding in swiftmailer but I guess the question is about embedding elements in *twig template* through swiftmailer – Javad Apr 04 '14 at 21:08
-
Yeah, but in this case you can pass the generated url to template before tpl injection to body? – abdulklarapl Apr 05 '14 at 06:10