Skip to content

Commit fae72e5

Browse files
committed
Rescue from error when converting Mime to String
Rescue from Encoding::CompatibilityError when converting Mapi::Mime to a String. In some situations the parts can legitimately be different encodings, but cannot be concatenated given the encoding differences. In this situation, force the encoding to ASCII-8BIT rather than erroring.
1 parent fac10c8 commit fae72e5

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

lib/mapi/mime.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,17 @@ def to_s opts={}
106106
opts = {:boundary_counter => 0}.merge opts
107107
if multipart?
108108
boundary = Mime.make_boundary opts[:boundary_counter] += 1, self
109-
@body = [preamble, parts.map { |part| "\r\n" + part.to_s(opts) + "\r\n" }, "--\r\n" + epilogue].
110-
flatten.join("\r\n--" + boundary)
109+
begin
110+
@body = [preamble, parts.map { |part| "\r\n" + part.to_s(opts) + "\r\n" }, "--\r\n" + epilogue].
111+
flatten.join("\r\n--" + boundary)
112+
rescue Encoding::CompatibilityError => e
113+
if ''.respond_to?(:force_encoding)
114+
@body = [preamble, parts.map { |part| "\r\n" + part.to_s(opts).force_encoding('ASCII-8BIT') + "\r\n" }, "--\r\n" + epilogue].
115+
flatten.join("\r\n--" + boundary)
116+
else
117+
raise e
118+
end
119+
end
111120
content_type, attrs = Mime.split_header @headers['Content-Type'][0]
112121
attrs['boundary'] = boundary
113122
@headers['Content-Type'] = [([content_type] + attrs.map { |key, val| %{#{key}="#{val}"} }).join('; ')]

0 commit comments

Comments
 (0)