PHP code example of notfloran / mjml-bundle

1. Go to this page and download the library: Download notfloran/mjml-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

notfloran / mjml-bundle example snippets


// app/AppKernel.php

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

        // ...
    }

    // ...
}

// config/packages/mjml.php
 declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $configurator): void
{
    $configurator->extension('mjml', [
        'renderer' => 'binary',
        'options' => [
            'binary' => '%kernel.project_dir%/node_modules/.bin/mjml',
            'minify' => true,
            'validation_level' => 'skip'
        ]
    ]);
};

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('[email protected]')
        ->to('[email protected]')
        ->subject('Hello from MJML!')
        ->html($htmlBody);

    $mailer->send($email);

    // ...
}

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('[email protected]')
        ->to('[email protected]')
        ->subject('Hello from MJML!')
        ->html($htmlBody);

    $mailer->send($email);

    // ...
}

$message = (new \Swift_Message('Hello Email'))
    ->setFrom('[email protected]')
    ->setTo('[email protected]')
    ->setBody(
        $this->renderView('mail/example.mjml.twig'),
        'text/mjml'
    );

$mailer->send($message);