Skip to content

Commit 05475ed

Browse files
committed
Handle incomptible encodings in email headers
1 parent e054847 commit 05475ed

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/mail/encodings.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,15 @@ def Encodings.value_decode(str)
134134
when *Q_VALUES then q_value_decode(string)
135135
end
136136
end
137-
end.join("")
137+
end
138+
139+
if lines.each_slice(2).all? { |x, y| Encoding.compatible?(x, y) }
140+
lines.join("")
141+
else
142+
lines.map do |line|
143+
line.encode("UTF-8", invalid: :replace, undef: :replace, replace: "�")
144+
end.join("")
145+
end
138146
end
139147

140148
# Takes an encoded string of the format =?<encoding>?[QB]?<string>?=

spec/mail/encoding_spec.rb

+7
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,13 @@
189189
expect(m.subject).to eq "Hello #{replace} World"
190190
end
191191

192+
it "handle incompatible encodings by force encoding to UTF-8" do
193+
m = Mail.new
194+
subject = "\xEC =?utf-8?Q?=F0=9F=98=80?=".dup.force_encoding("ASCII-8BIT")
195+
m['Subject'] = Mail::SubjectField.new(subject)
196+
expect(m.subject).to eq "� 😀"
197+
end
198+
192199
it "should replace characters of unknown and invalid encoding" do
193200
m = Mail.new
194201
m['Subject'] = Mail::SubjectField.new("Hello=?UNKNOWN?B?4g==?=")

0 commit comments

Comments
 (0)