While auditing the security of Notifuse, our email platform, I found that every MJML engine available in Go had security holes, and that none of them matched MJML's output well. So I built safe-mjml 1.0, a new open-source MJML engine written in Go. My goal is to make it the best MJML library in the world, in any language.
That's a big claim, so the rest of this post is about how we measure it. Today, safe-mjml follows MJML 5.4.1 and matches its HTML on 99.3% of our test documents, runs about 5 times faster than MJML, and is safe with templates written by your users.
Our standards
We hold safe-mjml to three standards.
The first is output. Email clients are tested against MJML's HTML, and Outlook in particular punishes anything that drifts from it, so an engine whose HTML is "almost the same" as MJML's ships broken layouts to real inboxes. We compare byte for byte, and write down every difference we keep with its reason.
The second is speed. Rendering is on the hot path of every campaign and every transactional email, and it shouldn't be the reason a send is slow.
The third is safety, and most engines skip it. When your customers write the templates, the template engine is part of your attack surface, and a template should never be able to crash the process, eat all the memory, or inject markup into an attribute.
Each of those standards has tests in the repository, and each benchmark below comes with the command to run it on your own machine.
Why we built it
In Notifuse, our users write their own templates, so the MJML engine renders whatever they paste in. MJML itself is a JavaScript project: from Go, you end up running a Node.js process next to your service, or embedding MJML in WebAssembly or a JavaScript interpreter. So we looked at the engines written in Go, and gave them templates written to cause harm.
One of them crashes the whole process when two emails render at the same time. Another one turns an escaped " back into a raw quote, so data that was escaped correctly still adds an onclick to the HTML. With 1 MB of inline styles, the ones that accept it need between 1 minute and 5 minutes. And when we started, the best of them gave the same HTML as MJML on 82.4% of our test documents. None of them was designed for untrusted input, so we wrote one that is.
safe-mjml started from gomjml, Preslav Rachev's native Go port of MJML, and we owe him a lot for that head start. Most of the work came after: we went through every component and every attribute, compared our output with MJML 4.18 until they matched, and wrote the tests that keep them matched.
MJML 5
safe-mjml 1.0 followed MJML 4.18. Version 1.1, released on November 18, follows MJML 5.4.1. Every email changes by a few bytes that clients render the same, except in mj-hero, whose inner-padding now applies in every client and not only in Outlook. It also brings what MJML 5 added, such as the gutter attribute on mj-section for the space between columns, and it now supports mj-html-attributes. The module path doesn't change and the API only gains two names for the limits described below, so upgrading takes one command:
go get github.com/Notifuse/safe-mjml/mjml@v1.1.0Same HTML
MJML 5.4.1 is pinned in the repository, and the test suite compares safe-mjml's output with it on 8,795 documents. Those documents cover every attribute of every component, set at every level where MJML lets you set it.
Every attribute on the element itself, in an mj-class, in mj-attributes, in mj-all, on the parent and on the grandparent: 6,944 of 6,987 documents identical.
MJML's 25 official templates: all 25 identical.
The documents of MJML's own test suite: all 65 that have a reference are identical.
495 hand-written test cases: 484 identical, and the 11 others are explained in the compatibility notes.
516 templates with one author mistake each: rendered exactly as MJML renders them, or refused with the line of the mistake.
Templates containing Liquid tags: every Liquid tag that MJML keeps, safe-mjml keeps too.
Matching MJML is the baseline. Where safe-mjml deliberately departs from it, it's to fix MJML's output, as the next section explains.
Where it differs
In our library comparison, safe-mjml's HTML differs from MJML's on 57 documents out of 8,093. 50 of those differences are on purpose, in places where MJML writes HTML that is broken or not what the author meant. The 7 others come from the minifier used for the comparison, and match MJML in safe-mjml's own tests. Here is what we changed:
safe-mjml never writes
NaNorInfinity. Leave some values empty in MJML and you getNaNas a width, height or padding in the HTML you send. safe-mjml keeps the value as written or leaves the property out.MJML's CSS inliner protects
{{ }}and<% %>but not Liquid's{% %}, and it drops a tag that sits alone in astyleattribute. If you personalize emails with Liquid, as Notifuse does, safe-mjml keeps every tag where you put it. And where MJML 5 fails on astyleattribute that holds only Liquid, such asstyle="{{ styles.paragraph }}", safe-mjml still renders the email.MJML writes a
"in an attribute value as is, which can break the tag or let a template inject markup. safe-mjml escapes it.MJML wraps every
mj-social-elementlink in the network's share URL, so a link to your X or LinkedIn profile becomes a "share" link. safe-mjml links to your profile.An
mj-navbar-linkwithouthrefin a navbar with abase-urlends withundefinedin MJML. safe-mjml links to the base URL, so noundefinedends up in your links.A missing close tag, text before
<mjml>or content after</mjml>is an error with the line at fault, instead of an email with parts missing. A component placed outsidemj-bodyis dropped, as in MJML, but safe-mjml warns about it where MJML stays silent.When MJML fails on an
mj-html-attributesselector, an invalid one for instance, safe-mjml skips it with a warning and renders the email.
Every one of these is listed in docs/differences-from-mjml.md. Any difference that isn't on that list is a bug, and we want to hear about it.
About 5× faster
Measured on GitHub's Linux arm64 runner, against MJML 5.4.1 running on Node.js 24:
The Austin template from mjml.io renders in 1.48 ms, compared with 7.8 ms in MJML.
A typical Notifuse email takes 0.70 ms, compared with 4.9 ms.
Running the CLI 50 times takes 0.25 s and 10 MB of memory, compared with 26.0 s and 151 MB.
Untrusted templates
We designed safe-mjml for the worst case, a template written to hurt you, so you can put it behind a template editor that your customers use.
A panic never reaches your program. It comes back as an error, with its stack.
Nesting depth is limited, rendering time and memory grow linearly with the size of the template, and the HTML output is capped at 16 MB.
Inline
mj-styleandmj-html-attributesstop at a work limit.mjml.WithLimitschanges the limits of a render, anderrors.Is(err, mjml.ErrLimitExceeded)tells a template refused by a limit from an author mistake.Attribute values are always escaped.
A malformed template is refused with the line at fault, instead of being rendered with pieces missing.
safe-mjml never touches the file system or the network and never executes code, and concurrent renders are safe.
Each of these has its own tests. On top of that, every pull request runs a fuzzer, 52 scaling tests on hostile templates, a memory test on each, and the race detector. safe-mjml does not sanitize the HTML you put inside mj-text or mj-raw; that part is still up to you.
Comparison
We ran every MJML library we could use from Go on the same 8,093 documents and compared each one's output with MJML 5.4.1. safe-mjml matched 99.3% of them. Most of the others still write MJML 4's HTML, so they matched none.
safe-mjml: 99.3% identical, with no render failures.
julez-dev/mjmlgo, whose v2 targets MJML 5.4.1: 0.1% identical, or 29.7% if you ignore whitespace inside Outlook comments, and 3,068 failures, mostly templates it rejects where MJML would only warn.
krozhkov/mgml ports MJML 4.18, so none of its documents match MJML 5.4.1 (against MJML 4.18, it matched 82.4%), and it has 21 failures. It also crashes the whole process when two renders run at the same time.
preslavrachev/gomjml writes MJML 4's body, so none are identical, and it has 648 failures, mostly templates it rejects for an attribute MJML accepts.
TcMits/mjml2html, which runs MJML 4.15 in a JavaScript interpreter, and codedninja/mrmlgo, which calls MRML, the Rust port, through cgo: neither produced a single document identical to MJML 5.4.1.
We also gave each of them templates written to cause harm, and every other engine failed at least one test.
Given a quote in an attribute, MJML itself, mgml and mjmlgo write it raw, so the rest of the value becomes a new attribute, such as an
onclick. mjmlgo does it even when the quote was escaped as".When two renders run at once, mgml crashes the process.
safe-mjml renders 1 MB of inline styles in 0.35 s, mjmlgo in 1 min 19 s, and mgml in 5 minutes. gomjml refuses such templates from 61 KB.
The full method, the library versions and the commands to reproduce both comparisons are in docs/library-comparison.md. If you maintain one of these libraries and think we measured it unfairly, open an issue and we'll run it again.
What's missing
Being the best MJML engine also means supporting everything MJML supports, and we aren't there yet. safe-mjml doesn't handle mj-include or custom components, nor MJML's minify, beautify and .mjmlconfig options. It renders what an mj-include holds, as MJML 5 does, but never reads the file it names. Its output is compact HTML: MJML's HTML with the whitespace collapsed, as MJML 4 wrote it with minify.
Try it
The code is on GitHub and the docs are on pkg.go.dev. Run it on your own templates, especially the ugly ones. When safe-mjml and MJML disagree, open an issue with the MJML that shows it, and we'll fix it.
Getting started
go get github.com/Notifuse/safe-mjml/mjmlimport "github.com/Notifuse/safe-mjml/mjml"
html, err := mjml.Render(`<mjml>
<mj-body>
<mj-section>
<mj-column>
<mj-text>Hello World!</mj-text>
</mj-column>
</mj-section>
</mj-body>
</mjml>`)And there's a CLI:
go install github.com/Notifuse/safe-mjml/cmd/safe-mjml@latest
safe-mjml compile input.mjml -o output.htmlsafe-mjml is MIT-licensed, and there's no Node.js, WebAssembly or cgo involved. The only dependencies are the standard library and golang.org/x/net.
