MJML PHP is a package by Spatie to Convert MJML to HTML using PHP. MJML stands for Mailjet Markup Language. It is an open-source markup language made to make coding responsive emails simpler. It is a free markup language designed to make creating responsive emails easier. Although the semantic grammar of MJML is similar to that of HTML, it also includes a number of features that make it particularly well-suited for email development. For instance, media queries, flexible layouts, and a sizable component library are all included with MJML.

Using MJML has the advantages listed below:

  1. It is easy to learn and use even if you are not a seasoned programmer.
  2. It makes it easier to create responsive emails that look good on all platforms.
  3. You can save time and effort by automating many of the tasks involved in email development.
  4. The majority of email clients are compatible with it.

Convert MJML to HTML using PHP

The MJML provides a number of tools, such as a Node tool, a CLI tool, and a website where you may manually convert MJML, for converting MJML to HTML. Using one of the various plugins, you can also obtain auto-completion for MJML in your favorite editor.

Convert MJML to HTML Using PHP

Spacie/mjml-php, the most recent version of our program, uses PHP to translate MJML to HTML. It is a beautifully constructed, approachable wrapper for the Node tool. We don’t have to create every PHP feature; we can simply use them all by utilizing the Node tool below. But since you’re utilizing the PHP package, none of this should worry you.

Top 5 Techniques of Hacking Social, iTop Screen Recorder V4.1 License Key

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);

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);

Instead of using the PHP implementation that it offers for converting to HTML, the package instead uses the node version of MJML in the background. The mjml-sidecar package allows you to compile MJML to HTML even when using Sidecar. If you use this package, you won’t need Node on your server, and AWS Lambda will handle all HTML conversions.

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