From 66c0c4f5139e71f357be5d8cd1dca3cc2857622d Mon Sep 17 00:00:00 2001 From: Mantas Date: Wed, 21 Oct 2020 19:28:06 +0300 Subject: [PATCH] Fixes #3115 - Zammad can't import specific ISO-2022-JP mails --- app/models/channel/email_parser.rb | 18 ++++++++++++++++-- test/data/mail/mail091.yml | 27 +++++++++++++++++++++++++++ test/data/mail/mail093.box | 28 ++++++++++++++++++++++++++++ test/data/mail/mail093.yml | 18 ++++++++++++++++++ 4 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 test/data/mail/mail091.yml create mode 100644 test/data/mail/mail093.box create mode 100644 test/data/mail/mail093.yml diff --git a/app/models/channel/email_parser.rb b/app/models/channel/email_parser.rb index 2cc8b07af..0a27bbc52 100644 --- a/app/models/channel/email_parser.rb +++ b/app/models/channel/email_parser.rb @@ -512,13 +512,27 @@ process unprocessable_mails (tmp/unprocessable_mail/*.eml) again def force_single_part_encoding_if_needed(part) return if part.charset != 'iso-2022-jp' - part.body = part.body.encoded.unpack1('M').tr('_', ' ').force_encoding('ISO-2022-JP').encode('UTF-8') + part.body = part.body.encoded.unpack1('M').force_encoding('ISO-2022-JP').encode('UTF-8') + end + + ISO2022JP_REGEXP = /=\?ISO-2022-JP\?B\?(.+?)\?=/.freeze + + # 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') + end end def message_header_hash(mail) imported_fields = mail.header.fields.map do |f| begin - value = f.to_utf8 + value = if f.value.match?(ISO2022JP_REGEXP) + header_field_unpack_japanese(f) + else + f.to_utf8 + end + if value.blank? value = f.decoded.to_utf8 end diff --git a/test/data/mail/mail091.yml b/test/data/mail/mail091.yml new file mode 100644 index 000000000..e499d5405 --- /dev/null +++ b/test/data/mail/mail091.yml @@ -0,0 +1,27 @@ +--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess +from: '"岡本和正" ' +from_email: mail@domain.jp +from_display_name: 岡本和正 +to: '"support@ourdomain.com" ' +subject: メルマガ解除 +body: "
このアドレスへのメルマガを解除してください。
" +content_type: text/html +attachments: +- !ruby/hash:ActiveSupport::HashWithIndifferentAccess + data: | + + + + + + +
+ このアドレスへのメルマガを解除してください。
+ + + filename: message.html + preferences: !ruby/hash:ActiveSupport::HashWithIndifferentAccess + content-alternative: true + original-format: true + Mime-Type: text/html + Charset: iso-2022-jp diff --git a/test/data/mail/mail093.box b/test/data/mail/mail093.box new file mode 100644 index 000000000..be3a76cf4 --- /dev/null +++ b/test/data/mail/mail093.box @@ -0,0 +1,28 @@ +Return-Path: +Received: by example.com (Postfix, from userid 0) + id 123; Wed, 31 Jul 2020 11:38:04 +0900 (JST) +To: =?ISO-2022-JP?B?iXeWa4z7IA==?= +Subject: c[ +MIME-Version: 1.0 +Content-Type: multipart/alternative; boundary="--799140365efbe012d87669.81669397" +Content-Transfer-Encoding: quoted-printable +From: =?ISO-2022-JP?B?g2OBW4OLIEVYQU1QTEU=?= +Message-Id: <123@example.org> +Date: Wed, 31 Jul 2020 11:38:04 +0900 (JST) + +----799140365efbe012d87669.81669397 +Content-Type: text/html; charset=iso-2022-jp +Content-Disposition: inline +Content-Transfer-Encoding: quoted-printable + + + + +=1B$B!!:#$J$i!"=1B(B=1B$B@hCe=1B(B100=1B$B;\@_8BDj=1B(B=1B$B$G=1B(B
=20 +=1B$B!!=1B(B=1B$BL5NA%P!<%8%g%s=1B(B=1B$B$rF3F~$$$?= +$@$1$^$9!#=1B(B + + +----799140365efbe012d87669.81669397-- + diff --git a/test/data/mail/mail093.yml b/test/data/mail/mail093.yml new file mode 100644 index 000000000..707b5d67b --- /dev/null +++ b/test/data/mail/mail093.yml @@ -0,0 +1,18 @@ +--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess +from: ツール EXAMPLE +from_email: bar@example.org +from_display_name: ツール EXAMPLE +to: 駅北口 +subject: ツール +body: 今なら、先着100施設限定で
無料バージョンを導入いただけます。 +content_type: text/html +attachments: +- !ruby/hash:ActiveSupport::HashWithIndifferentAccess + data: "\n\n\n 今なら、先着100施設限定
+ \n 無料バージョンを導入いただけます。\n\n" + filename: message.html + preferences: !ruby/hash:ActiveSupport::HashWithIndifferentAccess + content-alternative: true + original-format: true + Mime-Type: text/html + Charset: iso-2022-jp