diff --git a/Rakefile b/Rakefile index f09cb7d..f2e659f 100644 --- a/Rakefile +++ b/Rakefile @@ -4,7 +4,8 @@ require 'rake/testtask' require 'rbconfig' require 'fileutils' -spec = eval File.read('ruby-msg.gemspec') +gemspec_file = File.expand_path(File.dirname(__FILE__) + '/ruby-msg.gemspec') +spec = eval File.read(gemspec_file) task :default => [:test] diff --git a/lib/mapi/mime.rb b/lib/mapi/mime.rb index 7240489..c8b007a 100644 --- a/lib/mapi/mime.rb +++ b/lib/mapi/mime.rb @@ -106,8 +106,17 @@ def to_s opts={} opts = {:boundary_counter => 0}.merge opts if multipart? boundary = Mime.make_boundary opts[:boundary_counter] += 1, self - @body = [preamble, parts.map { |part| "\r\n" + part.to_s(opts) + "\r\n" }, "--\r\n" + epilogue]. - flatten.join("\r\n--" + boundary) + begin + @body = [preamble, parts.map { |part| "\r\n" + part.to_s(opts) + "\r\n" }, "--\r\n" + epilogue]. + flatten.join("\r\n--" + boundary) + rescue Encoding::CompatibilityError => e + if ''.respond_to?(:force_encoding) + @body = [preamble, parts.map { |part| "\r\n" + part.to_s(opts).force_encoding('ASCII-8BIT') + "\r\n" }, "--\r\n" + epilogue]. + flatten.join("\r\n--" + boundary) + else + raise e + end + end content_type, attrs = Mime.split_header @headers['Content-Type'][0] attrs['boundary'] = boundary @headers['Content-Type'] = [([content_type] + attrs.map { |key, val| %{#{key}="#{val}"} }).join('; ')] diff --git a/test/small-business-rates-relief.msg b/test/small-business-rates-relief.msg new file mode 100644 index 0000000..1de1130 Binary files /dev/null and b/test/small-business-rates-relief.msg differ diff --git a/test/test_msg.rb b/test/test_msg.rb index ed52f0e..c1741b6 100644 --- a/test/test_msg.rb +++ b/test/test_msg.rb @@ -35,6 +35,15 @@ def test_rendered_string_is_valid_encoding end end + def test_multipart_rendered_string_is_valid_encoding + msg = Mapi::Msg.open "#{TEST_DIR}/small-business-rates-relief.msg" do |msg| + string_version = msg.to_mime.to_s + if string_version.respond_to?(:valid_encoding?) + assert_equal true, string_version.valid_encoding? + end + end + end + def test_embedded_msg_renders_as_string msg = Mapi::Msg.open "#{TEST_DIR}/embedded.msg" do |msg| assert_match "message/rfc822", msg.to_mime.to_s