mjml-solid
v1.4.0
Published
SolidJS component library to generate the HTML emails on the fly
Maintainers
Readme
mjml-solid ·

There is an awesome library mjml with github repo here https://github.com/mjmlio/mjml.
MJML is a markup language created by Mailjet.
So in order to create emails on the fly we created a library with Solid components.
This library depends on Solid's
renderToStringAsyncmethod, which is only available in Node.js. It might be possible to run this library in the browser, but it's not supported.MJML 5 and
mjml-solidrequire Node.js 20 or newer.
Development and test runs require Node.js 22.12 or newer for Vitest 5.
Migrating from 1.3.x
Rendering is now asynchronous. render(email, options) returns a Promise<MJMLParseResults>, and renderToMjml(email) returns a Promise<string>. Await either call before using its result:
const { html, errors } = await render(email, options);
const mjmlSource = await renderToMjml(email);The renderToMjmlAsync export has been removed; replace it with renderToMjml. If you copied this repository's renderSolidEmail/renderSolidEmailAsync example, keep one async renderSolidEmail function and await its result. MJML 5 also ignores mj-include by default; see the include options below.
Getting Started
npm install mjml-solid mjml solid-js- Wire up your render function
import mjml2html from "mjml";
import type { MJMLParseResults, MJMLParsingOptions } from "mjml-core";
import { renderToMjml } from "mjml-solid";
import type { JSX } from "solid-js";
export const renderSolidEmail = async (email: JSX.Element, options?: MJMLParsingOptions): Promise<MJMLParseResults> => {
const mjmlSrc = await renderToMjml(email);
return mjml2html(mjmlSrc, options);
}
- And afterwards write a code like a pro:
import {
Mjml,
MjmlHead,
MjmlTitle,
MjmlPreview,
MjmlBody,
MjmlSection,
MjmlColumn,
MjmlButton,
MjmlImage,
} from "mjml-solid";
import { renderSolidEmail } from "./renderSolidEmail";
const { html, errors } = await renderSolidEmail(
<Mjml>
<MjmlHead>
<MjmlTitle>Last Minute Offer</MjmlTitle>
<MjmlPreview>Last Minute Offer...</MjmlPreview>
</MjmlHead>
<MjmlBody width={500}>
<MjmlSection fullWidth backgroundColor="#efefef">
<MjmlColumn>
<MjmlImage src="https://static.wixstatic.com/media/5cb24728abef45dabebe7edc1d97ddd2.jpg" />
</MjmlColumn>
</MjmlSection>
<MjmlSection>
<MjmlColumn>
<MjmlButton
padding="20px"
backgroundColor="#346DB7"
href="https://www.wix.com/"
>
I like it!
</MjmlButton>
</MjmlColumn>
</MjmlSection>
</MjmlBody>
</Mjml>,
{ validationLevel: "soft" }
);And as the result you will get a nice looking email HTML (works in mobile too!)

Options
mjml-solid sets the following MJML options when rendering to HTML:
{
keepComments: false,
beautify: false,
validationLevel: 'strict'
}If you want to override these, you can pass an object to render as a second argument. MJML 5 defaults minify to false; pass { minify: true } to use its built-in htmlnano/cssnano minifier. See the MJML docs for the full list of options you can set.
MJML 5 ignores mj-include by default. To enable includes, explicitly pass ignoreIncludes: false and scope filePath/includePath to the directories that contain trusted templates.
Extensions
import {
MjmlHtml,
MjmlComment,
MjmlConditionalComment
} from "mjml-solid";
<MjmlComment comment="Built with ... at ..." />
// <!--Built with ... at ...-->
<MjmlConditionalComment comment="MSO conditionals" />
// <!--[if gte mso 9]>MSO conditionals<![endif]-->
<MjmlConditionalComment comment="MSO conditionals" condition="if IE" />
// <!--[if IE]>MSO conditionals<![endif]-->
<MjmlHtml html="<span>Hello World!</span>" />
// <span>Hello World!</span>Utils
We do have also some utils for post processing the output HTML.
Because not all mail clients do support named HTML entities, like '.
So we need to replace them to hex.
import { namedEntityToHexCode } from "mjml-solid";
const html = "<div>'</div>";
namedEntityToHexCode(html);
// <div>'</div>Limitations
Currently mjml and mjml-solid libraries are meant to be run inside of node.
