如何将’premailer’与Rails集成

如何将’ premailer’gem与Rails(3.0.7)项目集成? 我目前在我的邮件中:

def welcome(user) @user = user mail to: user.email, subject: "Welcome" end 

但我无法弄清楚如何整合图书馆。 我需要打电话:

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

但是,我不确定如何从邮件程序操作访问我的电子邮件的内容。

尝试:

 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 

看看我最近写的简单的premailer-rails gem 。 它使用Rails邮件程序挂钩进行转换。

对于Rails 4用户,您可以:添加gem

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

将其添加到样式表(Haml):

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

由于某种原因,它无法使用正常的stylesheet_link_tag

这就是我必须做的一切。 希望这有帮助!

要么

 gem "actionmailer_inline_css"