3

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

4

1 回答 1

3

在过去的几天里,我花了很多时间在这上面,似乎没有很好的解决方案。可以显式呈现消息,然后通过 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
于 2012-09-25T12:15:58.567 回答