MJML to HTML using PHP


August 31st, 2023

MJML to HTML using PHP

MJML PHP is a package by Spatie to Convert MJML to HTML using PHP. MJML is a markup language designed to reduce the pain of writing responsive email templates. Templates use semantic markup and are even responsive in Outlook.

Here's the most basic usage from the package's README file:

use Spatie\Mjml\Mjml;
$mjml = <<<'MJML'
<mj-text invalid-attribute>Hello World</mj-text>
$html = Mjml::new()->toHtml($mjml);

MJML doesn't have a PHP implementation to convert to HTML—the package uses the node version behind the scenes. You can even use the mjml-sidecar package to compile MJML to HTML using Sidecar. Using this package, you won't need to have Node available on your server, and all conversions to HTML will be done in AWS Lambda.

To get started with this package, you should familiarize yourself with the Documentation for MJML. The MJML PHP package and documentation can be found on GitHub at spatie/mjml-php.

If you'd like to learn more background on the creation of this package, read Freek Van der Herten's writeup, Craft emails that look good in each email client using MJML.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.