sighmon / mjml-rails

MJML + ERb/Haml/Slim view template
https://mjml.io
Other
296 stars 64 forks source link

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

Closed dyanagi closed 4 years ago

dyanagi commented 4 years ago

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: "app@example.com") do |format|
      format.html
    end

to

    mail(to: user.email, from: "app@example.com") 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:

https://github.com/dyanagi/example_mjml_rails/commit/b30ac7c2c540e5894380694eed8aa2a68051f390

Notes:

sighmon commented 4 years ago

@dyanagi Sorry for the delay in getting back to you. I've had a play with your repo and forked it with some other options for setting up the user mailer: https://github.com/sighmon/example_mjml_rails/commit/a22b184c0b2e0c2eaf7a87cf35a3ded82da2f011

How does that look to you?

dyanagi commented 4 years ago

@sighmon Thank you! I appreciate it. I understand the main cause of this issue.

The code below didn't compile MJML tags because mailer.html.erb took the priority to be the default layout for the UserMailer. ( mailer.html.mjml wasn't used. )

class ApplicationMailer < ActionMailer::Base
  default from: 'from@example.com'
  layout 'mailer'
end

class UserMailer < ApplicationMailer
  def user_signup_confirmation
    @greeting = "Hi"

    mail to: "to@example.org" do |format|
      format.text
      format.html # looks for "mailer.html.erb" and then "mailer.html.mjml"
    end
end

I removed mailer.html.erb and now MJML tags are compiled.

Could you take a look at this pull request to see if the changes look good? https://github.com/dyanagi/example_mjml_rails/pull/1

Also, maybe we could mention this behaviour in the documentation. https://github.com/sighmon/mjml-rails#using-email-layouts

Thanks!

sighmon commented 4 years ago

@dyanagi That looks great to me. Would you like to put in a PR with the changes to the documentation you'd like with a link to your example repo?

dyanagi commented 4 years ago

@sighmon Thanks! Sure, I'll work on it!

dyanagi commented 4 years ago

@sighmon Thanks for reviewing and merging it, Simon! I'm closing this issue.