MJML Python Wrapper
mjml-python is a Python wrapper for MRML, a high-performance Rust port of MJML. It enables rendering MJML templates into email-compatible HTML entirely within Python, eliminating the need for external Node.js services or subprocesses. Currently at version 1.4.0, the library is actively maintained with regular updates, often coinciding with upstream MRML changes.
Warnings
- gotcha This `mjml-python` library is a wrapper for a Rust implementation (MRML) and is distinct from another `mjml` package on PyPI, which is a pure Python port. Ensure you install `mjml-python` for the Rust-backed performance benefits.
- gotcha The `mjml-python` library, while feature-rich, might not implement every single feature or component found in the official Node.js MJML implementation or other alternative Python ports. Always verify compatibility for advanced or less common MJML features.
- gotcha When using `mjml-python` with user-supplied MJML templates, be aware of potential cross-site scripting (XSS) vulnerabilities if untrusted data is directly inserted into the templates. Like HTML, MJML itself does not inherently sanitize all inputs, and custom HTML in `mj-raw` tags or other components could render scripts.
- breaking The underlying MRML (Rust) library, which `mjml-python` wraps, undergoes major version bumps (e.g., v1.3.5 updated MRML to 5.0.0). While the `mjml-python` wrapper aims to maintain API stability, significant changes in MRML could potentially introduce subtle behavioral differences or require updates to MJML syntax expectations.
Install
-
pip install mjml-python
Imports
- mjml2html
from mjml import mjml2html
Quickstart
from mjml import mjml2html
mjml_template = '''
<mjml>
<mj-body>
<mj-section>
<mj-column>
<mj-text font-size="20px" color="#F45E43">Hello World</mj-text>
</mj-column>
</mj-section>
</mj-body>
</mjml>
'''
result = mjml2html(mjml_template,
disable_comments=True,
fonts={
"Open Sans": "https://fonts.googleapis.com/css?family=Open+Sans:300,400,500,700"
}
)
if result.errors:
print("MJML rendering errors:", result.errors)
else:
print(result.html)