MJML PHP is a package by Spatie to Convert MJML to HTML using PHP. MJML stand for Mailjet Markup Language. It is an open-source markup language made to make coding responsive emails simpler. The semantic grammar of MJML is comparable to that of HTML, but it also has a number of properties that make it particularly well-suited for email development. For instance, MJML comes with built-in support for media queries, adaptable layouts, and a large library of components.

The following are some advantages of utilizing MJML:

  • Even if you are not an experienced programmer, it is simple to understand and use.
  • It aids in the creation of responsive emails that display well across all platforms.
  • By automating many of the steps required in email development, it saves you time and effort.
  • It works with the majority of email clients.

Convert MJML to HTML using PHP

The MJML offers a variety of methods for converting MJML to HTML, including a Node tool, a CLI tool, and a website where you may manually convert MJML. You can also get auto-completion for MJML in your preferred editor by using one of the many plugins.

How to Convert MJMl to HMTL using PHP

Our most recent package, spatie/mjml-php, uses PHP to convert MJML to HTML. It’s a skillfully made, user-friendly wrapper for the Node tool. Instead of needing to develop every feature in PHP, we can just use it all by using the Node tool below. But you don’t need to worry about any of this as you’re using the PHP package.

The package can be installed using composer:

composer require spatie/mjml-php

Additionally, check that your project has access to the Node MJML package.

npm install mjml

With this out of the way, you can start converting MJML.

use Spatie\Mjml\Mjml;

// let's assume $mjml contains the MJML you want to convert

$html = Mjml::new()->toHtml($mjml);

Also Read: How to Connect Database in PHP? Using MySQL and PDO

There are a couple of methods, such as beautify, hideComments()minify() to customize the conversion process.

use Spatie\Mjml\Mjml;

// let's assume $mjml contains the MJML you want to convert
$minifiedHtml = Mjml::new()->minify()->toHtml($mjml);

We also added a method that you can use to ensure the given MJML is valid.

use Spatie\Mjml\Mjml;

Mjml::new()->canConvert($mjml); // returns a boolean

From the package’s README file, here is the simplest usage:

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 package uses the node version of MJML in the background rather than the PHP implementation that it has for converting to HTML. Even using Sidecar, you can compile MJML to HTML by using the mjml-sidecar package. You won’t need Node on your server if you use this package, and all HTML conversions will be handled by AWS Lambda.

With the sidecar package installed, you should have to tack on the sidecar() method to execute the conversion on AWS.

use Spatie\Mjml\Mjml;

// let's assume $mjml contains the MJML you want to convert

$html = Mjml::new()->sidecar()->toHtml($mjml);

Leave Your Comment