mjml-python
Compile MJML at runtime without a Node.js service, external API, or subprocess.
mjml-python is a Python wrapper around the Rust MJML engine MRML, a high-performance port of the official MJML.
Why
Using MJML traditionally requires either:
- running the Node.js-based MJML CLI as a subprocess, or
- calling an external MJML API service.
Both approaches add memory overhead, latency, operational complexity, and cost.
From MRML:
A Node.js server rendering an MJML template takes around 20 MB of RAM at startup and 130 MB under stress test. In Rust, less than 1.7 MB at startup and a bit less that 3 MB under stress test. The Rust version can also handle twice as many requests per second.
By embedding the Rust engine directly, mjml-python avoids all external dependencies while staying compatible with most of MJML (see Known limitations).
Installation
Install from PyPI:
pip install mjml-python
Usage
Call mjml2html() with your MJML string:
from mjml import mjml2html
html = mjml2html(
'''
<mjml>
<mj-body>
<mj-section>
<mj-column>
<mj-image width="100px" src="/assets/img/logo-small.png"></mj-image>
<mj-divider border-color="#F45E43"></mj-divider>
<!-- Say hello to the user -->
<mj-text font-size="20px" color="#F45E43" font-family="Open Sans">Hello World</mj-text>
</mj-column>
</mj-section>
<mj-section>
<mj-column>
<mj-social font-size="15px" icon-size="30px" mode="horizontal">
<mj-social-element name="facebook" href="https://mjml.io/">
Facebook
</mj-social-element>
<mj-social-element name="google" href="https://mjml.io/">
Google
</mj-social-element>
<mj-social-element name="twitter" href="https://mjml.io/">
Twitter
</mj-social-element>
</mj-social>
</mj-column>
</mj-section>
</mj-body>
</mjml>
''',
disable_comments=True,
social_icon_origin="https://example.com/",
fonts={
"Open Sans": "https://fonts.googleapis.com/css?family=Open+Sans:300,400,500,700",
"Ubuntu": "https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700",
}
)
Warnings
mjml2html() reports problems MRML finds while parsing (for example a misspelt attribute) through Python's warnings module as mjml.MjmlWarning, a UserWarning subclass. To fail on them:
import warnings
from mjml import MjmlWarning
warnings.simplefilter("error", MjmlWarning)
Errors
A template that cannot be parsed or rendered raises mjml.MjmlError, a ValueError subclass. It has kind (e.g. "unexpected-token"; see mjml.pyi for all of them), origin (the include path, or None for the root template) and start/end, character offsets into that template's source (so source[start:end] works), or None when MRML reports no location. If an include_loader callback raises, the MjmlError has kind == "include-loader-error", the failing include's path, and the callback's exception as __cause__.
from mjml import MjmlError, mjml2html
src = "<mj-text>Hello</mj-text>"
try:
mjml2html(src)
except MjmlError as e:
e.kind, e.origin, e.start, e.end # ("unexpected-token", None, 0, 8)
Inspecting a template before rendering
parse() returns a parsed template exposing the <mj-title>, <mj-preview> and any warnings MRML recorded while parsing (for example a misspelt attribute). Call render() on it to get the HTML; render() takes the same disable_comments, social_icon_origin and fonts options as mjml2html().
from mjml import parse
template = parse('<mjml lnag="fr"><mj-head><mj-title>Shipped</mj-title></mj-head><mj-body></mj-body></mjml>')
template.title # "Shipped"
template.preview # None
template.warnings # [MjmlWarning("unexpected-attribute at 6:15 in root template")]
html = template.render(disable_comments=True)
Example using Django templates
from django.core.mail import send_mail
from django.template.loader import render_to_string
from mjml import mjml2html
context = {'foo': 'bar'}
text_message = render_to_string('my_text_template.txt', context)
html_message = mjml2html(render_to_string('my_mjml_template.mjml', context))
send_mail(
'Subject here',
text_message,
'from@example.com',
['to@example.com'],
fail_silently=False,
html_message=html_message,
)
Configuration Options
mjml-python supports the following options:
| Name | Type | Default value | Comment |
|---|---|---|---|
disable_comments |
bool |
False |
Strip comments out of rendered HTML |
social_icon_origin |
str | None |
None |
Custom URL origin for social icons. Icon name is appended as-is (e.g. facebook.png), so include a trailing slash. |
fonts |
dict[str, str] | None |
None |
Fonts imported in the HTML rendered by MJML. |
include_loader |
Callable[[str], str] | None |
None |
Fetch the included template using the path attribute. If it raises, MjmlError is raised with the original as __cause__. |
Default fonts (used when fonts=None):
{
"Open Sans": "https://fonts.googleapis.com/css?family=Open+Sans:300,400,500,700",
"Droid Sans": "https://fonts.googleapis.com/css?family=Droid+Sans:300,400,500,700",
"Lato": "https://fonts.googleapis.com/css?family=Lato:300,400,500,700",
"Roboto": "https://fonts.googleapis.com/css?family=Roboto:300,400,500,700",
"Ubuntu": "https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700",
}
Known limitations
mjml-python renders with MRML, which covers most of MJML but not all of it. Differences to be aware of:
mj-html-attributesandmj-selectorare not supported; a template using them fails to compile with aValueError.<mj-raw position="file-start">placed directly under<mjml>fails to parse.- There is no equivalent of MJML's
validationLevel. Unknown tags such as<mj-txet>are copied into the output, and unknown attributes are dropped. MRML only reports a limited set of mistakes (e.g. an unexpected attribute on<mjml>), andmjml-pythonsurfaces those asMjmlWarning. - There are no
minifyorbeautifyoptions. - MRML 6.0.1 is tested against MJML 4.18 output. Alignment with MJML 5 is not released yet.
Why choose mjml-python instead of other MJML packages?
-
Simple, Pythonic API – a single
mjml2html()function that takes plain keyword arguments and returns a clean HTML string. No need to construct parser/render option objects or work with custom result classes. -
Batteries-included defaults – sensible defaults for fonts, comment handling, and include behavior, so most templates work with zero configuration.
-
Flexible include loader – pass a Python callable to handle
<mj-include>however you like (filesystem, database, HTTP, etc.) without learning MRML’s multiple loader types. -
Minimal surface area – intentionally focused on the common case: take MJML input → return production-ready HTML. No extra abstractions.
-
No external services or subprocesses required – rendering is performed entirely inside the Python extension using the embedded Rust MJML engine. No Node.js installation, MJML CLI, or HTTP API needed.
-
Stable wheel builds – lightweight ABI-3 wheels for all major platforms (CPython 3.8+), fast installation, and no compilation required on typical environments.
In short: mjml-python is ideal when you want a straightforward, Python-friendly way to render MJML without dealing with the full MRML configuration surface or external tooling.
Development
With Nix:
nix-shell
With Python 3.8+, uv, Rust 1.83+ and Cargo installed:
uv sync --locked --no-install-project
uv run --locked --no-sync maturin develop
uv run --locked --no-sync python -m unittest
Metadata
Release files for mjml-python 1.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mjml_python-1.5.0.tar.gz | 33.4 kB | With provenance | Details for mjml_python-1.5.0.tar.gz |
Built distributions (wheels)
Total release size: 8.5 MB
Release files / mjml_python-1.5.0.tar.gz
| Download URL | mjml_python-1.5.0.tar.gz |
|---|---|
| Size | 33.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8180361306f79020da7c2977123d6c4825c3b9d66a554cb10e5259935f00d2b4
|
|
BLAKE2b-256 checksum How to use checksums |
807b43cd8143a1e50f4ea0b287fd6bf97a3d5ee97db4387eba1880e3a64a7309
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logRelease files / mjml_python-1.5.0-cp38-abi3-win_amd64.whl
| Download URL | mjml_python-1.5.0-cp38-abi3-win_amd64.whl |
|---|---|
| Size | 996.6 kB |
| Tags | CPython 3.8 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
7fea4a549547e2b80361340f87031cceaaeb885fc5f3f2acc30f7af00feaa3d8
|
|
BLAKE2b-256 checksum How to use checksums |
dad2e047091e04ad34fb89dd09f4152376198f4fb0cc22a3d4a56bf990f238cc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logRelease files / mjml_python-1.5.0-cp38-abi3-musllinux_1_2_x86_64.whl
| Download URL | mjml_python-1.5.0-cp38-abi3-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 1.3 MB |
| Tags | CPython 3.8 Linux musl 1.2+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
ede73cef591dd545591d64c3f1ddece72d010615c2df5404cf623ab78e661d4c
|
|
BLAKE2b-256 checksum How to use checksums |
8da1c7bd881f22cf79af7cff3c690cb4b1a805b1f1af1e18803dbb009de17765
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logRelease files / mjml_python-1.5.0-cp38-abi3-musllinux_1_2_aarch64.whl
| Download URL | mjml_python-1.5.0-cp38-abi3-musllinux_1_2_aarch64.whl |
|---|---|
| Size | 1.4 MB |
| Tags | CPython 3.8 Linux musl 1.2+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
a1e05fb521a3facf7e5a80f00054092c8306e5f575ec92c5a8ca325c61f3c0c8
|
|
BLAKE2b-256 checksum How to use checksums |
b85702eeffacebed12f1d718cfb997d664a466fbb8e8c3078e8f7e24c98b4e00
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logRelease files / mjml_python-1.5.0-cp38-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
| Download URL | mjml_python-1.5.0-cp38-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl |
|---|---|
| Size | 1.2 MB |
| Tags | CPython 3.8 Linux glibc 2.17+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
9c9b41d0fbef9cfac39463fcfa8c8a20f8e4df62a083df52090e003ec034d522
|
|
BLAKE2b-256 checksum How to use checksums |
d2ab25053d670d4cd4fc373b1c6171b6767f5cae90ea7b93f37a33c6d8bac3a5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logRelease files / mjml_python-1.5.0-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
| Download URL | mjml_python-1.5.0-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl |
|---|---|
| Size | 1.3 MB |
| Tags | CPython 3.8 Linux glibc 2.17+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
d870198ec766427c22bea480ecaf009947cec034ddd8f776f23c117a385e851a
|
|
BLAKE2b-256 checksum How to use checksums |
4630310deafbdf656da22de05fb18e463f5647e5194cd46b18d971966d1ee05d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logRelease files / mjml_python-1.5.0-cp38-abi3-macosx_11_0_arm64.whl
| Download URL | mjml_python-1.5.0-cp38-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 1.1 MB |
| Tags | CPython 3.8 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
0bf3f9c43e20f34fba9cb13558e07862e81fa1979b0a6f1884f12f2eab7ab3d9
|
|
BLAKE2b-256 checksum How to use checksums |
9233411d33f89ded2bf5efeeac2e33d56018965dd5c26b63599811035845240c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logRelease files / mjml_python-1.5.0-cp38-abi3-macosx_10_12_x86_64.whl
| Download URL | mjml_python-1.5.0-cp38-abi3-macosx_10_12_x86_64.whl |
|---|---|
| Size | 1.1 MB |
| Tags | CPython 3.8 abi3 macOS 10.12+ x86-64 |
|
SHA-256 checksum How to use checksums |
6e55866456c50c3ea91680df76a8c9793afde57e728d2e4a2909c4d9c3818530
|
|
BLAKE2b-256 checksum How to use checksums |
50a7a850fcdbae6d0d2ed098fc028f4d0bfa596937750b65fc31c4b9954b6755
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logSecurity for mjml-python 1.5.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 8 files in this release have been uploaded with provenance metadata.
Attestation for mjml_python-1.5.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.