How to Integrate 'premailer' with Rails
Asked Answered
G

4

10

How does one integrate the 'premailer' gem with a Rails (3.0.7) project? I currently have in my mailer:

def welcome(user)
  @user = user

  mail to: user.email, subject: "Welcome"
end

But I can't figure out how to integrate the library. I need to call:

premailer = Premailer.new(html)
html = premailer.to_inline_css

However, I'm not sure how to access the contents of my email from a mailer action.

Goodygoody answered 25/5, 2011 at 15:36 Comment(0)
E
5

Try:

def premailer(message)
  message.text_part.body = Premailer.new(message.text_part.body.to_s, with_html_string: true).to_plain_text
  message.html_part.body = Premailer.new(message.html_part.body.to_s, with_html_string: true).to_inline_css

  return message
end

def welcome(user)
  @user = user

  message = mail to: user.email, subject: "Welcome"
end
Ertha answered 27/5, 2011 at 2:17 Comment(4)
Hi, this seems to take care of the mail body quite well.. Thanks.. . How can I use premailer to include the layout css inline as well along with mail body, as one complete mail..?Cordate
can you explain why this is working? It's not quite self-explaining, at least not for meMaggoty
I don't get it. When is this premailer method called? Doesn't seem to work with Rails 5.Gibrian
i think in the welcome method, he meant to do premailer(mail to: user.email, subject: "Welcome")Whittemore
A
12

Have a look at the simple premailer-rails gem I recently wrote. It uses Rails mailer hooks to do the conversion.

Appleby answered 31/5, 2011 at 17:14 Comment(0)
E
5

Try:

def premailer(message)
  message.text_part.body = Premailer.new(message.text_part.body.to_s, with_html_string: true).to_plain_text
  message.html_part.body = Premailer.new(message.html_part.body.to_s, with_html_string: true).to_inline_css

  return message
end

def welcome(user)
  @user = user

  message = mail to: user.email, subject: "Welcome"
end
Ertha answered 27/5, 2011 at 2:17 Comment(4)
Hi, this seems to take care of the mail body quite well.. Thanks.. . How can I use premailer to include the layout css inline as well along with mail body, as one complete mail..?Cordate
can you explain why this is working? It's not quite self-explaining, at least not for meMaggoty
I don't get it. When is this premailer method called? Doesn't seem to work with Rails 5.Gibrian
i think in the welcome method, he meant to do premailer(mail to: user.email, subject: "Welcome")Whittemore
H
5

For Rails 4 users you can: add the gems

gem 'premailer-rails'
gem 'nokogiri' (if you don't have it)

add this to your stylesheet (Haml):

%style{type:"text/css"}= Rails.application.assets.find_asset('email_stylesheet').to_s

for some reason it wasn't working with a normal stylesheet_link_tag

That's all I had to do. Hope this help!

Herlindaherm answered 15/8, 2014 at 23:28 Comment(0)
V
1

or

gem "actionmailer_inline_css"
Voguish answered 28/11, 2012 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.