MJML to HTML using PHP
Published on by Paul Redmond
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' <mjml> <mj-body> <mj-section> <mj-column> <mj-text invalid-attribute>Hello World</mj-text> </mj-column> </mj-section> </mj-body> </mjml> MJML; $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.