James Ball

Web Developer

MJML to HTML Using PHP

If you want to create responsive email templates with ease, you can use MJML, a markup language that simplifies the process. MJML by Spatie uses semantic tags that work well even in Outlook.

First install the composer package:

1
composer require spatie/mjml-php

Then you can render a simple hello world message like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
use Spatie\Mjml\Mjml;

$markup = <<<'MJML'
    <mjml>
      <mj-body>
        <mj-section>
          <mj-column>
            <mj-text>Hello World</mj-text>
          </mj-column>
        </mj-section>
      </mj-body>
    </mjml>
MJML;

$body = Mjml::new()->toHtml($markup);

The MJML PHP package and documentation can be found via GitHub at spatie/mjml-php.