gpt4 book ai didi

ruby-on-rails - Rails 在我的纯文本电子邮件中转义 HTML

转载 作者:数据小太阳 更新时间:2023-10-29 07:02:56 25 4
gpt4 key购买 nike

我正在使用 rails 3.2.5 ActionMailer发送纯文本邮件。鉴于我有这样的邮件 View :

message_from_user.text.erb :

Hi <%= @recipient.name %>,

You got the following message from <%= @sender.name %>:

<%= @message %>

@message"quotes & ampersands" ,那么纯文本邮件包含 &quot;quotes &amp; ampersands&quot; .所以看起来 rails 只是将其视为 HTML View 并转义任何 html 以防止跨站点脚本。然而,这是一封纯文本邮件。分机号是.text.erbActionMailer检测到这一点并将 MIME 设置为 text/plain .所以我从不想转义其中的任何 html。

我的应用程序中有很多邮件模板,它们都是纯文本。我会考虑修补所有这些以包括 <%=raw @message%><%= @message.html_safe %>糟糕的风格 - 不是很干

我尝试了各种变通办法,包括花钱修补 Erubis。它们似乎都不起作用。我正在寻找一些补丁或配置选项或任何禁用转义 html 的所有 .text.erb文件。

非常感谢任何帮助!

最佳答案

通过 Erubis 代码调试几个小时后,我发现了以下修复。您可以将其放入 config/initializers/fix_my_mails.rb。我已经用 rails 3.2.7 测试过了。它可能适用于其他版本。

module ActionView
class Template
module Handlers
class ERB
def call(template)
if template.source.encoding_aware?
# First, convert to BINARY, so in case the encoding is
# wrong, we can still find an encoding tag
# (<%# encoding %>) inside the String using a regular
# expression
template_source = template.source.dup.force_encoding("BINARY")

erb = template_source.gsub(ENCODING_TAG, '')
encoding = $2

erb.force_encoding valid_encoding(template.source.dup, encoding)

# Always make sure we return a String in the default_internal
erb.encode!
else
erb = template.source.dup
end

self.class.erb_implementation.new(
erb,
:trim => (self.class.erb_trim_mode == "-"),
:escape => template.identifier =~ /\.text/ # only escape HTML templates
).src
end
end
end
end
end

它只是在文件名中包含 .text 的每个 erb 文件中禁用 HTML 实体。

关于ruby-on-rails - Rails 在我的纯文本电子邮件中转义 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11756304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com