1
0
Fork 0
Wrapper around MiniJinja and MRML to enable creating templated HTML mails with MJML syntax via a simple web API.
Find a file
Bauke Drijkoningen 628a3638bd
All checks were successful
Test / cargo check (push) Successful in 2m28s
Add an example NixOS config.
2025-05-30 11:56:32 +02:00
.forgejo/workflows Add test_v1.json as a Rust test. 2025-05-30 11:37:08 +02:00
src Add test_v1.json as a Rust test. 2025-05-30 11:37:08 +02:00
.envrc Initial commit! 2025-05-01 10:29:46 +02:00
.gitignore Add the Nix package derivation. 2025-05-01 11:10:00 +02:00
Cargo.lock Add initial setup with MiniJinja and MRML. 2025-05-01 10:56:12 +02:00
Cargo.toml Add the Nix package derivation. 2025-05-01 11:10:00 +02:00
default.nix Add the Nix package derivation. 2025-05-01 11:10:00 +02:00
derivation.nix Add the Nix package derivation. 2025-05-01 11:10:00 +02:00
flake.lock Initial commit! 2025-05-01 10:29:46 +02:00
flake.nix Initial commit! 2025-05-01 10:29:46 +02:00
README.md Add an example NixOS config. 2025-05-30 11:56:32 +02:00
rustfmt.toml Initial commit! 2025-05-01 10:29:46 +02:00
rustup-toolchain.toml Initial commit! 2025-05-01 10:29:46 +02:00
shell.nix Initial commit! 2025-05-01 10:29:46 +02:00
test_v1.json Add initial setup with MiniJinja and MRML. 2025-05-01 10:56:12 +02:00

mrml-api

Wrapper around MiniJinja and MRML to enable creating templated HTML mails with MJML syntax via a simple web API.

Usage

In the request a context object and template string are expected.

The context is the data that will be available in the MiniJinja render step, which will run over the template inserting its data.

Then MRML will run over the MJML template and render out the email HTML.

For example, using this test_v1.json file and the mrml-api server running on port 8000:

{
  "context": {
    "title": "Title Test"
  },
  "template": "<mjml lang=\"en\"><mj-body>{{ title }}</mj-body></mjml>"
}
curl -X POST http://127.0.0.1:8000/api/v1/render -d @test_v1.json
<!doctype html><html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office"><head><title></title><!--[if !mso]><!--><meta http-equiv="X-UA-Compatible" content="IE=edge"><!--<![endif]--><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
#outlook a { padding: 0; }
body { margin: 0; padding: 0; -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; }
table, td { border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; }
img { border: 0; height: auto; line-height: 100%; outline: none; text-decoration: none; -ms-interpolation-mode: bicubic; }
p { display: block; margin: 13px 0; }
</style>
<!--[if mso]>
<noscript>
<xml>
<o:OfficeDocumentSettings>
  <o:AllowPNG/>
  <o:PixelsPerInch>96</o:PixelsPerInch>
</o:OfficeDocumentSettings>
</xml>
</noscript>
<![endif]-->
<!--[if lte mso 11]>
<style type="text/css">
.mj-outlook-group-fix { width:100% !important; }
</style>
<![endif]-->
<style type="text/css"></style></head><body style="word-spacing:normal;"><div lang="en">Title Test</div></body></html>

Example deployment with NixOS

Here is an example deployment I use in my NixOS configuration that builds the derivation.nix as a package and then uses it inside a Systemd service.

# imports/mrml-api.nix
{ lib, pkgs, ... }:

let
  src = pkgs.fetchFromGitea {
    domain = "forgejo.drijkoningen.dev";
    owner = "bauke";
    repo = "mrml-api";
    rev = "b4bfa88a0391dfa2568af976f9498e042518d078";
    sha256 = "sha256-t9GzdXD5MC456sO5bG+ZYPWsl/d0a4nxiLbToNiHrjg=";
  };
in
pkgs.callPackage "${src}/derivation.nix" { }
# systemd/mrml-api.nix
{ config, pkgs, ... }:

let
  mrml-api = (pkgs.callPackage ../imports/mrml-api.nix { });
in
{
  systemd.services."mrml-api" = {
    path = [
      mrml-api
    ];

    wantedBy = [ "multi-user.target" ];

    serviceConfig = {
      # NOTE: Run at port 8160, this can be any available port.
      Environment = "ROCKET_PORT=8160";
      ExecStart = "${mrml-api}/bin/mrml-api";
      Type = "simple";
    };
  };
}