All Projects → atrujillofalcon → mjml-rest-client

atrujillofalcon / mjml-rest-client

Licence: other
Java library to convert MJML templates to HTML

Programming Languages

kotlin
9241 projects
java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to mjml-rest-client

easy-email
React.js Drag-and-Drop Email Editor based on MJML. Transform structured JSON data into major email clients compatible HTML. Written in Typescript and supported both in browser and Node.js.
Stars: ✭ 449 (+2706.25%)
Mutual labels:  mjml, mjml-template
tde-webpack-mjml-plugin
Webpack plugin for converting MJML files to HTML
Stars: ✭ 12 (-25%)
Mutual labels:  mjml
mjml-bundle
✉️ Symfony bundle for MJML
Stars: ✭ 87 (+443.75%)
Mutual labels:  mjml
rss-to-email
Generate HTML emails from your RSS feeds.
Stars: ✭ 92 (+475%)
Mutual labels:  mjml
plugin-grapesjs-builder
GrapesJS HTML and MJML integration for Mautic
Stars: ✭ 42 (+162.5%)
Mutual labels:  mjml
zahper
Zahper - The power of MJML into Laravel newsletters
Stars: ✭ 13 (-18.75%)
Mutual labels:  mjml
dnde
Drag and drop react email editor
Stars: ✭ 31 (+93.75%)
Mutual labels:  mjml-api
language-mjml
Atom Editor package providing syntax support for MJML
Stars: ✭ 48 (+200%)
Mutual labels:  mjml
shopify-mail-notifications
Blazing-fast Shopify mail notifications templating environment with Liquid, MJML and Twig
Stars: ✭ 25 (+56.25%)
Mutual labels:  mjml
mjml-server
MJML wrapped in Express for use over HTTP
Stars: ✭ 31 (+93.75%)
Mutual labels:  mjml
mjml-ruby
✉️ MJML parser and template engine for Ruby
Stars: ✭ 49 (+206.25%)
Mutual labels:  mjml
catapulte
Rust implementation of catapulte email sender
Stars: ✭ 113 (+606.25%)
Mutual labels:  mjml
mautic-plugin-grapesbuilder
Grapesjs integration for Mautic
Stars: ✭ 23 (+43.75%)
Mutual labels:  mjml
Mjml
MJML: the only framework that makes responsive-email easy
Stars: ✭ 12,880 (+80400%)
Mutual labels:  mjml
jolimail
Send nice emails
Stars: ✭ 78 (+387.5%)
Mutual labels:  mjml
laravel-mjml
Laravel MJML offers support for rendering MJML syntax into in-line HTML that can be sent within mails.
Stars: ✭ 26 (+62.5%)
Mutual labels:  mjml
mjml-starter-kit
MJML starter kit, create responsive emails very quickly using MJML and this productive toolchain
Stars: ✭ 35 (+118.75%)
Mutual labels:  mjml
mjml-syntax
Sublime package for the MJML
Stars: ✭ 44 (+175%)
Mutual labels:  mjml
mjml-loader
MJML loader for webpack
Stars: ✭ 27 (+68.75%)
Mutual labels:  mjml

Mjml Rest Client

ko-fi

CircleCI Codacy Badge codecov

Mjml is the best responsive mail framework, I love it ❤️. I created this project to have a Java library that use the mjml API to convert a mjml template into valid html ready to use.

Built With

Installing

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"

Usage

Creating templates

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.

File templates

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-text font-style="bold" align="center" color="#8B9C36"><h1 th:text="${myTitle}"></h1></mj-text>
                    <mj-text font-style="bold" align="center" color="#8B9C36"><h3 th:text="${myDescription}"></h3></mj-text>
                    <mj-carousel>
                        <mj-carousel-image src="https://unblogdecode.es/gallery/dog1.jpg"/>
                        <mj-carousel-image src="https://unblogdecode.es/gallery/dog2.jpg"/>
                        <mj-carousel-image src="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 and myDescription that we're going to replace dynamically. Let's see how use the File Template mode:

File fileTemplate = new File("/path/to/mjml/readme-template.mjml");
Context contextVars = new Context();
contextVars.setVariable("myTitle","Dog Gallery");
contextVars.setVariable("message","This is my dog Bilbo, modeling for the camera");
       
String mjmlTemplate = TemplateFactory.builder()
               .withFileTemplate()
               .template(fileTemplate)
               .templateContext(contextVars)
               .buildTemplate();                

Final Result of Template

Mjml Screenshoot

In-Memory String templates

private static final String DUMMY_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>";
...
...
...
Context contextVars = new Context();
contextVars.setVariable("message","Hello MJML");

String mjmlTemplate = TemplateFactory.builder()
                .withStringTemplate()
                .template(DUMMY_TEMPLATE)
                .templateContext(contextVars)
                .buildTemplate();              

API Credentials

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 credentials here.

Before calling our service we have to create an instance of MjmlAuth through the MjmlAuthFactory class. We have three options:

//Get credentials from environments variables
MjmlAuth systemEnvAuthConf = MjmlAuthFactory.builder()
                .withEnvironmentCredentials()
                .mjmlKeyNames(MJML_APP_ID, MJML_SECRET_KEY)
                .build();

//Enter in-memory string credentials directly into auth factory
MjmlAuth memoryAuthConf = MjmlAuthFactory.builder()
                .withMemoryCredentials()
                .mjmlCredentials(mjmlAppId, mjmlSecretKey)
                .build();

//Get credentials from properties file
MjmlAuth propertyAuthConf = MjmlAuthFactory.builder()
                .withPropertiesCredential()
                .properties(propertiesFile)
                .mjmlKeyNames(appPropKey, secretPropKey)
                .build();

Obtaining final HTML

Finally, we just need to instantiate our client with the credentials obtained and use it to convert the template into the final HTML to send it to whoever we want.

MjmlService mjmlService = new MjmlRestService(authConfInstance);

String resultHtmlMail = mjmlService.transpileMjmlToHtml(mjmlTemplate);
//after obtain the html you can send it using your email service implementation.

Running the tests

First you have to set MJML_APP_ID and MJML_SECRET_KEY environment variables.

Execute from root folder:

gradle test

Author

Arnaldo Trujillo

See also the list of contributors who participated in this project.

License

This project is licensed under the Apache License - see the LICENSE.md file for details

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].