From a32251487174dffe420980fbc9d43bcf4472d2af Mon Sep 17 00:00:00 2001 From: Jamis Buck Date: Thu, 1 Sep 2005 14:45:10 +0000 Subject: [PATCH] Preserve underscores when unquoting message bodies #1930 git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2089 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionmailer/CHANGELOG | 2 ++ .../lib/action_mailer/vendor/tmail/quoting.rb | 11 ++++++----- actionmailer/test/mail_service_test.rb | 10 ++++++++++ actionmailer/test/tmail_test.rb | 17 +++++++++++++++++ 4 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 actionmailer/test/tmail_test.rb diff --git a/actionmailer/CHANGELOG b/actionmailer/CHANGELOG index 1fe104dcba..1d4118ebff 100644 --- a/actionmailer/CHANGELOG +++ b/actionmailer/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Preserve underscores when unquoting message bodies #1930 + * Encode multibyte characters correctly #1894 * Multipart messages specify a MIME-Version header automatically #2003 [John Long] diff --git a/actionmailer/lib/action_mailer/vendor/tmail/quoting.rb b/actionmailer/lib/action_mailer/vendor/tmail/quoting.rb index b593efafcd..36bf03f0a3 100644 --- a/actionmailer/lib/action_mailer/vendor/tmail/quoting.rb +++ b/actionmailer/lib/action_mailer/vendor/tmail/quoting.rb @@ -9,7 +9,7 @@ def unquoted_body(to_charset = 'utf-8') case (content_transfer_encoding || "7bit").downcase when "quoted-printable" Unquoter.unquote_quoted_printable_and_convert_to(quoted_body, - to_charset, from_charset) + to_charset, from_charset, true) when "base64" Unquoter.unquote_base64_and_convert_to(quoted_body, to_charset, from_charset) @@ -47,7 +47,7 @@ def body(to_charset = 'utf-8', &block) class Unquoter class << self - def unquote_and_convert_to(text, to_charset, from_charset = "iso-8859-1") + def unquote_and_convert_to(text, to_charset, from_charset = "iso-8859-1", preserve_underscores=false) return "" if text.nil? if text =~ /^=\?(.*?)\?(.)\?(.*)\?=$/ from_charset = $1 @@ -55,7 +55,7 @@ def unquote_and_convert_to(text, to_charset, from_charset = "iso-8859-1") text = $3 case quoting_method.upcase when "Q" then - unquote_quoted_printable_and_convert_to(text, to_charset, from_charset) + unquote_quoted_printable_and_convert_to(text, to_charset, from_charset, preserve_underscores) when "B" then unquote_base64_and_convert_to(text, to_charset, from_charset) else @@ -66,8 +66,9 @@ def unquote_and_convert_to(text, to_charset, from_charset = "iso-8859-1") end end - def unquote_quoted_printable_and_convert_to(text, to, from) - convert_to(text.gsub(/_/," ").unpack("M*").first, to, from) + def unquote_quoted_printable_and_convert_to(text, to, from, preserve_underscores=false) + text = text.gsub(/_/, " ") unless preserve_underscores + convert_to(text.unpack("M*").first, to, from) end def unquote_base64_and_convert_to(text, to, from) diff --git a/actionmailer/test/mail_service_test.rb b/actionmailer/test/mail_service_test.rb index 50eb1984b5..c181b14f9a 100755 --- a/actionmailer/test/mail_service_test.rb +++ b/actionmailer/test/mail_service_test.rb @@ -145,6 +145,11 @@ def html_mail(recipient) content_type "text/html" end + def html_mail_with_underscores(recipient) + subject "html mail with underscores" + body %{_Google} + end + def custom_template(recipient) recipients recipient subject "[Signed up] Welcome #{recipient}" @@ -631,6 +636,11 @@ def test_html_mail assert_equal "text/html", mail.content_type end + def test_html_mail_with_underscores + mail = TestMailer.create_html_mail_with_underscores(@recipient) + assert_equal %{_Google}, mail.body + end + def test_various_newlines mail = TestMailer.create_various_newlines(@recipient) assert_equal("line #1\nline #2\nline #3\nline #4\n\n" + diff --git a/actionmailer/test/tmail_test.rb b/actionmailer/test/tmail_test.rb new file mode 100644 index 0000000000..3930c7d39a --- /dev/null +++ b/actionmailer/test/tmail_test.rb @@ -0,0 +1,17 @@ +$:.unshift(File.dirname(__FILE__) + "/../lib/") +$:.unshift File.dirname(__FILE__) + "/fixtures/helpers" + +require 'test/unit' +require 'action_mailer' + +class TMailMailTest < Test::Unit::TestCase + def test_body + m = TMail::Mail.new + expected = 'something_with_underscores' + m.encoding = 'quoted-printable' + quoted_body = [expected].pack('*M') + m.body = quoted_body + assert_equal "something_with_underscores=\n", m.quoted_body + assert_equal expected, m.body + end +end -- GitLab