Build HTML Email And Add Content In Symfony

Symfony creators promote the crafting of email content with the help of Twig templates, another Symfony project. Twig is a template engine for PHP and indeed is a great option to use when creating beautiful emails. It can be customized and offers a list of integrations and extensions. For example, you can use it with the Foundation for Emails framework or create your templates with Markdown. Either way, you can use pure HTML. Let’s start with HTML and then explore Twig’s advanced options.

Add the HTML part directly to the email object (and don’t forget to include the text part, as well):

$email = (new Email())
// ...
->text('Hey! Learn the best practices of building HTML emails and play with ready-to-go templates. Mailtrap’s Guide on How to Build HTML Email is live on our blog')
->html(‘<html>
<body>
<p>Hey<br>
Hey! Learn the best practices of building HTML emails and play with ready-to-go templates.</p>
<p><a href="/blog/build-html-email/">Mailtrap’s Guide on How to Build HTML Email</a> is live on our blog</p>
</body>
</html>')

Embed images

In Symfony Mailer, you have two main options: direct embedding with embed argument or CID attachment. (Refer to your image in the message body by setting its Content-ID and using a standard HTML tag.)

If you have used Swift Mailer before, you will notice that embedding images looks pretty similar:

$email = (new Email())
// ...
//image from a PHP resource, for example, GD
->embed(fopen('/path/to/newlogo.png', 'r'), 'logo')
//image hosted locally or on some external resource
->embedFromPath('/path/to/newcover.png', 'new-cover-image')
// CID attachment
->html('<img src="cid:logo"> ... <img src="cid:new-cover-image"> ...')

Attach files

The way you can attach files to your email is very similar to embedding images.

Here, you have the attachFromPath() method to include files hosted locally or from an external link, as well as attach(), for reading files from a stream:

$email = (new Email())
// …
// local file
->attachFromPath('/path/to/yourconfirmation.pdf')
// external URL - make sure that allow_url_fopen is enabled in your PHP installation
->attachFromPath('http://mailtrap.io/path/to/yourconfirmation', Your Confirmation', 'application/msword')
// PHP resource
->attach(fopen('/path/to/yourconfirmation.pdf', 'r'));

Twig templates

Let’s get back to Twig. It’s integrated with the Mime component, which offers you a wide set of options, including CSS inlining and direct integration with HTML/CSS frameworks.

We won’t dive into Twig templating here – just review its capabilities and give an example of rendering a Twig template in Symfony. For more details on creating email templates with Twig, refer to the Symfony documentation: Twig section and Templates section.

Once you have created your template and saved it as a .twig file, instruct the Mime component to render email content from that file. Use the TemplatedEmail class for this purpose:

use Symfony\Bridge\Twig\Mime\TemplatedEmail;
$email = (new TemplatedEmail())
->from('mailtrap@example.com')
->to('alex@example.com', 'Alex'))
->subject('Experimenting with Symfony Mailer and Mailtrap')
// path to your Twig template
->htmlTemplate('path/experiment.html.twig')
])
;

The current Twig templates are special because they offer support for other frameworks, which you can install as extensions:

  • MarkdownExtension for Markdown
  • InkyExtension for Foundation for Emails (earlier it was called Inky)
  • CssInlinerExtension for CSS inlining

Thank you for taking your time for reading a part of our post on Sending Emails in Symfony! To discover the whole article follow the link to the Mailtrap Guide.