Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to attach images in swiftmailer to embed them in twig mail template?

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?

like image 426
jeff Avatar asked Apr 04 '14 10:04

jeff


2 Answers

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.

like image 142
sjdawson Avatar answered Nov 06 '22 23:11

sjdawson


You can embed elements while you are creating a body

documentation

like image 2
abdulklarapl Avatar answered Nov 06 '22 21:11

abdulklarapl