Skip to content

Java implementation for MJML - a framework that makes responsive-email easy

License

Notifications You must be signed in to change notification settings

digitalfondue/mjml4j

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Jan 16, 2025
0f4cdec · Jan 16, 2025

History

52 Commits
Jun 16, 2024
Feb 24, 2024
Jan 15, 2025
Jan 16, 2025
Feb 18, 2024
Feb 18, 2024
Jan 15, 2025
Feb 24, 2024
Feb 24, 2024
Feb 18, 2024
Feb 25, 2024
Jan 15, 2025

Repository files navigation

MJML4J

A java based mjml implementation.

Require at least java 17.

Javadoc: https://javadoc.io/doc/ch.digitalfondue.mjml4j/mjml4j

Why

As far as I know, there is no pure java porting of mjml. This library is quite compact (~150Kb) with a single dependency - the html5 parser (jfiveparse) (~153kb).

License

mjml4j is licensed under the MIT License.

The code is based on the following projects:

Status

Most of the mj-* tags are supported. It's currently missing:

  • mj-include: will be implemented implemented in 1.1.1
  • mj-style: the inline attribute will be ignored
  • mj-html-attributes: will not be supported, as it requires a css selector

Additionally, no pretty print/minimization of the output is provided.

Download

maven:

<dependency>
   <groupId>ch.digitalfondue.mjml4j</groupId>
   <artifactId>mjml4j</artifactId>
   <version>1.1.2</version>
</dependency>

gradle:

implementation 'ch.digitalfondue.mjml4j:mjml4j:1.1.2'

Use

If you use it as a module, remember to add requires ch.digitalfondue.mjml4j; in your module-info.

The api is quite simple:

package ch.digitalfondue.test;

import ch.digitalfondue.mjml4j.Mjml4j;

public class App {
   public static void main(String[] args) {
      Mjml4j.Configuration configuration = new Mjml4j.Configuration("en");
      String renderedTemplate = Mjml4j.render("""
              <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>
                                      
                      <mj-text font-size="20px" color="#F45E43" font-family="helvetica">Hello World</mj-text>
                                      
                    </mj-column>
                  </mj-section>
                </mj-body>
              </mjml>
              """, configuration);
      System.out.println(renderedTemplate);
   }
}

The render static method accept as a parameters:

  1. a string which will be then parsed and processed by the html5 parser (jfiveparse), or it can accept a org.w3c.dom.Document
  2. a configuration object with language, optionally a direction and an IncludeResolver

mj-include support

By default, mjml4j don't have an IncludeResolver configured, thus mj-include will not work out of the box, you must implement or specify yourself. mjml4j offer 2 implementations:

TODO:

  • mj-include implemented in 1.1.1
  • check https://github.com/mjmlio/mjml/compare/v4.14.1...v4.15.3 , printing especially
  • validation api:
    • add "parent element" check
    • attribute unit type check
  • improve the renderer
  • cleanup/rewrite the box model, kinda hacky
  • more robust handling of invalid input (check mjml behaviour)