Convert MJML to HTML using PHP
MJML is a markup language designed to reduce the pain of coding a responsive email. Our
mjml-php package can convert MJML to HTML.
Here's an example of how to use our package:
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);
The returned HTML will look like the HTML in this snapshot file (it's a bit too large to inline in this readme). Most email clients will be able to render this HTML perfectly.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
You can install the package via composer:
composer require spatie/mjml-php
npm install mjml
... or Yarn.
yarn add mjml
Make sure you have installed Node 16 or higher.
The easiest way to convert MJML to HTML is by using the
use Spatie\Mjml\Mjml; // let's assume $mjml contains the MJML you want to convert $html = Mjml::new()->toHtml($mjml);
If the MJML could not be converted at all a
Spatie\Mjml\Exceptions\CouldNotRenderMjml exception will be thrown.
toHtml() method will just return the converted HTML. There's also a
convert() method that will return an instance of
Spatie\Mjml\MjmlResult that contains the converted HTML and some metadata.
use Spatie\Mjml\Mjml; // let's assume $mjml contains the MJML you want to convert $result = Mjml::new()->convert($mjml); // returns an instance of Spatie\Mjml\MjmlResult
On the returned instance of
Spatie\Mjml\MjmlResult you can call the following methods:
html(): returns the converted HTML
array(): returns a structured version of the given MJML
hasErrors(): returns a boolean indicating if there were errors while converting the MJML
errors(): returns an array of errors that occurred while converting the MJML
errors() method returns an array containing instances of
Spatie\Mjml\MjmlError has the following methods:
line(): returns the line number where the error occurred
message(): returns the error message
formattedMessage(): returns the error message with the line number prepended
tagName(): returns the name of the tag where the error occurred
There are various methods you can call on the
Mjml class to customize the rendering. For instance the
minify() method will minify the HTML that is returned.
use Spatie\Mjml\Mjml; // let's assume $mjml contains the MJML you want to convert $minifiedHtml = Mjml::new()->minify()->toHtml($mjml);
These are all the methods you can call on the
minify(): minify the HTML that is returned
beautify(): beautify the HTML that is returned
hideComments(): hide comments in the HTML that is returned
validationLevel(ValidationLevel $validationLevel): set the validation level to
Instead of using these dedicated methods, you could opt to pass an array with options as the second argument of the
convert method. You can use any of the options that are mentioned in the MJML documentation for Node.js.
use Spatie\Mjml\Mjml; // let's assume $mjml contains the MJML you want to convert $minifiedHtml = Mjml::new()->minify()->toHtml($mjml, [ 'beautify' => true, 'minify' => true, ]);
You can make sure a piece of MJML is valid by using the
use Spatie\Mjml\Mjml; Mjml::new()->canConvert($mjml); // returns a boolean
true is returned we'll be able to convert the given MJML to HTML. However, there may still be some errors while converting the MJML to HTML. These errors are not fatal and the MJML will still be converted to HTML. You can see these non-fatal errors when calling
errors() on the
MjmlResult instance that is returned when calling
You can use
canConvertWithoutErrors to make sure the MJML is both valid and that there are no non-fatal errors while converting it to HTML.
use Spatie\Mjml\Mjml; Mjml::new()->canConvertWithoutErrors($mjml); // returns a boolean
This package also supports running through Sidecar in Laravel projects.
To use the
->sidecar() method, a few extra steps are needed:
Install the Sidecar package:
composer require spatie/mjml-sidecar
MjmlFunction in your
sidecar.php config file.
/* * All of your function classes that you'd like to deploy go here. */ 'functions' => [ \Spatie\MjmlSidecar\MjmlFunction::class, ],
Deploy the Lambda function by running:
php artisan sidecar:deploy --activate
See the Sidecar documentation for details.
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.