diff --git a/app/models/channel/email_parser.rb b/app/models/channel/email_parser.rb index 0ab4727cd..58e4d331f 100644 --- a/app/models/channel/email_parser.rb +++ b/app/models/channel/email_parser.rb @@ -940,10 +940,21 @@ process unprocessable_mails (tmp/unprocessable_mail/*.eml) again # specific email needs to be forced to ISO-2022-JP # but that breaks other emails that can be forced to SJIS only # thus force to ISO-2022-JP but fallback to SJIS + # + # https://github.com/zammad/zammad/issues/3368 + # some characters are not included in the official ISO-2022-JP + # ISO-2022-JP-KDDI superset provides support for more characters def force_japanese_encoding(input) - input.force_encoding('ISO-2022-JP').encode('UTF-8') + %w[ISO-2022-JP ISO-2022-JP-KDDI SJIS] + .lazy + .map { |encoding| try_encoding(input, encoding) } + .detect(&:present?) + end + + def try_encoding(input, encoding) + input.force_encoding(encoding).encode('UTF-8') rescue - input.force_encoding('SJIS').encode('UTF-8') + nil end end diff --git a/test/data/mail/mail099.box b/test/data/mail/mail099.box new file mode 100644 index 000000000..77f783d39 --- /dev/null +++ b/test/data/mail/mail099.box @@ -0,0 +1,13 @@ +Content-Type: text/plain; charset=ISO-2022-JP +Content-Transfer-Encoding: 7bit +From: "example" + +Reply-To: info@example.org +Errors-To: return@example.org +To: noreply@example.org +Subject: example +Message-Id: +Date: Fri, 1 Jan 2021 10:00:00 +0900 (JST) + +$B!!!!!!!!!!-!#1ET#38)$N03$H!"(B + diff --git a/test/data/mail/mail099.yml b/test/data/mail/mail099.yml new file mode 100644 index 000000000..0ddd907ce --- /dev/null +++ b/test/data/mail/mail099.yml @@ -0,0 +1,10 @@ +--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess +from: example +from_email: noreply@example.org +from_display_name: example +to: noreply@example.org +subject: example +body: "     ①1都3県の鯵と、\n\n" +content_type: text/plain +reply-to: info@example.org +attachments: []