Developed with love by KnpLabs Hire us for your project!
89

mjml-bundle

by notFloran

✉️ Symfony bundle for MJML

MjmlBundle

Latest Stable Version
Latest Unstable Version

Bundle to use MJML 3 and 4 with Symfony >= 3.

Installation

Applications that use Symfony Flex

Open a command console, enter your project directory and execute:

$ composer require notfloran/mjml-bundle

Applications that don't use Symfony Flex

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the
following command to download the latest stable version of this bundle:

$ composer require notfloran/mjml-bundle

This command requires you to have Composer installed globally, as explained
in the installation chapter
of the Composer documentation.

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles
in the app/AppKernel.php file of your project:

// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            // ...
            new NotFloran\MjmlBundle\MjmlBundle(),
        ];

        // ...
    }

    // ...
}

Renderer

For the moment only one renderer is available, the binary renderer.

Binary

Install MJML

$ npm install mjml

Then you need to update the configuration:

# config/packages/mjml.yaml
mjml:
  renderer: binary # default: binary
  options:
    binary: '%kernel.project_dir%/node_modules/.bin/mjml' # default: mjml
    minify: true # default: false
    validation_level: skip # default: strict. See https://mjml.io/documentation/#validating-mjml

Custom

First you must create a class which implements NotFloran\MjmlBundle\Renderer\RendererInterface, then declare it as a service.

And finally you have to change the configuration:

# config/packages/mjml.yaml
mjml:
    renderer: 'service'
    options:
        service_id: 'App\Mjml\MyCustomRenderer'

API

The bundle has no official integration with the MJML API.

You can create your own integration by using juanmiguelbesada/mjml-php and following this gist : https://gist.github.com/notFloran/ea6bab137be628f6a0c19054e08e6906.

Usage

Use "mjml" twig tag

{# mail/example.mjml.twig #}
{% block email_content %}
    {% mjml %}
    <mjml>
        <mj-body>
                <mj-section>
                    <mj-column>

                        <mj-image width="100px" src="https://mjml.io/assets/img/logo-small.png"></mj-image>

                        <mj-divider border-color="#F45E43"></mj-divider>

                        <mj-text font-size="20px" color="#F45E43" font-family="helvetica">
                            Hello {{ name }} from MJML and Symfony
                        </mj-text>

                    </mj-column>
                </mj-section>
        </mj-body>
    </mjml>
    {% endmjml %}
{% endblock %}
public function sendEmail(MailerInterface $mailer)
{
    // The MJMl body is rendered by the mjml tag in the twig file
    $htmlBody = $this->renderView('templates/mail/example.mjml.twig', ['name' => 'Floran']);

    $email = (new Email())
        ->from('my-app@example.fr')
        ->to('me@example.fr')
        ->subject('Hello from MJML!')
        ->html($htmlBody);

    $mailer->send($email);

    // ...
}

Use "mjml" service

{# templates/mail/example.mjml.twig #}
<mjml>
    <mj-body>
            <mj-section>
                <mj-column>

                    <mj-image width="100px" src="https://mjml.io/assets/img/logo-small.png"></mj-image>

                    <mj-divider border-color="#F45E43"></mj-divider>

                    <mj-text font-size="20px" color="#F45E43" font-family="helvetica">
                        Hello {{ name }} from MJML and Symfony
                    </mj-text>

                </mj-column>
            </mj-section>
    </mj-body>
</mjml>
use NotFloran\MjmlBundle\Renderer\RendererInterface;

// ...

public function sendEmail(MailerInterface $mailer, RendererInterface $mjml)
{
    $mjmlBody = $this->renderView('templates/mail/example.mjml.twig', ['name' => 'Floran']);
    $htmlBody = $mjml->render($mjmlBody);

    $email = (new Email())
        ->from('my-app@example.fr')
        ->to('me@example.fr')
        ->subject('Hello from MJML!')
        ->html($htmlBody);

    $mailer->send($email);

    // ...
}

SwiftMailer integration

❗ This integration is deprecated and will be removed in the next major version.

Declare the following service:

NotFloran\MjmlBundle\SwiftMailer\MjmlPlugin:
    tags: [swiftmailer.default.plugin]

Create a SwiftMailer message with a MJML body (without {% mjml %}) and with text/mjml as content-type:

$message = (new \Swift_Message('Hello Email'))
    ->setFrom('send@example.com')
    ->setTo('recipient@example.com')
    ->setBody(
        $this->renderView('mail/example.mjml.twig'),
        'text/mjml'
    );

$mailer->send($message);

The plugin will automatically render the MJML body and replace the body with the rendered HTML.

In the case where a spool is used: the MJML content is save in the spool and render when the spool is flushed.

License

MjmlBundle is licensed under the MIT license.

MIT License

Copyright (c) 2017 Floran Brutel

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
  • Update changelog for 3.4.1
    By web-flow, 2 months ago
  • Merge pull request #62 from notFloran/remove-suggest-swiftmailer
    By web-flow, 2 months ago
  • Remove suggest of SwiftMailer
    By notFloran, 2 months ago
  • Update CHANGELOG and UPGRADE for 3.4 release
    By notFloran, 2 months ago
  • Merge pull request #61 from notFloran/CONTRIBUTING
    By web-flow, 2 months ago
  • Add contributing file
    By notFloran, 2 months ago
  • Merge pull request #59 from notFloran/mjml-plugin-swiftmailer-deprecated
    By web-flow, 2 months ago
  • The Swiftmailer integration is deprecated
    By notFloran, 2 months ago
  • Merge pull request #58 from notFloran/47-sf-mailer-examples
    By web-flow, 2 months ago
  • Use SF Mailer syntax for examples
    By notFloran, 2 months ago
  • Update CHANGELOG
    By notFloran, 2 months ago
  • Add 3.3.0 in CHANGELOG
    By notFloran, 2 months ago
  • Merge pull request #57 from notFloran/update-readme-examples
    By web-flow, 2 months ago
  • Better examples in README
    By notFloran, 2 months ago
  • Remove useless line in travis
    By notFloran, 2 months ago
  • Merge pull request #55 from notFloran/travis-config
    By web-flow, 2 months ago
  • Refactor travis config and add PHP 7.4
    By notFloran, 2 months ago
  • Fix code example
    By web-flow, 2 months ago
  • Merge pull request #54 from notFloran/ecs
    By web-flow, 2 months ago
  • Execute ECS in travis build
    By notFloran, 2 months ago
  • :art: Clean code with ECS
    By notFloran, 2 months ago
  • Setup EasyCodingStandard
    By notFloran, 2 months ago
  • Merge pull request #48 from notFloran/twig-test
    By web-flow, 7 months ago
  • Fix typehint of getGlobals
    By notFloran, 7 months ago
  • Add test for rendering with twig extension
    By notFloran, 7 months ago
  • Merge pull request #46 from notFloran/travis
    By web-flow, 7 months ago
  • Rollback phpunit version to allow PHP 7.1
    By notFloran, 7 months ago
  • :arrow_up: friendsofphp/php-cs-fixer
    By notFloran, 7 months ago
  • :arrow_up: phpunit
    By notFloran, 7 months ago
  • :arrow_up: phpstan
    By notFloran, 7 months ago