Browse Source

Improved error handling of emails with invalid byte sequence for utf8 encoding.

Martin Edenhofer 7 years ago
parent
commit
88e3d9e574
3 changed files with 43 additions and 0 deletions
  1. 1 0
      app/models/channel/email_parser.rb
  2. 16 0
      test/fixtures/mail60.box
  3. 26 0
      test/unit/email_process_test.rb

+ 1 - 0
app/models/channel/email_parser.rb

@@ -151,6 +151,7 @@ class Channel::EmailParser
       if data[:body].empty? && mail.text_part
         data[:body] = mail.text_part.body.decoded
         data[:body] = Encode.conv(mail.text_part.charset, data[:body])
+        data[:body] = data[:body].to_s.force_encoding('utf-8')
 
         if !data[:body].valid_encoding?
           data[:body] = data[:body].encode('utf-8', 'binary', invalid: :replace, undef: :replace, replace: '?')

+ 16 - 0
test/fixtures/mail60.box

@@ -0,0 +1,16 @@
+From: Martin Edenhofer <martin@example.com>
+Content-Type: multipart/alternative; boundary="Apple-Mail=_EB2F27C4-F4CD-40C9-82F1-D115D4FFA394"
+Subject: abc
+Date: Fri, 4 May 2012 14:01:03 +0200
+Message-Id: <BC182994-03FA-4DC5-8202-98CBFACA0887@example.com>
+To: metest@znuny.com
+Mime-Version: 1.0 (Apple Message framework v1257)
+
+--Apple-Mail=_EB2F27C4-F4CD-40C9-82F1-D115D4FFA394
+Content-Transfer-Encoding: base64
+Content-Type: text/plain;
+  charset=iso-gb2312
+
+SGVyZSBpdCBnb2VzIC0gw6TDtsO8IC0g5beu5Ye65Lq6SGVyZSBpdCBnb2VzIC0g5Pb8IC0gaGkgrQ==
+
+--Apple-Mail=_EB2F27C4-F4CD-40C9-82F1-D115D4FFA394--

+ 26 - 0
test/unit/email_process_test.rb

@@ -2449,6 +2449,32 @@ Some Text',
           ],
         },
       },
+      {
+        data: IO.binread('test/fixtures/mail60.box'),
+        success: true,
+        result: {
+          0 => {
+            priority: '2 normal',
+            title: 'abc',
+          },
+          1 => {
+            from: 'Martin Edenhofer <martin@example.com>',
+            sender: 'Customer',
+            type: 'email',
+            body: 'Here it goes - ?????? - ?????????Here it goes - ??? - hi ?',
+          },
+        },
+        verify: {
+          users: [
+            {
+              firstname: 'Martin',
+              lastname: 'Edenhofer',
+              fullname: 'Martin Edenhofer',
+              email: 'martin@example.com',
+            },
+          ],
+        },
+      },
     ]
     assert_process(files)
   end