Skip to content

srghma/mjml-premailer

Repository files navigation

mjml-premailer

Write your emails using mjml framework on rails, using any template language supported by Rails

Travis Gem Version Maintainability Coverage Status

How it works

This gem will processes html part of your mail using mjml cli before delivery, just use mjml_mail function for delivery instead of mail

class ApplicationMailer < ActionMailer::Base
  include MjmlPremailer::MjmlMail # adds function `mjml_mail`

  layout "mailer"
end
class WelcomeMailer < ApplicationMailer
  def welcome(user)
    @user = user
    mjml_mail(to: @user.email, subject: 'Welcome')
  end
end
<!-- app/views/layouts/mailer.html.erb -->

<mjml>
  <mj-head>
    <mj-head>
      My site
    <mj-head>
  <mj-head>
  <mj-body>
    <%= yield %>
  <mj-body>
</mjml>
<!-- app/views/welcome_mailer/welcome.html.erb -->

<mj-text>Hello, <%= @user.name %></mj-text>

Or you can transform mail object yourself using

MjmlPremailer::TransformMail.transform_mail(mail)

Example rails project you can find here

Mjml documentation is here

Installation

Install mjml npm package (v4) globally

$ npm install -g mjml@^4.0

or locally

$ npm install --save-dev mjml@^4.0

Add the gem to your Gemfile:

gem "mjml-premailer"

Configuration options

In /config/initializers/mjml_premailer.rb

MjmlPremailer.config.merge!(
  minify:        Rails.env.production?,  # default - false
  beautify:      !Rails.env.production?, # default - true
  keep_comments: !Rails.env.production?, # default - false

  ## other possible options

  # debug: false,           # default - false
  # bin:   ...,             # by default bin path is found authomatically, but you can specify it here
  # validation_level: :skip # default - :skip, possible options - :strict/:soft/:skip
)

More about options here

Difference from other gems

sighmon/mjml-rails:

  • no support for /app/views/layouts

kolybasov/mjml-ruby:

  • best gem I found, this gem is based on it, thanks kolybasov for his work
  • doesnt support mjml v4 (at time of writing)
  • doesnt support template languages other then erb (at least I didnt managed)

Hacking

make run_env
make test
make publish