1

I might be experience complications as I am using letter_opener to check the output in development.

At any rate I have the following code being called when I run an email:

    add_attachment('mobile.css')
    mail(to: @user.email, subject: @alert.title do |format|
      format.html {
        Premailer.new(
          "#{render}",
          :with_html_string => true,
          :css => [Rails.root.join("app/assets/mail/basestyles.css"),Rails.root.join("app/assets/mail/" +         Site.current.name + ".css")],
          :preserve_styles => true
        ).to_inline_css
       }
    format.text {  
      Premailer.new(
        "#{render}",
        :with_html_string => true,
        :css => [Rails.root.join("app/assets/mail/basestyles.css"),Rails.root.join("app/assets/mail/" +         Site.current.name + ".css")],
        :preserve_styles => true
      ).to_plain_text
    }
  end
  )

Now this does not raise any errors, but when I run it what I get out is the html file with none of the styles set inline.

The view is rendered correctly, when I run premailer to generate inline styles by itself it works fine, its when I use activemailer I run into problems.

I've looked at How to Integrate 'premailer' with Rails but it does not work for me.

Community
  • 1
  • 1
user254694
  • 1,461
  • 2
  • 23
  • 46

1 Answers1

0

Wow, it's a syntax error! I would not have been surprised at that except it never gave me any error messages, so if the future anyone is sitting with a similar situation don't expect your syntax errors have been caught. The do |format| should be moved out of the method and the render should have the actual view to render, so

add_attachment('mobile.css')
mail(to: @user.email, subject: @alert.title) do |format|
  format.html {
    Premailer.new(
      "#{render 'path to view to render'}",
      :with_html_string => true,
      :css => [Rails.root.join("app/assets/mail/basestyles.css"),Rails.root.join("app/assets/mail/" +         Site.current.name + ".css")],
      :preserve_styles => true
    ).to_inline_css
   }

end

user254694
  • 1,461
  • 2
  • 23
  • 46