- Notifications
You must be signed in to change notification settings - Fork6
Java library to convert MJML templates to HTML
License
atrujillofalcon/mjml-rest-client
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Mjml is the best responsive mail framework, I love it ❤️. I created this project to have a Java library that use themjml API to convert a mjml template into valid html ready to use.
- Kotlin - The future of JVM languages
- Gradle - Dependency Management
- Spring Rest Template - Used to communicate with API
- Jackson - Best databind Java library
- Thymeleaf - Template Engine
To include this library into your project your only need to add the dependency.
Maven:
<dependency> <groupId>es.atrujillo.mjml</groupId> <artifactId>mjml-rest-client</artifactId> <version>2.0.1</version></dependency>
Gradle:
compile"es.atrujillo.mjml:mjml-rest-client:2.0.1"This library includes Thymleaf engine to allow to create dynamic templates before to send to Mjml API.
We have two options for templating mjml mails. In-memory String or File.
Now we're going to see how create the template from a file source to create a fun mail. Let's imagine that we have a Thymeleaf template in a file called readme-template.mjml with the following content:
<mjml> <mj-body> <mj-container> <mj-section> <mj-column> <mj-textfont-style="bold"align="center"color="#8B9C36"><h1th:text="${myTitle}"></h1></mj-text> <mj-textfont-style="bold"align="center"color="#8B9C36"><h3th:text="${myDescription}"></h3></mj-text> <mj-carousel> <mj-carousel-imagesrc="https://unblogdecode.es/gallery/dog1.jpg"/> <mj-carousel-imagesrc="https://unblogdecode.es/gallery/dog2.jpg"/> <mj-carousel-imagesrc="https://unblogdecode.es/gallery/dog3.jpg"/> </mj-carousel> </mj-column> </mj-section> </mj-container> </mj-body></mjml>
If we look, we have two variables:myTitle andmyDescription that we're going to replace dynamically. Let's see how use the File Template mode:
FilefileTemplate =newFile("/path/to/mjml/readme-template.mjml");ContextcontextVars =newContext();contextVars.setVariable("myTitle","Dog Gallery");contextVars.setVariable("message","This is my dog Bilbo, modeling for the camera");StringmjmlTemplate =TemplateFactory.builder() .withFileTemplate() .template(fileTemplate) .templateContext(contextVars) .buildTemplate();
Final Result of Template
privatestaticfinalStringDUMMY_TEMPLATE ="<mjml><mj-body><mj-container><mj-section><mj-column><mj-text th:text=\"${message}\"></mj-text></mj-column></mj-section></mj-container></mj-body></mjml>";.........ContextcontextVars =newContext();contextVars.setVariable("message","Hello MJML");StringmjmlTemplate =TemplateFactory.builder() .withStringTemplate() .template(DUMMY_TEMPLATE) .templateContext(contextVars) .buildTemplate();
We already have the template, but before to call to API we need the API credentials to initialize our service client.
You can obtain the credentialshere.
Before calling our service we have to create an instance of MjmlAuth through the MjmlAuthFactory class.We have three options:
//Get credentials from environments variablesMjmlAuthsystemEnvAuthConf =MjmlAuthFactory.builder() .withEnvironmentCredentials() .mjmlKeyNames(MJML_APP_ID,MJML_SECRET_KEY) .build();//Enter in-memory string credentials directly into auth factoryMjmlAuthmemoryAuthConf =MjmlAuthFactory.builder() .withMemoryCredentials() .mjmlCredentials(mjmlAppId,mjmlSecretKey) .build();//Get credentials from properties fileMjmlAuthpropertyAuthConf =MjmlAuthFactory.builder() .withPropertiesCredential() .properties(propertiesFile) .mjmlKeyNames(appPropKey,secretPropKey) .build();
Finally, we just need to instantiate our client with the credentials obtainedand use it to convert the template into the final HTML to send it to whoever we want.
MjmlServicemjmlService =newMjmlRestService(authConfInstance);StringresultHtmlMail =mjmlService.transpileMjmlToHtml(mjmlTemplate);//after obtain the html you can send it using your email service implementation.
First you have to setMJML_APP_ID andMJML_SECRET_KEY environment variables.
Execute from root folder:
gradle test
See also the list ofcontributors who participated in this project.
This project is licensed under the Apache License - see theLICENSE.md file for details
About
Java library to convert MJML templates to HTML
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors4
Uh oh!
There was an error while loading.Please reload this page.
