Mjml4j
Java implementation for MJML - a framework that makes responsive-email easy
Install / Use
/learn @digitalfondue/Mjml4jREADME
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 version of mjml. This library is quite compact with a single dependency - the html5 parser (jfiveparse).
License
mjml4j is licensed under the MIT License.
The code is based on the following projects:
- https://github.com/mjmlio/mjml/
- https://github.com/SebastianStehle/mjml-net
- https://github.com/LiamRiddell/MJML.NET
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: will be supported when the work on css selector support is done. WIP
- mj-html-attributes: will be supported when the work on css selector support is done. WIP
Additionally, no pretty print/minimization of the output is provided.
Download
maven:
<dependency>
<groupId>ch.digitalfondue.mjml4j</groupId>
<artifactId>mjml4j</artifactId>
<version>1.1.5</version>
</dependency>
gradle:
implementation 'ch.digitalfondue.mjml4j:mjml4j:1.1.5'
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:
- a string which will be then parsed and processed by the html5 parser (jfiveparse), or it can accept a
org.w3c.dom.Document - 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:
- FileSystemResolver if your resources are present on the filesystem
- SimpleResourceResolver a resolver that need a ResourceLoader to be implemented
Development notes:
- the project has a java 17 baseline
- the code is formatted using the maven spotless plugin. Use
mvn spotless:applyto format it
TODO:
- 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)
- check differences/import tests
Related Skills
node-connect
349.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
109.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
349.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
349.0kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
