WMC SwiftMailer Twig bridge
This bundle provides an easy way to create email templates using Twig for the
SwiftMailer library., (*1)
This helper is inspired from
FOSUB TwigSwiftMailer., (*2)
If you're using FOS User Bundle, we also provide a mailer service drop-in
replacement to support our additional features., (*3)
Installation
With Symfony
The best way to install this extension is through composer:, (*4)
First, require the bundle:, (*5)
composer require wemakecustom/swiftmailer-twig-bundle "^1.0"
Second, enable it:, (*6)
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new WMC\SwiftmailerTwigBundle\WMCSwiftmailerTwigBundle(),
// ...
);
}
Third and finally, configure the web_directory parameter to use it:, (*7)
# config.yml
parameters:
web_directory: %kernel.root_dir%/../web
and you're done., (*8)
FOS User Bundle integration
# config.yml
fos_user:
service:
mailer: wmc.swiftmailer_twig.fosub
With a pure Swiftmailer/Twig
The best way to install this extension is through composer:, (*9)
First, require the bundle:, (*10)
composer require wemakecustom/swiftmailer-twig-bundle "^1.0"
Then give the naming strategy to doctrine's configuration:, (*11)
<?php
$swiftMailerTemplateHelper = new \WMC\SwiftmailerTwigBundle\TwigSwiftHelper($twig, $web_directory);
Usage
It allows a Swift_Message to be populated with a Twig template. The template
expects three blocks (subject
, body_html
, body_text
). If any local image
(i.e. whose src
attribute starting with a single forward-slash /
) with the
class inline-image
is found in the body_html
block, it will be inlined in
the email, allowing some eMail clients to render the image more easily., (*12)
This helper is available as the service wmc.swiftmailer_twig
. To use it,
invoke the method populateMessage
with these three parameters:, (*13)
- the
\Swift_Message
- template name
- data array for the template
The helper depends on Symfony's component DomCrawler, Twig and SwiftMailer., (*14)
Example:, (*15)
$data = [];
// ...
$data['recipient'] = ['name' => 'Jonh Smith', 'email' => 'recipient@example.com'];
$message = $mailer->createMessage()->setTo(['recipient@example.com' => 'John Smith']);
$swiftMailerTemplateHelper->populateMessage($message, 'AppBundle:Mail:my_email.mail.twig', $data);
$mailer->send($message);
{# my_email.mail.twig #}
{% block subject -%}
My email Subject
{%- endblock %}
{% block body_text %}
Hello {{ recipient.name }},
{# ... Awesome plain text email ... #}
Best Regards,
Keep being awesome!
{% endblock %}
{% block body_html %}
Hello {{ recipient.name }},
<p>
{# ... Awesome HTML email ... #}
</p>
<p>
Best Regards,<br />
<em>Keep being awesome!</em>
</p>
{% endblock %}