Wrapper around MiniJinja and MRML to enable creating templated HTML mails with MJML syntax via a simple web API.
|
All checks were successful
Test / cargo check (push) Successful in 2m28s
|
||
|---|---|---|
| .forgejo/workflows | ||
| src | ||
| .envrc | ||
| .gitignore | ||
| Cargo.lock | ||
| Cargo.toml | ||
| default.nix | ||
| derivation.nix | ||
| flake.lock | ||
| flake.nix | ||
| README.md | ||
| rustfmt.toml | ||
| rustup-toolchain.toml | ||
| shell.nix | ||
| test_v1.json | ||
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";
};
};
}