FIX: check we used the right encoding when parsing incoming emails

This commit is contained in:
Régis Hanol 2016-03-30 19:54:38 +02:00
parent 2ac38e9d05
commit 79329c8e85

View file

@ -162,12 +162,13 @@ module Email
return fixed if fixed.present?
end
# 2) default to UTF-8
try_to_encode(string, "UTF-8")
# 2) try most used encodings
try_to_encode(string, "UTF-8") || try_to_encode(string, "ISO-8859-1")
end
def try_to_encode(string, encoding)
string.encode("UTF-8", encoding)
encoded = string.encode("UTF-8", encoding)
encoded.present? && encoded.valid_encoding? ? encoded : nil
rescue Encoding::InvalidByteSequenceError,
Encoding::UndefinedConversionError,
Encoding::ConverterNotFoundError