aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-05-06 10:39:00 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-05-06 10:39:00 +0000
commit483931ea615d49a31d6de225f273e0945ad6cbb3 (patch)
treedb2c3f955ca855a78582d32ef7cd4b188a4fee95
parentc971c248394c7d603d336e31457c51ca33edf67d (diff)
downloadrails-483931ea615d49a31d6de225f273e0945ad6cbb3.tar.gz
rails-483931ea615d49a31d6de225f273e0945ad6cbb3.tar.bz2
rails-483931ea615d49a31d6de225f273e0945ad6cbb3.zip
Fixed the TMail#body method to look at the content-transfer-encoding header and unquote the body according to the rules it specifies #1265 [Jamis Buck] Added unquoting even if the iconv lib can't be loaded--in that case, only the charset conversion is skipped #1265 [Jamis Buck]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1290 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--actionmailer/CHANGELOG4
-rw-r--r--actionmailer/lib/action_mailer/vendor/tmail/quoting.rb170
-rwxr-xr-xactionmailer/test/mail_service_test.rb62
3 files changed, 150 insertions, 86 deletions
diff --git a/actionmailer/CHANGELOG b/actionmailer/CHANGELOG
index 25c980803b..3d033705dc 100644
--- a/actionmailer/CHANGELOG
+++ b/actionmailer/CHANGELOG
@@ -1,5 +1,9 @@
*SVN*
+* Fixed the TMail#body method to look at the content-transfer-encoding header and unquote the body according to the rules it specifies #1265 [Jamis Buck]
+
+* Added unquoting even if the iconv lib can't be loaded--in that case, only the charset conversion is skipped #1265 [Jamis Buck]
+
* Added automatic decoding of base64 bodies #1214 [Jamis Buck]
* Added that delivery errors are caught in a way so the mail is still returned whether the delivery was successful or not
diff --git a/actionmailer/lib/action_mailer/vendor/tmail/quoting.rb b/actionmailer/lib/action_mailer/vendor/tmail/quoting.rb
index 1ff00d9299..a31638100b 100644
--- a/actionmailer/lib/action_mailer/vendor/tmail/quoting.rb
+++ b/actionmailer/lib/action_mailer/vendor/tmail/quoting.rb
@@ -1,101 +1,109 @@
-begin
- require 'iconv'
- require 'base64'
+require 'base64'
- module TMail
- class Mail
- def subject(to_charset = 'utf-8')
- Unquoter.unquote_and_convert_to(quoted_subject, to_charset)
- end
-
- def unquoted_body(to_charset = 'utf-8')
- from_charset = header['content-type']['charset'] rescue 'us-ascii'
- Unquoter.unquote_and_convert_to(quoted_body, to_charset, from_charset)
- end
+module TMail
+ class Mail
+ def subject(to_charset = 'utf-8')
+ Unquoter.unquote_and_convert_to(quoted_subject, to_charset)
+ end
- def body(to_charset = 'utf-8', &block)
- attachment_presenter = block || Proc.new { |file_name| "Attachment: #{file_name}\n" }
-
- if multipart?
- parts.collect { |part|
- part.header["content-type"].main_type == "text" ?
- part.unquoted_body(to_charset) :
- attachment_presenter.call(part.header["content-type"].params["name"])
- }.join
+ def unquoted_body(to_charset = 'utf-8')
+ from_charset = header['content-type']['charset'] rescue 'us-ascii'
+ case (content_transfer_encoding || "7bit").downcase
+ when "quoted-printable"
+ Unquoter.unquote_quoted_printable_and_convert_to(quoted_body,
+ to_charset, from_charset)
+ when "base64"
+ Unquoter.unquote_base64_and_convert_to(quoted_body, to_charset,
+ from_charset)
+ when "7bit", "8bit"
+ Unquoter.convert_to(quoted_body, to_charset, from_charset)
+ when "binary"
+ quoted_body
else
- unquoted_body(to_charset)
- end
+ quoted_body
end
end
- class Unquoter
- class << self
- def unquote_and_convert_to(text, to_charset, from_charset = "iso-8859-1")
- return "" if text.nil?
- if text =~ /^=\?(.*?)\?(.)\?(.*)\?=$/
- from_charset = $1
- quoting_method = $2
- text = $3
- case quoting_method.upcase
- when "Q" then
- unquote_quoted_printable_and_convert_to(text, from_charset, to_charset)
- when "B" then
- unquote_base64_and_convert_to(text, from_charset, to_charset)
- else
- raise "unknown quoting method #{quoting_method.inspect}"
- end
- else
- unquote_quoted_printable_and_convert_to(text, from_charset, to_charset)
- end
- end
-
- def unquote_quoted_printable_and_convert_to(text, from, to)
- text ? Iconv.iconv(to, from || "ISO-8859-1", text.gsub(/_/," ").unpack("M*").first).first : ""
- end
-
- def unquote_base64_and_convert_to(text, from, to)
- text ? Iconv.iconv(to, from || "ISO-8859-1", Base64.decode64(text)).first : ""
- end
+ def body(to_charset = 'utf-8', &block)
+ attachment_presenter = block || Proc.new { |file_name| "Attachment: #{file_name}\n" }
+
+ if multipart?
+ parts.collect { |part|
+ part.header["content-type"].main_type == "text" ?
+ part.unquoted_body(to_charset) :
+ attachment_presenter.call(part.header["content-type"].params["name"])
+ }.join
+ else
+ unquoted_body(to_charset)
end
end
end
- if __FILE__ == $0
- require 'test/unit'
-
- class TC_Unquoter < Test::Unit::TestCase
- def test_unquote_quoted_printable
- a ="=?ISO-8859-1?Q?[166417]_Bekr=E6ftelse_fra_Rejsefeber?="
- b = TMail::Unquoter.unquote_and_convert_to(a, 'utf-8')
- assert_equal "[166417] Bekr\303\246ftelse fra Rejsefeber", b
+ class Unquoter
+ class << self
+ def unquote_and_convert_to(text, to_charset, from_charset = "iso-8859-1")
+ return "" if text.nil?
+ if text =~ /^=\?(.*?)\?(.)\?(.*)\?=$/
+ from_charset = $1
+ quoting_method = $2
+ text = $3
+ case quoting_method.upcase
+ when "Q" then
+ unquote_quoted_printable_and_convert_to(text, to_charset, from_charset)
+ when "B" then
+ unquote_base64_and_convert_to(text, to_charset, from_charset)
+ else
+ raise "unknown quoting method #{quoting_method.inspect}"
+ end
+ else
+ convert_to(text, to_charset, from_charset)
+ end
end
- def test_unquote_base64
- a ="=?ISO-8859-1?B?WzE2NjQxN10gQmVrcuZmdGVsc2UgZnJhIFJlanNlZmViZXI=?="
- b = TMail::Unquoter.unquote_and_convert_to(a, 'utf-8')
- assert_equal "[166417] Bekr\303\246ftelse fra Rejsefeber", b
+ def unquote_quoted_printable_and_convert_to(text, to, from)
+ convert_to(text.gsub(/_/," ").unpack("M*").first, to, from)
end
- def test_unquote_without_charset
- a ="[166417]_Bekr=E6ftelse_fra_Rejsefeber"
- b = TMail::Unquoter.unquote_and_convert_to(a, 'utf-8')
- assert_equal "[166417] Bekr\303\246ftelse fra Rejsefeber", b
+ def unquote_base64_and_convert_to(text, to, from)
+ convert_to(Base64.decode64(text), to, from)
+ end
+
+ begin
+ require 'iconv'
+ def convert_to(text, to, from)
+ text ? Iconv.iconv(to, from || "ISO-8859-1", text).first : ""
+ end
+ rescue LoadError
+ # Not providing quoting support
+ def convert_to(text, to, from)
+ warn "Action Mailer: iconv not loaded; ignoring conversion from #{from} to #{to} (#{__FILE__}:#{__LINE__})"
+ text
+ end
end
end
end
-rescue LoadError => e
- # Not providing quoting support
- module TMail
- class Mail
- def subject
- warn "Action Mailer: iconv couldn't be required, so the charset conversion is skipped"
- quoted_subject
- end
-
- def body
- warn "Action Mailer: iconv couldn't be required, so the charset conversion is skipped"
- quoted_body
- end
+end
+
+if __FILE__ == $0
+ require 'test/unit'
+
+ class TC_Unquoter < Test::Unit::TestCase
+ def test_unquote_quoted_printable
+ a ="=?ISO-8859-1?Q?[166417]_Bekr=E6ftelse_fra_Rejsefeber?="
+ b = TMail::Unquoter.unquote_and_convert_to(a, 'utf-8')
+ assert_equal "[166417] Bekr\303\246ftelse fra Rejsefeber", b
+ end
+
+ def test_unquote_base64
+ a ="=?ISO-8859-1?B?WzE2NjQxN10gQmVrcuZmdGVsc2UgZnJhIFJlanNlZmViZXI=?="
+ b = TMail::Unquoter.unquote_and_convert_to(a, 'utf-8')
+ assert_equal "[166417] Bekr\303\246ftelse fra Rejsefeber", b
+ end
+
+ def test_unquote_without_charset
+ a ="[166417]_Bekr=E6ftelse_fra_Rejsefeber"
+ b = TMail::Unquoter.unquote_and_convert_to(a, 'utf-8')
+ assert_equal "[166417]_Bekr=E6ftelse_fra_Rejsefeber", b
end
end
end
diff --git a/actionmailer/test/mail_service_test.rb b/actionmailer/test/mail_service_test.rb
index 04fa6bde1f..4863c9b4f4 100755
--- a/actionmailer/test/mail_service_test.rb
+++ b/actionmailer/test/mail_service_test.rb
@@ -240,20 +240,72 @@ class ActionMailerTest < Test::Unit::TestCase
assert_equal 1, ActionMailer::Base.deliveries.size
end
- def test_unquote_subject
+ def test_unquote_quoted_printable_subject
msg = <<EOF
From: me@example.com
Subject: =?utf-8?Q?testing_testing_=D6=A4?=
Content-Type: text/plain; charset=iso-8859-1
-This_is_a_test
-2 + 2 =3D 4
+The body
EOF
mail = TMail::Mail.parse(msg)
assert_equal "testing testing \326\244", mail.subject
assert_equal "=?utf-8?Q?testing_testing_=D6=A4?=", mail.quoted_subject
- assert_equal "This is a test\n2 + 2 = 4\n", mail.body
- assert_equal "This_is_a_test\n2 + 2 =3D 4\n", mail.quoted_body
+ end
+
+ def test_unquote_7bit_subject
+ msg = <<EOF
+From: me@example.com
+Subject: this == working?
+Content-Type: text/plain; charset=iso-8859-1
+
+The body
+EOF
+ mail = TMail::Mail.parse(msg)
+ assert_equal "this == working?", mail.subject
+ assert_equal "this == working?", mail.quoted_subject
+ end
+
+ def test_unquote_7bit_body
+ msg = <<EOF
+From: me@example.com
+Subject: subject
+Content-Type: text/plain; charset=iso-8859-1
+Content-Transfer-Encoding: 7bit
+
+The=3Dbody
+EOF
+ mail = TMail::Mail.parse(msg)
+ assert_equal "The=3Dbody", mail.body.strip
+ assert_equal "The=3Dbody", mail.quoted_body.strip
+ end
+
+ def test_unquote_quoted_printable_body
+ msg = <<EOF
+From: me@example.com
+Subject: subject
+Content-Type: text/plain; charset=iso-8859-1
+Content-Transfer-Encoding: quoted-printable
+
+The=3Dbody
+EOF
+ mail = TMail::Mail.parse(msg)
+ assert_equal "The=body", mail.body.strip
+ assert_equal "The=3Dbody", mail.quoted_body.strip
+ end
+
+ def test_unquote_base64_body
+ msg = <<EOF
+From: me@example.com
+Subject: subject
+Content-Type: text/plain; charset=iso-8859-1
+Content-Transfer-Encoding: base64
+
+VGhlIGJvZHk=
+EOF
+ mail = TMail::Mail.parse(msg)
+ assert_equal "The body", mail.body.strip
+ assert_equal "VGhlIGJvZHk=", mail.quoted_body.strip
end
def test_extended_headers