try to fix badly encoded emails

This commit is contained in:
Régis Hanol 2016-06-26 13:27:34 +02:00
parent ccf9b70671
commit 589bae5c03
1 changed files with 8 additions and 6 deletions

View File

@ -32,7 +32,7 @@ module Email
def initialize(mail_string)
raise EmptyEmailError if mail_string.blank?
@staged_users_created = 0
@raw_email = mail_string
@raw_email = try_to_encode(mail_string, "UTF-8") || try_to_encode(mail_string, "ISO-8859-1") || mail_string
@mail = Mail.new(@raw_email)
@message_id = @mail.message_id.presence || Digest::MD5.hexdigest(mail_string)
end
@ -236,14 +236,16 @@ module Email
return nil if string.blank?
# 1) use the charset provided
if mail_part.charset.present?
fixed = try_to_encode(string, mail_part.charset)
# common encodings
encodings = ["UTF-8", "ISO-8859-1"]
encodings.unshift(mail_part.charset) if mail_part.charset.present?
encodings.uniq.each do |encoding|
fixed = try_to_encode(string, encoding)
return fixed if fixed.present?
end
# 2) try most used encodings
try_to_encode(string, "UTF-8") || try_to_encode(string, "ISO-8859-1")
nil
end
def try_to_encode(string, encoding)