Skip to content

Commit a9272b2

Browse files
committed
Fix another incompatible character encoding error
In fae72e5 we introduced a fallback for incompatible character encodings, but that only covered the case where Mapi::Mime#is_multipart? returns true. This is a slight variation for the other branch of the conditional. The error is being raised where we have a `@body` in a different encoding to the default string encoding. In this case, just always force the string to be the same encoding as `@body` so we don’t get an error. This passes the spec for the particular file we’re seeing problems with, but I’m not at all sure this is the right thing to do. Maybe we should always treat anything in mapi as ASCII, and then force to UTF-8 for display? [1] Fixes mysociety/alaveteli#5783 [1] aquasync#9 (comment)
1 parent 2d09aaa commit a9272b2

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

lib/mapi/mime.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def to_s opts={}
126126
@headers.each do |key, vals|
127127
vals.each { |val| str << "#{key}: #{val}\r\n" }
128128
end
129-
str << "\r\n" + @body
129+
str.force_encoding(@body.encoding) << "\r\n" + @body
130130
end
131131

132132
def self.split_header header

test/28101-outlook-attachment.msg

44 KB
Binary file not shown.

test/test_msg.rb

+9
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ def test_multipart_rendered_string_is_valid_encoding
4444
end
4545
end
4646

47+
def test_non_multipart_rendered_string_is_valid_encoding
48+
msg = Mapi::Msg.open "#{TEST_DIR}/28101-outlook-attachment.msg" do |msg|
49+
string_version = msg.to_mime.to_s
50+
if string_version.respond_to?(:valid_encoding?)
51+
assert_equal true, string_version.valid_encoding?
52+
end
53+
end
54+
end
55+
4756
def test_embedded_msg_renders_as_string
4857
msg = Mapi::Msg.open "#{TEST_DIR}/embedded.msg" do |msg|
4958
assert_match "message/rfc822", msg.to_mime.to_s

0 commit comments

Comments
 (0)