From 29c513193458c4294d73d50c3324b1e13a8c39cb Mon Sep 17 00:00:00 2001 From: Bob Maerten Date: Sat, 7 Sep 2013 20:58:47 +0200 Subject: [PATCH] Fixed text mail displaying html entities using method found on http://stackoverflow.com/questions/11756304/rails-escapes-html-in-my-plain-text-mails/11763211#11763211 --- config/initializers/fix_text_mails.rb | 33 +++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 config/initializers/fix_text_mails.rb diff --git a/config/initializers/fix_text_mails.rb b/config/initializers/fix_text_mails.rb new file mode 100644 index 00000000..eae6a10b --- /dev/null +++ b/config/initializers/fix_text_mails.rb @@ -0,0 +1,33 @@ +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