MJML-Rails
MJML-Rails allows you to render HTML e-mails from an MJML template.
An example template might look like:
<!-- ./app/views/user_mailer/email.mjml -->
<mjml>
<mj-body>
<mj-container>
<mj-section>
<mj-column>
<mj-text>Hello World</mj-text>
<%= render :partial => 'info', :formats => [:html] %>
</mj-column>
</mj-section>
</mj-container>
</mj-body>
</mjml>
And the partial _info.mjml:
<!-- ./app/views/user_mailer/_info.mjml -->
<mj-text>This is <%= @user.username %></mj-text>
- Notice you can use ERb and partials inside the template.
Your user_mailer.rb might look like this::
# ./app/mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
def user_signup_confirmation()
mail(to: '[email protected]', subject: 'test') do |format|
format.text
format.mjml
end
end
end
Installation
Add it to your Gemfile.
gem 'mjml-rails'
Run the following command to install it:
bundle install
Install the MJML parser (optional -g to install it globally):
npm install -g mjml@^2.0
How to guides
Hugo Giraudel wrote a post on using MJML in Rails.
Deploying with Heroku
To deploy with Heroku you'll need to setup multiple buildpacks so that Heroku first builds Node for MJML and then the Ruby environment for your app.
Once you've installed the Heroku Toolbelt you can setup the buildpacks from the commandline:
$ heroku buildpacks:set heroku/ruby
And then add the Node buildpack to index 1 so it's run first:
$ heroku buildpacks:add --index 1 heroku/nodejs
Check that's all setup by running:
$ heroku buildpacks
Next you'll need to setup a package.json file in the root, something like this:
{
"name": "your-site",
"version": "1.0.0",
"description": "Now with MJML email templates!",
"main": "index.js",
"directories": {
"doc": "doc",
"test": "test"
},
"dependencies": {
"mjml": "^2.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/your-repo/your-site.git"
},
"keywords": [
"mailer"
],
"author": "Your Name",
"license": "ISC",
"bugs": {
"url": "https://github.com/sighmon/mjml-rails/issues"
},
"homepage": "https://github.com/sighmon/mjml-rails"
}
Then $ git push heroku master and it should Just WorkTM.
Bug reports
If you discover any bugs, feel free to create an issue on GitHub. Please add as much information as possible to help us fixing the possible bug. We also encourage you to help even more by forking and sending us a pull request.
github.com/sighmon/mjml-rails/issues
Maintainers
- Simon Loffler github.com/sighmon
- Steven Pickles github.com/thatpixguy
License
MIT License. Copyright 2016 Simon Loffler. sighmon.com
Lovingly built on github.com/plataformatec/markerb