mirror of
https://github.com/discourse/discourse.git
synced 2025-04-01 01:10:39 +08:00
try to fix badly encoded emails
This commit is contained in:
parent
ccf9b70671
commit
589bae5c03
@ -32,7 +32,7 @@ module Email
|
|||||||
def initialize(mail_string)
|
def initialize(mail_string)
|
||||||
raise EmptyEmailError if mail_string.blank?
|
raise EmptyEmailError if mail_string.blank?
|
||||||
@staged_users_created = 0
|
@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)
|
@mail = Mail.new(@raw_email)
|
||||||
@message_id = @mail.message_id.presence || Digest::MD5.hexdigest(mail_string)
|
@message_id = @mail.message_id.presence || Digest::MD5.hexdigest(mail_string)
|
||||||
end
|
end
|
||||||
@ -236,14 +236,16 @@ module Email
|
|||||||
|
|
||||||
return nil if string.blank?
|
return nil if string.blank?
|
||||||
|
|
||||||
# 1) use the charset provided
|
# common encodings
|
||||||
if mail_part.charset.present?
|
encodings = ["UTF-8", "ISO-8859-1"]
|
||||||
fixed = try_to_encode(string, mail_part.charset)
|
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?
|
return fixed if fixed.present?
|
||||||
end
|
end
|
||||||
|
|
||||||
# 2) try most used encodings
|
nil
|
||||||
try_to_encode(string, "UTF-8") || try_to_encode(string, "ISO-8859-1")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def try_to_encode(string, encoding)
|
def try_to_encode(string, encoding)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user