Git Product home page Git Product logo

mjml-rails's Introduction

MJML-Rails

Build Status Gem Version

MJML-Rails allows you to render HTML emails from an MJML template.

Note: As of MJML 4.3.0 you can no longer use <mj-text> directly inside an <mj-body>, wrap it in <mj-section><mj-column>.

An example template might look like:

<!-- ./app/views/user_mailer/user_signup_confirmation.mjml -->
<mjml>
  <mj-head>
    <mj-preview>Hello World</mj-preview>
  </mj-head>
  <mj-body>
    <mj-section>
      <mj-column>
        <mj-text>Hello World</mj-text>
        <%= render partial: "info", formats: [:html] %>
      </mj-column>
    </mj-section>
  </mj-body>
</mjml>

And the partial:

<!-- ./app/views/user_mailer/_info.html.erb -->
<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]", from: "[email protected]") do |format|
      format.text
      format.mjml
    end
  end
end

Example application

Installation

Using MJML NPM package

Add it to your Gemfile.

gem 'mjml-rails'

Run the following command to install it:

bundle install

Add the MJML parser to your project with your favourite package manager:

# with npm
npm install mjml

# or install it globally (The best way for Rails 7.x.x with importmaps)
npm install -g mjml

# with yarn
yarn add mjml

MJML-Rails falls back to a global installation of MJML but it is strongly recommended to add MJML directly to your project.

You'll need at least Node.js version 6 for MJML to function properly.

Using MRML with included binaries

If for some reason you can't or don't want to run JS code in your production environment, you can use MRML. It ships with already compiled binaries for Rust implementation of MJML so it has no external dependencies.

Add mjml-rails and mrml to your Gemfile.

gem 'mjml-rails'
gem 'mrml'

Run the following command to install it:

bundle install

Set use_mrml option to true in your initializer:

# config/initializers/mjml.rb
Mjml.setup do |config|
  config.use_mrml = true
end

Note: MRML does not fully support all MJML functionalities, see Missing implementations

Configuration

MJML-Rails has the following settings with defaults:

  • template_language: :erb

    ERB can be used inside MJML templates by default. Possible values are all template languages that you have installed, e.g. :haml or :slim.

    Note: If you’re using Haml/Slim layouts, please don’t put <mjml> in comments in your partial. Read more at #34.

  • raise_render_exception: true

    Exceptions will be raised and passed to your application by default.

    Beware that setting it to false leads to an empty html email when an exception is raised, so only set this to false if you do not rely on html (e.g. you have a text fallback for your emails).

  • minify: false

  • beautify: true

  • validation_level: "strict"

    MJML-Rails will raise an exception on any template error by default.

    If set to soft, MJML will render invalid templates and ignore invalid parts. This means in case of an invalid template those invalid parts will be missing from the output.

    See MJML validation documentation for all possible values.

  • mjml_binary: nil

    This can be used to specify the path to a custom MJML binary if it is not detected automatically (shouldn't be needed).

  • mjml_binary_version_supported: "4."

    MJML-Rails checks the version of the MJML binary and fails if it does not start with this version, e.g. if an old version is installed by accident.

  • use_mrml: false Enabling this will allow you to use Rust implementation of MJML via the mrml gem. It comes with prebuilt binaries instead of having to install MJML along with Node. When enabled the options mjml_binary_version_supported, mjml_binary, minify, beautify and validation_level are ignored.

  • fonts By default, MJML-Rails uses MJML default fonts, but enables you to override it. Example : config.fonts = { Raleway: 'https://fonts.googleapis.com/css?family=Raleway }

# config/initializers/mjml.rb
Mjml.setup do |config|
  # Use :haml as a template language
  config.template_language = :haml

  # Ignore errors silently
  config.raise_render_exception = false

  # Optimize the size of your emails
  config.beautify = false
  config.minify = true

  # Render MJML templates with errors
  config.validation_level = "soft"

  # Use MRML instead of MJML, false by default
  config.use_mrml = false

  # Use custom MJML binary with custom version
  config.mjml_binary = "/path/to/custom/mjml"
  config.mjml_binary_version_supported = "3.3.5"

  # Use default system fonts instead of google fonts
  config.fonts = {}
end

MJML v3.x & v4.x support

Version 4.x of this gem brings support for MJML 4.x

Version 2.3.x and 2.4.x of this gem brings support for MJML 3.x

If you'd rather still stick with MJML 2.x then lock the mjml-rails gem:

gem 'mjml-rails', '2.2.0'

For MJML 3.x lock the mjml-rails gem:

gem 'mjml-rails', '2.4.3'

And then to install MJML 3.x

npm install -g [email protected]

How to guides

Kitty Giraudel wrote a post on using MJML in Rails.

Using Email Layouts

Note: Aleksandrs Ļedovskis kindly updated the gem for better Rails Email Layouts support - it should be a non-breaking change, but check the updated file naming below if you experience problems.

Mailer:

# mailers/my_mailer.rb
class MyMailer < ActionMailer::Base
  layout "default"

  def foo_bar(user)
    @recipient = user

    mail(to: user.email, from: "[email protected]") do |format|
      format.html # This will look for "default.html.erb" and then "default.html.mjml"
    end
  end
end

Note: If default.html.erb exists, email will be rendered as ERB, and MJML tags will not be compiled.

Email layout:

<!-- views/layouts/default.html.mjml -->
<mjml>
  <mj-body>
    <%= yield %>
  </mj-body>
</mjml>

Email view:

<!-- views/my_mailer/foo_bar.html.mjml (or foo_bar.html.erb) -->
<%= render partial: "to" %>

<mj-section>
  <mj-column>
    <mj-text>
      Something foo regarding bar!
    </mj-text>
  </mj-column>
</mj-section>

Email partial:

<!-- views/my_mailer/_to.html.mjml (or _to.html.erb) -->
<mj-section>
  <mj-column>
    <mj-text>
      Hello <%= @recipient.name %>,
    </mj-text>
  </mj-column>
</mj-section>

Sending Devise user emails

If you use Devise for user authentication and want to send user emails with MJML templates, here's how to override the devise mailer:

# app/mailers/devise_mailer.rb
class DeviseMailer < Devise::Mailer
  def reset_password_instructions(record, token, opts={})
    @token = token
    @resource = record
    # Custom logic to send the email with MJML
    mail(
      template_path: 'devise/mailer',
      from: "[email protected]",
      to: record.email,
      subject: "Custom subject"
    ) do |format|
      format.text
      format.mjml
    end
  end
end

Now tell devise to user your mailer in config/initializers/devise.rb by setting config.mailer = 'DeviseMailer' or whatever name you called yours.

And then your MJML template goes here: app/views/devise/mailer/reset_password_instructions.mjml

Devise also have more instructions if you need them.

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": "^4.0.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

Other similar gems

License

MIT License. Copyright 2018 Simon Loffler. sighmon.com

Lovingly built on github.com/plataformatec/markerb

mjml-rails's People

Contributors

adrianob avatar amekss avatar anhtrantuan avatar carlosantoniodasilva avatar chris-pinuts avatar clairezed avatar colszowka avatar der-flo avatar doits avatar dyanagi avatar jipiboily avatar josevalim avatar loqimean avatar lucasmazza avatar mpetnuch avatar mudge avatar nashby avatar nobodysnightmare avatar pbougie avatar petergoldstein avatar pokonski avatar rahearn avatar raybrownco avatar sbiastoch avatar sighmon avatar stevenh512 avatar thatpixguy avatar tmaier avatar tylerhunt avatar yuta-hayashi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mjml-rails's Issues

Library doesn't support MJML 4.x

As far as I can tell this library doesn't support MJML 4.x, instead you get this error which is confusing:

Couldn't find the MJML binary.. have you run $ npm install mjml?

Can be fixed by installing mjml ^3.0 but it is worth mentioning in the docs or improving the error msg.

RuboCop?

Are you interested in integrating RuboCop in your Gem?

I'm willing to create a pull request containing a RuboCop setup and fixes for all offenses in the existing code. I would start with the default RuboCop configuration which seems common sense in the community.

HAML links resulting in inline HTML

Issue

When using mjml-rails in conjunction with haml, the expected haml behavior of embedding a Ruby link into a line with = resulted in inline HTML instead of a functioning link.

%mj-text
    = "Link your Twitter, Slack, and GitHub accounts today. #{link_to 'Set up integrations >', user_settings_url(@user)}"

Expected behavior

The code above should render a working link, correctly formatted, like this:

Screenshot 2021-03-18 at 16 35 05

Resulting behavior

The HTML appears inline in the corresponding view.

Screenshot 2021-03-18 at 16 35 50


Solutions

We were able to get the links working correctly with either == or ! (examples below). We ultimately went with the bang method (!), but neither option successfully bypassed our Prettier checks.

%mj-text
    == "Link your Twitter, Slack, and GitHub accounts today. #{link_to 'Set up integrations >', user_settings_url(@user)}"

With the bang method, we're able to prevent interpolation escaping. More info here.

%mj-text
    ! Link your Twitter, Slack, and GitHub accounts today. #{link_to 'Set up integrations >', user_settings_url(@user)}

We were able to get everything to work by adding the file in question to our Prettier ignore file.

Support variables as used in Mailjet

Mailjet's MJML templates allow templating of variables using this syntax: {{var:variable_name}}. data is also used as a key for things that come from the recipient's profile in Mailjet. It'd be good if this library supported templating in that format so that templates are portable between a local environment and Mailjet.

pre render mjml

Hey guys,
this might be a stupid question and if so feel free to close this.

I was wondering if there was a way to pre-render the mjml to html so we are not always rendering mjml to erb and erb to html. This way we would pre render mjml to erb and then when we wanted to send the email we would render the erb to html.

Some kind of caching mechanism

Request out to google fonts for Ubuntu font necessary when using Webmock.

I ran into this issue when running tests that would send emails. I'm using webmock to stub out the requests and it seems like mjml pulls in the Ubuntu font from google fonts api. After spending some time on a strange error inside of premailer-rails I figured out what mjml-rails needed to get it working.
The following gist has a webmock response.
https://gist.github.com/skunkworker/c7d4cc065bdeaafa71307f18652f00c4

Is there an easier way to disable this for tests?

MJML 4.8.x broke my validator again :)

I don't have time to dig into this one as thoroughly as last time, sorry 🙁 I have a nasty feeling it may be filed under 'feature not a bug' at the MJML end so I'll have to adapt somehow, but I thought I'd run it past you as it is a change in behaviour of/via your gem, without your code having changed, which I would imagine is quite frustrating!

Layout: plugins/ShinyNewsletters/app/views/layouts/newsletter_mailer.html.mjml
Partial: spec/fixtures/TEST/views/shiny_newsletters/newsletter_mailer/an_example.html.mjml
Validator: app/validators/mjml_syntax_validator.rb
Test: plugins/ShinyNewsletters/spec/models/shiny_newsletters/template_spec.rb#L44

Up to MJML 4.7.1 the validator passed and now it doesn't. If I move the layout code into the partial, then the validator passes, so it seems like either it was wrapping the partial in the layout before and isn't now, or it was less strict about templates being a whole document?

If this is just a case of my needing to tweak a config setting somewhere to allow partials to render (and you could point me at it!) that would be great 😄 Otherwise, I've pinned MJML to 4.7.1 for now and hopefully I'll have more time to dig into this again late February/early March.

'AbstractController::ActionNotFound' error with mj-font & Google font unescaped URL

Pulling in multiple font weights the official way via Google Fonts url will throw an error:

AbstractController::ActionNotFound (Email 'css2' not found in [[[Mailer Preview Here]]]):

In order to pull in multiple font multiple font weights from Google, this is the tag used:

<mj-font name="Inter" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" />

Looks like MJML is getting held up on the unescaped semicolons being used in the ?family param. Escaping those semicolons like so:

<mj-font name="Inter" href="https://fonts.googleapis.com/css2?family=Inter:wght@400%3B500%3B600%3B700&display=swap" />

... gets around the error. Not blocking me, but figured I'd flag it.

support for mjml 4.1.0

The last version of mjml is 4.1.0 but mjml-rails not recognise this bin.
If I install mjml 4.0.0 with npm, npm install 4.1.0. (npm install [email protected])
If I use mjml-rails setup in a initializer and I put

Mjml.setup do |config|
config.mjml_binary_version_supported = "4.1."
end

not works.

"No such file or directory - npm (Errno::ENOENT)"

Hi,
I've got a particularly frustrating error: I can't run my Rails app from RubyMine because I get:
"/home/###/.rbenv/versions/2.4.4/lib/ruby/gems/2.4.0/gems/mjml-rails-4.2.4/lib/mjml.rb:29:in ``': No such file or directory - npm (Errno::ENOENT)"

If I launch in terminal the command "npm bin", I get "/home/####/myproject/node_modules/.bin"
I don't understand why npm bin would fail

npm version is 5.0.3
node version is v8.1.4

Please advise
thank you

Invalid mjml silently fails

If you input invalid mjml, mjml-rails doesn't raise any error and just siltenly fails.

Example:

<mj-section>
        <mj-column>
            <mj-text align="left"> Some text for your preheader</mj-text>
        </mj-column>
        <mj-column>
            <mj-text align="right"><a href="email-permalink"> Read in the browser </a></mj-text>
        </mj-column>
      </mj-section>
      <mj-section background-url="https://mjml.io/assets/img/background-url.jpg">
          <mj-column>
              <mj-image src="https://metricswatch.com/wp-content/uploads/2017/04/[email protected]"></mj-image>
              <mj-text align="center"> Lorem ipsum </mj-text>
              <mj-button> Discover <%= 2 %> </mj-button>
          </mj-column>
      </mj-section>

This doesn't render and returns nothing (nil or empty string, not sure, didn't check).

It would be relatively easy to catch MJMLError or something like that and raise when we get that from the external rendering through the npm module.

Thoughts?

Blank emails on production

Hi,

can't find any solutions so far for this, hope you could have some ideas about it.
Our mjml powered emails work perfectly when we use deliver_later, come out empty when we use deliver_now.
If we try the same methods from console, everything works.

Everything works fine in development too, I thought it could be something related to envs, but I could find any references of envs in the gem, am I wrong?
Thanks

Email very slow to be sent: compilation time is the cause ?

Hi and thank you for this gem!

I have one question: is there a possibility to compile only once the mjml to html and not everytime an email is sent ?

I'm seeing an increase from 10ms to 1000 to 2000ms to send an email. I imagine that the cause is the mjml compilation.

Thank in advance for your help!

Problem with mime-types using turbolinks

When using turbolinks to sign_in with devise, I have the following error:

ActionController::MissingRenderer in Devise::SessionsController#new
No renderer defined for format: mjml
responders (2.2.0) lib/action_controller/responder.rb:211:in `api_behavior'
responders (2.2.0) lib/action_controller/responder.rb:193:in `rescue in to_format'
responders (2.2.0) lib/action_controller/responder.rb:185:in `to_format'
responders (2.2.0) lib/action_controller/responder.rb:163:in `respond'
responders (2.2.0) lib/action_controller/responder.rb:156:in `call'
responders (2.2.0) lib/action_controller/respond_with.rb:205:in `respond_with'
devise (4.1.1) app/controllers/devise/sessions_controller.rb:12:in `new'
actionpack (4.2.6) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.2.6) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.6) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.6) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.6) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.6) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
activesupport (4.2.6) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.6) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
activesupport (4.2.6) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
activesupport (4.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.6) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.6) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.6) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.6) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.6) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.6) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.6) lib/active_record/railties/controller_runtime.rb:18:in `process_action'

I'm not sure how it exactly works, but I suspect that rails is trying to find out how to respond based on the MimeType which is text/html. Mjml-rails register also :mjml as text/html and it seems to create some kind of conflict. If I remove the minme-type registration inside Mjml-rails, then everything works fine, but the emails have the wrong MimeType.

I sent hours on it, but couldn't find any solution. Do you have any idea?

Couldn't find the MJML 4.0. binary

Hi folks,

I'm having a bit of trouble getting the library to detect my MJML 4.0. binary. I installed MJML globally, using Yarn and I am using gem 'mjml-rails', '~> 4.0.0'

$ mjml -V
mjml-core: 4.1.0
mjml-cli: 4.1.0

$ which mjml
/usr/local/bin/mjml

$ rails c
Running via Spring preloader in process 24555
Loading development environment (Rails 5.1.3)
[1] pry(main)> Mjml.discover_mjml_bin
Couldn't find the MJML 4.0. binary.. have you run $ npm install mjml?
=> nil

Do I need to add some additional config?

Thanks,

Joe

Support for mj-include tags

Not sure if this is actually possible within rails as it may be related more to how rails processes templates than with the gem itself, but I was wondering if it was possible to fully support the mj-include tag within views.

The gem provides the usual rails ability to use an email layout and yield the individual mail templates which is great, however I'm using an extension for Visual Studio Code which lets me view previews of the emails as I write them which is extremely helpful. Unfortunately because this extension uses just plain mjml it doesn't work with the rails template loading so it almost immediately fails to render emails due to them being invalid, or they're not complete.

To get around the preview issue I was trying to use <mj-include> instead which was working well, however when it came to rendering an actual email, mjml threw out errors about not being able to find the included file because it was now all in the /tmp directory somewhere. I was forced to then go back to using the rails template stuff which is fine and it works and I can use the ActionMailer::Preview classes to generate previews, but it is faster having it instantly render in my editor

Change in error raising behaviour

Hi,

I'm seeing a change in error-raising behaviour between 4.4.0 and 4.4.1, and looking at the code I think it may be a bug in the Parser.run method - specifically where it decides whether to raise an exception for an invalid MJML input file:
063152c#diff-5c149cf160539fd426925ca3b65fc5dafb2a1c9d5ae56df3212543e112d242fdR40

In every case that I've tested (three or four, not loads), status.success? is returning true when the MJML template is invalid, where stderr contains content and stderr.eof? returned false with the previous code. This means that instead of a ParseError being raised, an empty string is always rendered and returned - regardless of the setting of Mjml.raise_render_exception.

Previously I was catching the ParseError in a validator in my Rails app when people attached an MJML template to a database record - so with 4.4.1, my validator passes everything and my CMS allows items to be saved with invalid syntax in their related MJML files.

I'll post some more info below, let me know if you need something specific that I haven't included. Also if there's a better way I should be checking for valid MJML syntax in an ActiveRecord validator :)

Cheers,
Denny

denny@rocinante:~/ShinyCMS$ rspec ./plugins/ShinyNewsletters/spec/models/shiny_newsletters/template_spec.rb:46
ShinyNewsletters::Template
  validations
    mjml_syntax

From: /home/denny/.rvm/gems/ruby-2.7.2/gems/mjml-rails-4.4.0/lib/mjml/parser.rb:40 Mjml::Parser#run:
    38:     command = "#{mjml_bin} -r #{in_tmp_file} -o #{out_tmp_file.path} --config.beautify #{beautify} --config.minify #{minify} --config.validationLevel #{validation_level}"
    39:     _, _, stderr, _ = Open3.popen3(command)
 => 40:     binding.pry
    41:     raise ParseError.new(stderr.read.chomp) unless stderr.eof?

[1] pry(#<Mjml::Parser>)> stderr.eof?
false
[2] pry(#<Mjml::Parser>)> stderr.read(130)
"Line 4 of /tmp/in20201126-2467728-h1e7c3.mjml (mj-title) \xE2\x80\x94 mj-title cannot be used inside mj-column, only inside: mj-attributes,"
[3] pry(#<Mjml::Parser>)> 
      fails to create a new Template if the template file is not valid MJML

Finished in 17.64 seconds (files took 3.1 seconds to load)
1 example, 0 failures
denny@rocinante:~/ShinyCMS$ rspec ./plugins/ShinyNewsletters/spec/models/shiny_newsletters/template_spec.rb:46
ShinyNewsletters::Template
  validations
    mjml_syntax

From: /home/denny/.rvm/gems/ruby-2.7.2/gems/mjml-rails-4.4.1/lib/mjml/parser.rb:40 Mjml::Parser#run:
    38:     command = "-r #{in_tmp_file} -o #{out_tmp_file.path} --config.beautify #{beautify} --config.minify #{minify} --config.validationLevel #{validation_level}"
    39:     _, stderr, status = Mjml.run_mjml(command)
 => 40:     binding.pry
    41:     raise ParseError.new(stderr.chomp) unless status.success?

[1] pry(#<Mjml::Parser>)> stderr
"Line 4 of /tmp/in20201126-2468496-1yrhn3n.mjml (mj-title) — mj-title cannot be used inside mj-column, only inside: mj-attributes, mj-head\n"
[2] pry(#<Mjml::Parser>)> status.success?
true
[3] pry(#<Mjml::Parser>)> 
      fails to create a new Template if the template file is not valid MJML (FAILED - 1)

Failures:
  1) ShinyNewsletters::Template validations mjml_syntax fails to create a new Template if the template file is not valid MJML
     Failure/Error: expect( update_successful ).to be false
            expected false
            got true
     # ./plugins/ShinyNewsletters/spec/models/shiny_newsletters/template_spec.rb:51:in `block (4 levels) in <module:ShinyNewsletters>'

Finished in 13.08 seconds (files took 3.1 seconds to load)
1 example, 1 failure

(I've edited the above runs for length, mostly by removing the first pass each takes through the MJML parser, which I assume is the layout.)

Problem setup with Rails

Hello,

Thank you for your work! We are currently trying to use mjml-rails at Edenspiekermann, but we are facing an issue to make it work, unfortunately. :(

We manage to render the email and everything, but MJML templates are not compiled. The <mjml-*> tags are reaching the browser, and no compilation to HTML ever seems to happen.

Screenshot

Here is our setup:

  • ruby 2.2.0
  • rails 4.2.6
  • mjml-rails 2.1.1

Here is what we did:

# ./Gemfile
gem 'mjml-rails', '~> 2.1.1', require: 'mjml'
# ./app/mailers/foobar_mailer.rb
class FoobarMailer < ActionMailer::Base
  def baz()
    mail(to: '[email protected]', subject: 'test')
  end
end
# ./app/views/foobar_mailer/baz.mjml
<% name = 'bar' %>
<mjml>
  <mj-body>
    <mj-container>
      <mj-text>Hello <%= name %></mj-text>
    </mj-container>
  </mj-body>
</mjml>
# ./app/spec/mailers/previews/foobar_mailer_preview.rb
class FoobarMailerPreview < ActionMailer::Preview
  def baz
    FoobarMailer.baz
  end
end
# ./config/routes.rb
if Rails.env.development?
    get '/rails/mailers'       => "rails/mailers#index"
    get '/rails/mailers/*path' => "rails/mailers#preview"
end

Note that we did correctly install the mjml npm package globally.

Any idea what’s happening? Ping my colleague @pgoetze.

Using Email Layouts in Rails 6.0.0 (with a sample application)

Hi, thank you for creating this great gem!

I faced an issue with using default layouts while using this gem in Rails 6.0.0.

I followed this section with Rails 6, but my MJML tags weren't compiled into HTML.

https://github.com/sighmon/mjml-rails#using-email-layouts

Screenshot:

image

My workaround is to change

    mail(to: user.email, from: "[email protected]") do |format|
      format.html
    end

to

    mail(to: user.email, from: "[email protected]") do |format|
      format.mjml { render layout: "mailer.html.mjml" } # Specify the MJML layout
    end

and change the filename user_signup_confirmation.html.erb to user_signup_confirmation.mjml.erb

Now, it works as expected with the layout.

image

I created a sample application with mjml-rails in Rails 6 (with no database, to make it easier to setup), which is currently working with the workaround.

https://github.com/dyanagi/example_mjml_rails

The Git commit to creating this sample:

dyanagi/example_mjml_rails@b30ac7c

Notes:

  • I didn't use the gem in the older rails versions.
  • mailer.html.mjml was generated automatically by rails g mailer foobar command

mjml-rails seems to be incompatible with mjml 4.7.0

Recently, MJML released a new version, 4.7.0.

mjml-rails was working fine for us until that point, but after the update, this line began to throw some errors (in a file called _header.html.mjml ).

%mj-column{"width" => "70%"}

MJML for some reason doesn't recognize the % unit and adds px, causing failures.

Failure/Error: %mjml

ActionView::Template::Error:
  Line 43 of /tmp/in20200921-1055-tu28bu.mjml (mj-column) — Attribute width has invalid value: 70%px for type Unit, only accepts (px, %) units and 1 value(s)
------------------
--- Caused by: ---
Mjml::Parser::ParseError:
  Line 43 of /tmp/in20200921-1055-tu28bu.mjml (mj-column) — Attribute width has invalid value: 70%px for type Unit, only accepts (px, %) units and 1 value(s)

Could anyone give any direction as to why this may be an issue?

<mj-hero> height bug

Heys!
just setup mjml-rails on a blank rails project. I've copy pasted <mj-hero> component from the docs to test if everything was working and got this bug:

https://cl.ly/3R1z4437453g

some old school delete refresh debugging, realised that the problem is on theline-height attribute on <mj-text>.

After removing it, everything works as expected:
https://cl.ly/0E140A0t3a14

Full mjml code, from the docs:

<mjml>
  <mj-body>
    <mj-container>
      <mj-hero mode="fixed-height" height="469px" background-width="600px" background-height="469px" background-url="https://cloud.githubusercontent.com/assets/1830348/15354890/1442159a-1cf0-11e6-92b1-b861dadf1750.jpg" background-color="#2a3448" padding="100px 0px">
        <!-- To add content like mj-image, mj-text, mj-button ... use the mj-hero-content component -->
        <mj-hero-content width="100%">
          <mj-text padding="20px" color="#ffffff" font-family="Helvetica" align="center" font-size="45" line-height="45" font-weight="900">
            GO TO SPACE
          </mj-text>
          <mj-button href="https://mjml.io/" align="center">
            ORDER YOUR TICKET NOW
          </mj-button>
        </mj-hero-content>
      </mj-hero>

    </mj-container>
  </mj-body>
</mjml>

Disclaimer, i'm a designer and frontend dev, but super noob at RoR. So won't be a great help on fixing this, sry 😓.
As I've said, i started this as a blank rails project, besides the default gems only mjml-rails & letter_opener were added, so that's the full setup.

Cheers, and keep up the good work against the HTML Emails beast!

System: macOS El Capitain 10.11.6
Ruby:  2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
Rails: Rails 5.0.0.1
Mjml-rails: (2.4.0)
letter_opener: (1.4.1)

Default node: v4.2.6 (have others installed with nvm)
mjml: 3.1.1

Problem processing non-ERB (Haml, Slim) layouts

In #33 / v4.2.0 a change in logic was made around assumption that layout template will always contain verbatim <mjml>.

In ERb world this (mostly) holds, but when layout is composed using advanced templating languages like Haml and Slim, both allowing tag definition without <> wrapping, it fails.

Also, it doesn't handle edge case where somebody is referencing/writing code comment in partial that is e.g. <%# Let's use MJML in this partial. Note that <mjml> must reside in layout %> - currently, mjml-rails will take that comment's <mjml> as flag to process template with MJML library. Or when root tag is generated using content_tag. Or when it's contained in a partial.

Current workaround: Put verbatim <mjml> in Haml/Slim layouts. Avoid dynamically generating MJML root tag. Don't reference <mjml> proper anywhere else except MJML layout files.


Simple change of

if template.source =~ /<mjml>/

to

if compiled_source =~ /<mjml>/

would solve no <> and comment-triggering-MJML situations, but still leave other aforementioned edge cases broken.


Possible solutions:

  1. Doing first-pass render of template to process nested partials/helpers, capture this output and use same match (=~ /<mjml>/) as done currently.

  2. Require some kind of magic comment in file (e.g. # mjml_processing: true) that would serve as signal to do MJML processing of rendered file.

  3. Require users to whitelist paths to MJML-processed layout/legacy-template files in initializer, akin to Asset Pipeline compilation does. Then just match against template.identifer


/cc @sighmon

Yarn bin command no longer functions as it is used in lib/mjml.rb

Running yarn bin as used in the lib/mjml.rb file no longer returns a bin directory, but a list of available binaries. In newer versions of Yarn, you must run yarn bin mjml to get a direct path to the binary. I'll take this as a PR if nobody else is able to.

Minification of output files

Mjml cli allows files to be minified automatically using --config.beautify false --config.minify true. The defaults are --config.beautify true --config.minify false.

Can you add a config option to the gem so that it is possible to change the default to minify the output?

support for mail layouts

I have the following ActionMailer classes:

class ApplicationMailer < ActionMailer::Base
  layout 'mailer'
end

class UserMailer < ApplicationMailer
  def password(user)
    @user = user
    mail to: @user.email, subject: 'Senha de acesso' do |format|
      format.mjml
    end
  end
end

This is my app/views/layouts/mailer.mjml:

<mjml>
  <mj-body>
    <mj-container>
      <mj-section>
        <mj-text>
          header
        </mj-text>
      </mj-section>
      <mj-section>
        <%= yield %>
      </mj-section>
      <mj-section>
        <mj-text>
          footer
        </mj-text>
      </mj-section>
    </mj-container>
  </mj-body>
</mjml

and this is my app/views/user_mailer/password.mjml

<mj-text>
  password content
</mj-text>

When I preview this email I can see the header and footer text, but no content. I also see this in the logs:

19:18:42 log.1     | Started GET "/rails/mailers/user_mailer/password?part=text%2Fhtml" for 127.0.0.1 at 2016-08-05 19:18:42 -0300
19:18:42 log.1     |   Event Load (0.2ms)  SELECT  "events".* FROM "events" WHERE "events"."acronym" = $1 LIMIT 1  [["acronym", "RAILS"]]
19:18:42 log.1     | Processing by Rails::MailersController#preview as HTML
19:18:42 log.1     |   Parameters: {"part"=>"text/html", "path"=>"user_mailer/password"}
19:18:42 log.1     |   User Load (0.5ms)  SELECT  "users".* FROM "users"  ORDER BY "users"."id" ASC LIMIT 1
19:18:42 log.1     |   Rendered user_mailer/password.mjml within layouts/mailer (0.0ms)
19:18:42 web.1     | Warning: Please upgrade your MJML markup to add a <mjml> root tag, <mj-body> as root will no longer be supported soon, see https://github.com/mjmlio/mjml/blob/master/UPGRADE.md
19:18:42 log.1     |   Rendered user_mailer/password.mjml within layouts/mailer (439.8ms)

Any hint in how to make mail layouts work with it?

Haml

I'm trying to use Haml instead of Erb to write my MJML templates. When I simply keep the mjml suffix (mail.mjml) the Haml is rendered as-is (not converted to HTML). When I use any combination of mjml, haml and html (e.g. mail.mjml.haml) the template cannot be found. How can I accomplish this?

Create and register custom components

Heys! thanks to the community for the work on this 🙏

This is a question, more than an issue. I was wondering how can we register mjml custom components using this setup?

I know that since we're in rails and have proper templating, we can just code custom component as partials, but i'm thinking on situation where the components are built in isolation elsewhere (as module), installed via npm/yarn and then imported, registered and ready to be used.

Let me know if this makes any sense.
Cheers ☮️
R.
(not the brightest ruby/rails dev here)

MJML not found with NVM setup

Background

After a format of my machine and careful new setup via my dotfiles, I have Node installed only via NVM (no duplicate Homebrew Node version at /usr/local/node) and have installed global Node packages using npm with the recommended approach. This includes Yarn, which they recommend installing via npm.

Point being I'm fairly confident I have a "clean" Node setup with no duplicates, everything installed as-per docs and scoped to the chosen NVM Node version.

Problem

I have mjml in my project's package.json and have ran yarn.

"mjml": "^4.9.3",

But I get this error from mjml-rails:

Couldn't find the MJML 4. binary.. have you run $ npm install mjml?

Even if make MJML a global package with npm install -g mjml I get the same error.

I'm not sure if local project npm install mjml works, but we use Yarn instead. Does mjml-rails rely on local project npm usage instead of Yarn?

Working locally but not in Heroku

Hi,

We've followed the instructions by:

  • adding the gem:
    gem 'mjml-rails'
  • installing it
    bundle install

and creating the buildpacks. This is the output of heroku buildpacks:

1. heroku/nodejs
2. heroku/ruby

It works great locally, but when trying to run it in heroku, it does not raise any error, it simply sends the email in mjml markup, without being interpreted.

Any leads about why the mjml markup is not translated into html in heroku?

Thanks!

Gem Load Error on Heroku review app

Hi @sighmon,

I'm having an issue when deploying a branch to Heroku's review apps.

The key errors I see are:
Bundler::GemRequireError: There was an error while trying to load the gem 'mjml-rails'
Gem Load Error is: undefined method chomp' for nil:NilClass NoMethodError: undefined method chomp' for nil:NilClass /tmp/.../.../vendor/bundle/ruby/2.1.0/gems/mjml-rails-2.4.1/lib/mjml.rb:25:in discover_mjml_bin'

I've set heroku to run the nodejs buildpack first.

On the CI platform, I have npm install as the first build instruction, but it feels like it's not triggering it. The strange thing is that it works on production.

Hope you can point out where I'm going wrong. 😃

Cannot find MJML bin.

Opening this task early while I continue to debug:

[7] pry(main)> reload!; UserMailer.confirmation_instructions(User.first, '123').deliver
Reloading...
   (1.0ms)  SET NAMES utf8mb4 COLLATE utf8mb4_bin,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
  User Load (0.4ms)  SELECT  `users`.* FROM `users` ORDER BY `users`.`id` ASC LIMIT 1
  Postcard Load (0.6ms)  SELECT  `postcards`.* FROM `postcards` WHERE `postcards`.`user_id` = 2 ORDER BY `postcards`.`id` DESC LIMIT 1
  Rendering users/mailer/confirmation_instructions.mjml within layouts/mailer
  Rendered users/mailer/confirmation_instructions.mjml within layouts/mailer (41.1ms)
  Rendering users/mailer/confirmation_instructions.mjml within layouts/mailer
  Rendered users/mailer/confirmation_instructions.mjml within layouts/mailer (8.7ms)
UserMailer#confirmation_instructions: processed outbound mail in 137.1ms
ActionView::Template::Error: Couldn't find the MJML binary.. have you run $ npm install mjml?
from /Users/amirsharif/.rvm/gems/ruby-2.4.1/gems/mjml-rails-2.4.3/lib/mjml/parser.rb:8:in `initialize'
[8] pry(main)> Mjml::BIN
=> nil
[9] pry(main)> Mjml.discover_mjml_bin
=> "/Users/amirsharif/Projects/sapco/node_modules/.bin/mjml"
[10] pry(main)> reload!; UserMailer.confirmation_instructions(User.first, '123').deliver
Reloading...
  User Load (0.9ms)  SELECT  `users`.* FROM `users` ORDER BY `users`.`id` ASC LIMIT 1
  Postcard Load (0.4ms)  SELECT  `postcards`.* FROM `postcards` WHERE `postcards`.`user_id` = 2 ORDER BY `postcards`.`id` DESC LIMIT 1
  Rendering users/mailer/confirmation_instructions.mjml within layouts/mailer
  Rendered users/mailer/confirmation_instructions.mjml within layouts/mailer (17.2ms)
  Rendering users/mailer/confirmation_instructions.mjml within layouts/mailer
  Rendered users/mailer/confirmation_instructions.mjml within layouts/mailer (16.3ms)
UserMailer#confirmation_instructions: processed outbound mail in 102.1ms
ActionView::Template::Error: Couldn't find the MJML binary.. have you run $ npm install mjml?
from /Users/amirsharif/.rvm/gems/ruby-2.4.1/gems/mjml-rails-2.4.3/lib/mjml/parser.rb:8:in `initialize'
[11] pry(main)> Mjml::BIN
=> nil
[12] pry(main)> Mjml::BIN = Mjml.discover_mjml_bin
(pry):12: warning: already initialized constant Mjml::BIN
/Users/amirsharif/.rvm/gems/ruby-2.4.1/gems/mjml-rails-2.4.3/lib/mjml.rb:33: warning: previous definition of BIN was here
=> "/Users/amirsharif/Projects/sapco/node_modules/.bin/mjml"

Support JSON MJML templates

Context is shared with mjmlio/mjml#2343:

I'm working with Mailjet's templates API. Templates get returned from this in JSON format. If I can't get that converted back to standard MJML so that it's compatible with this library, it'd at least be good to be able to use it directly.

It's already compatible with the CLI, so I think the only gatekeeper here might be the validation that the file starts with an <mjml> tag.

Version 4.2.0 breaks email rendering

My setup includes email templates, partials, and layouts. Using version 4.1.0, everything renders beautifully. Update to version 4.2.0 (or 4.2.1) and all the files are loading correctly but the MJML does not render into HTML.

I've tried renaming the files as described in the new README but it doesn't help. Tried different variations but nothing worked. Either the template is not found or the MJML does not render.

Working setup in version 4.1.0:

# Gemfile
gem 'mjml-rails', '4.1.0'

# mailers/test_mailer.rb
class TestMailer < ApplicationMailer
  layout 'test'

  def confirmation
    mail(to: '[email protected]', from: '[email protected]') do |format|
      format.mjml
    end
  end
end

# Template + Layout:
# views/layouts/test.mjml
# views/test_mailer/confirmation.mjml.erb

Non-working setup in version 4.2.1

# Gemfile
gem 'mjml-rails', '4.2.1'

# mailers/test_mailer.rb
class TestMailer < ApplicationMailer
  layout 'test'

  def confirmation
    mail(to: '[email protected]', from: '[email protected]') do |format|
      format.html
    end
  end
end

# Template + Layout:
# views/layouts/test.html.mjml
# views/test_mailer/confirmation.html.erb

No errors are thrown but the MJML is not converted into HTML. The log shows all templates and layouts are found and rendered.

Inline styling not being applied in heroku

I have some emails made with mjml that worked well in the dev setup but when we deployed to heroku they lost some of the styles.

Narrowing it down, I noticed that only the ones declared under <mj-style inline="inline"> were missing. All of the mjml got translated along with its default styles (for columns and tables for example)

This, for example, would change the element color in dev but not in heroku:

<mj-style inline="inline">
  .bblue { color: blue }
</mj-style>

Gemfile.lock -> mjml-rails (2.4.3)
package.json -> "dependencies" {"mjml": "^3.0.0"}

Is there any known reason for this to fail?

Exit code in case of an error when calling the mjml binary

We ran into issues when calling out to the mjml binary. Our Docker environment sometimes kills the child process because of limited memory. In the case the process fails silently. We were able to debug this with a monkey patch adding the exit code to the ParseError.

Are you interested in a pull request adding this debug information?

Can't seem to handle files named .html.erb

I'm experimenting with MJML, incorporating it into existing emails we have, just converting a couple partials to MJML so I don't have to convert everything to MJML at once. Some of our email view files end in .erb, and when I call format.mjml, it renders without any issue. However, some email views end in .html.erb, and calling format.mjml results in a ActionView::Template::Error: Missing template error. Curious if there's a way to address this.

MJML syntax issues

I am getting these errors, even though the MJML docs don't mention any of these restrictions. These errors started showing up after I upgraded JS library 4.1.1 => 4.2.1

Mjml.setup do |config|
  config.template_language = :slim
  config.raise_render_exception = true
  config.mjml_binary_version_supported = "4.2.1" # I added this later to see if this fixes the issue, but it doesnt
end

Versions:

  • mjml-rails (4.2.4) [Gemfile]
  • "mjml": "4.2.1" [package.json]

Errors:

ActionView::Template::Error (Line 1 of /var/folders/xx/bbv6bbfn22v0fg3jmxs87kpw0000gn/T/in20181213-26350-zdhsbs.mjml (mj-hero) — mj-hero cannot be used inside mj-column, only inside: mj-attributes, mj-body, mj-wrapper)
Line 1 of /var/folders/xx/bbv6bbfn22v0fg3jmxs87kpw0000gn/T/in20181213-26350-zdhsbs.mjml (mj-hero) — Attribute width is illegal
Line 9 of /var/folders/xx/bbv6bbfn22v0fg3jmxs87kpw0000gn/T/in20181213-26350-zdhsbs.mjml (mj-spacer) — mj-spacer cannot be used inside mj-section, only inside: mj-attributes, mj-column, mj-hero
Line 13 of /var/folders/xx/bbv6bbfn22v0fg3jmxs87kpw0000gn/T/in20181213-26350-zdhsbs.mjml (mj-spacer) — mj-spacer cannot be used inside mj-section, only inside: mj-attributes, mj-column, mj-hero

mjml-rails (4.2.4)

Rails Devise mailer does not work with mjml

Devise for Rails does not work with mjml.
The problem is it always tries the load the MJML format even when specifying format.text only!

this is my ApplicationMailer

class ApplicationMailer < ActionMailer::Base
  helper  :application
  layout "mailer" # i have /app/views/layouts/mailer.m
  append_view_path Rails.root.join('app', 'views', 'mailers') # i moved my mailer views from application_mailer to mailers folder
  default :from => "brand® <[email protected]>"
end

This is DeviseMailer which is subclassed from the ApplicationMailer

class DeviserMailer < Devise::Mailer

  include Devise::Mailers::Helpers
  include Devise::Controllers::UrlHelpers
  layout "mailer"
  helper  :application

  def reset_password_instructions(record, token, opts={})
    @token = token
    devise_mail(record, :reset_password_instructions, opts)
    # this default mail method uses the mailer.mjml layout file
    # but overwriting this with format.text and a regular mail block like 
#mail(to: organisation.email, subject: subject) do |format|
  #    format.text
    #end
  end

end

So even with the format.text it will NOT load the mailer.text.erb layout file.
Instead it tries to load the mailer.html.mjml layout file.

This mailer mjml layout file contains:

  <mj-head>
    <mj-preview></mj-preview>
    <%= render partial: "mailers/partials/head" %>
  </mj-head>

And it then breaks with a cannot find partial mailers/partials/head

**Anyone have any idea how to resolve this? After moving to MJML our application is now out of password recovery because of this issue ** I have temporarily disabled password retrieval and hope this problem can be resolved. I cannot pinpoint the exact issue.

Variables with html tags inside *.mjml.erb displayed as raw text in email body

Do you want to request a feature or report a bug?
Bug

Issue description
We recently upgraded our package mjml-rails from 2.4.1 to 4.2.4 and mjml npm dependency from 3.3.3 to 4.2.0. We have a function doSomeProcessingWithText which accepts name variable, the variable is supposed to be bold so its wrapped with <strong></strong> attribute. With the latest upgrade the output email body doesn't parse tag as html rather it displays it as raw text.

Code snippet

<mj-text>
        <%= doSomeProcessingWithText('%{name} Liked your post') % { name: "<strong>#{@person.name}</strong>" }%>
</mj-text>

What is the current behavior?
<strong>JOHN</strong> Liked your post

What is the expected behavior?
JOHN Liked your post ( notice that JOHN is bold here )

SSL ERROR

This is the output when trying to install the gem.

$> bundle install Fetching https://github.com/sighmon/mjml-rails.git Fetching source index from https://rubygems.org/ Could not verify the SSL certificate for https://rubygems.org/. There is a chance you are experiencing a man-in-the-middle attack, but most likely your system doesn't have the CA certificates needed for verification. For information about OpenSSL certificates, see bit.ly/ruby-ssl. To connect without using SSL, edit your Gemfile sources and change 'https' to 'http'.

any leads about why this is happening? Thank you!

followed all steps for heroku but still getting missing binary message.

I followed the Heroku instructions by adding the node buildpack and package.json but I'm still seeing a message about an unfound binary.

My package.json reads:

{
"name": "appname",
"version": "1.0.0",
"dependencies": {
"mjml": "^4.0.0"
}
}

and there is no error thrown in the node build, but I still see:

"Couldn't find the MJML binary.. have you run $ npm install mjml? "

when initialising the rails app. I have yet to write any mjml so I don't know whether or not the pacakge is working, and I don't have a lot of experience with npm. Do you have an advice for me?

`Mjml::Parser#render` suppresses errors in case of Tempfile exceptions

In case of an exception in Tempfile.open the in_tmp_file in the current ensure block is nil, which lets to another exception when calling unlink. This shadows the original exception.

We encountered this issue in context of a Docker environment and fixed this via a monkey patch.

Are you interested in a pull request, ideally with a test case?

Fix tests

  • Fix tests now that body appears to have a style attached by default

No plain text version

Normally with Rails, I can drop in both a html.erb and a text.erb template and it compiles into a multipart email with an HTML version and a plain text version.

I can't figure out how to include my plain text version in my emails with this. Is it not supported or am I missing a connection piece?

Changing binary version does not work

Changing the binary version in a Rails initializer like...

Mjml.setup do |config|
  config.mjml_binary_version_supported = '4.2.'
end

...does not work. The initializer is loaded too late. Mjml::BIN is computed earlier. I was able to override it by doing the following in my initializer after setup:

Mjml::BIN = Mjml.discover_mjml_bin

However, the error messages remain when loading the app.

Nothing renders from within Rails

Whatever I do MJML-s won't render from Rails.

Ruby 2.3.0
Rails 4.2.4
MJML 3.3.0
mjml-rails 2.4.1

My mailer:

# app/mailers/message_mailer.rb
class MessageMailer < ActionMailer::Base
  def new_message
    @message = message
    mail(to: '[email protected]', subject: 'test') do |format|
      format.mjml
    end
  end
end

My view:

# app/views/message_mailer/new_message.mjml
<mjml>
  <mj-body>
    <mj-container>
      <mj-section>
        <mj-column>
        </mj-column>
      </mj-section>
    </mj-container>
  </mj-body>
</mjml>

In the console I go:

2.3.0 :001 > MessageMailer.new_message.deliver_now
I, [2017-03-28T09:37:52.228263 #671]  INFO -- :   Rendered message_mailer/new_message.mjml (1090.0ms)
D, [2017-03-28T09:37:52.389439 #671] DEBUG -- :
MessageMailer#new_message: processed outbound mail in 1261.5ms
I, [2017-03-28T09:37:52.397568 #671]  INFO -- :
Sent mail to [email protected] (7.4ms)
D, [2017-03-28T09:37:52.398016 #671] DEBUG -- : Date: Tue, 28 Mar 2017 09:37:52 -0400
To: [email protected]
Message-ID: <[email protected]>
Subject: test
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit


 => #<Mail::Message:58501260, Multipart: false, Headers: <Date: Tue, 28 Mar 2017 09:37:52 -0400>, <To: [email protected]>, <Message-ID: <[email protected]>>, <Subject: test>, <Mime-Version: 1.0>, <Content-Type: text/html>, <Content-Transfer-Encoding: 7bit>>

As you can see the HTML output doesn't appear.

If from the command line I do:

mjml -s app/views/message_mailer/new_message.mjml
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
[ snip ]

It renders the HTML without problems.

I'm out of ideas.

image_tag helper in MJML template

Hi,
I'd like to use a standard rails image_tag in my template, but MJML seems to ignore it, am I doing it wrong ?

<mjml>
  <mj-body>
    <mj-section >
      <mj-column>
        <%= image_tag('logo_colored_horiz.png', width: "120" ) %>
      </mj-column>
    </mj-section>
  </mj-body>
</mjml>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.