MJML Python Wrapper

1.4.0 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

Render a basic MJML string to HTML. The `mjml2html` function takes the MJML string and optional keyword arguments for customization, such as disabling comments or specifying custom fonts. It returns a result object containing the rendered HTML and any errors encountered.

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)

view raw JSON →