【问题标题】:Integrate premailer gem with Rails 2.X application将 premailer gem 与 Rails 2.X 应用程序集成
【发布时间】:2012-06-25 14:33:05
【问题描述】:

我有一个 rails 2.3 应用程序,并希望将 premailer gem 集成到它。 我发现如何为 rails 3.X 应用程序做到这一点: How to Integrate 'premailer' with Rails 任何人都知道如何为 action mailer 2.3.10 做到这一点?

【问题讨论】:

    标签: ruby-on-rails ruby actionmailer ruby-on-rails-2 premailer


    【解决方案1】:

    过去几天我在这方面花了很多时间,但似乎没有很好的解决方案。可以显式呈现消息,然后通过 Premailer 传递结果,但如果与多部分电子邮件和 HTML 布局结合使用,并且模板使用 ASCII-8BIT 以外的其他编码,则会变得混乱。

    在没有多部分并假设为 ASCII-8BIT 编码模板的直接 HTML 电子邮件中,这对我有用:

    def some_email
        recipients   "Reciever <reciever@example.com>"
        from         "Sender <sender@example.com>"
        subject      "Hello"
        content_type "text/html"
    
        message = render_message("some_email", { }) # second argument is a hash of locals
        p.body = Premailer.new(message, with_html_string: true).to_inline_css
    end
    

    但是,如果模板使用 ASCII-8BIT 以外的其他编码进行编码,Premailer 会销毁所有非 ASCII 字符。 Premailer 存储库中合并了一个修复程序,但此后没有发布任何版本。使用最新版本并调用Premailer.new(message, with_html_string: true, input_encoding: "UTF-8").to_inline_css 或类似的应该可以工作。合并提交是https://github.com/alexdunae/premailer/commit/5f5cbb4ac181299a7e73d3eca11f3cf546585364

    对于多部分电子邮件,我还没有真正让 ActionMailer 在内部使用正确的内容类型来呈现模板。这导致通过模板文件名的隐式键入不起作用,因此布局被错误地应用于文本版本。一种解决方法是显式地为文本版本不使用布局,从而产生类似这样的结果(注意模板名称):

    def some_multipart_email
        recipients   "Reciever <reciever@example.com>"
        from         "Sender <sender@example.com>"
        subject      "Hello"
        content_type "text/html"
    
        part "text/html" do |p|
            message = render_message("some_email_html", { })
            p.body = Premailer.new(message, with_html_string: true).to_inline_css
        end
    
        part "text/plain" do |p|
            p.content_type = "text/plain"
            p.body = render(file: "some_email_text", body: { }, layout: false)
        end
    end
    

    【讨论】:

      猜你喜欢
      • 2011-09-01
      • 1970-01-01
      • 2016-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多