diff --git a/app/models/channel/email_parser.rb b/app/models/channel/email_parser.rb index dde40eeb7..0ab4727cd 100644 --- a/app/models/channel/email_parser.rb +++ b/app/models/channel/email_parser.rb @@ -528,14 +528,15 @@ process unprocessable_mails (tmp/unprocessable_mail/*.eml) again # https://github.com/zammad/zammad/issues/2922 def force_parts_encoding_if_needed(mail) - mail.parts.each { |elem| force_single_part_encoding_if_needed(elem) } + # enforce encoding on both multipart parts and main body + ([mail] + mail.parts).each { |elem| force_single_part_encoding_if_needed(elem) } end # https://github.com/zammad/zammad/issues/2922 def force_single_part_encoding_if_needed(part) - return if part.charset != 'iso-2022-jp' + return if part.charset&.downcase != 'iso-2022-jp' - part.body = part.body.encoded.unpack1('M').force_encoding('ISO-2022-JP').encode('UTF-8') + part.body = force_japanese_encoding part.body.encoded.unpack1('M') end ISO2022JP_REGEXP = /=\?ISO-2022-JP\?B\?(.+?)\?=/.freeze @@ -543,7 +544,7 @@ process unprocessable_mails (tmp/unprocessable_mail/*.eml) again # https://github.com/zammad/zammad/issues/3115 def header_field_unpack_japanese(field) field.value.gsub ISO2022JP_REGEXP do - Base64.decode64($1).force_encoding('SJIS').encode('UTF-8') + force_japanese_encoding Base64.decode64($1) end end @@ -934,6 +935,16 @@ process unprocessable_mails (tmp/unprocessable_mail/*.eml) again "" end + + # https://github.com/zammad/zammad/issues/3096 + # 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 + def force_japanese_encoding(input) + input.force_encoding('ISO-2022-JP').encode('UTF-8') + rescue + input.force_encoding('SJIS').encode('UTF-8') + end end module Mail diff --git a/test/data/mail/mail098.box b/test/data/mail/mail098.box new file mode 100644 index 000000000..329a220e3 --- /dev/null +++ b/test/data/mail/mail098.box @@ -0,0 +1,18 @@ +Return-Path: +Delivered-To: delivered@example.org +Received: from localhost (unknown [127.0.0.1]) + by mail.example.org (Postfix) with ESMTP id 31337; + Mon, 21 Dec 2020 11:03:03 +0900 (JST) +MIME-Version: 1.0 +Content-Type: text/plain; charset=ISO-2022-JP +Content-Transfer-Encoding: 7bit +From: "=?ISO-2022-JP?B?GyRCO3ZMMzZJGyhC==?=" + +Reply-To: info@example.org +To: noreply@example.org +Cc: +Subject: =?ISO-2022-JP?B?GyRCMGxNdyFXJHIlIiVDJVckNyReJDkbKEI=?= +Message-Id: <20201221110303> +Date: Mon, 21 Dec 2020 11:33:03 +0900 (JST) + +$B%+%F%4%j!'A4%a%s%P!<(B diff --git a/test/data/mail/mail098.yml b/test/data/mail/mail098.yml new file mode 100644 index 000000000..633dbaa02 --- /dev/null +++ b/test/data/mail/mail098.yml @@ -0,0 +1,13 @@ +--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess +from: '"事務局" ' +from_email: noreply@example.org +from_display_name: 事務局 +to: noreply@example.org +cc: '' +subject: 一覧」をアップします +body: 'カテゴリ:全メンバー + + ' +content_type: text/plain +reply-to: info@example.org +attachments: []