From b3aa055c0eec6a361d0f1eac128d8385de3b0b01 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 26 Nov 2015 09:29:58 +0100 Subject: [PATCH] Check if in text/plain is content - if not, use text/html. --- app/models/channel/email_parser.rb | 3 ++- test/fixtures/mail34.box | 29 +++++++++++++++++++++++++++++ test/unit/email_parser_test.rb | 21 ++++++++++++++++++--- 3 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/mail34.box diff --git a/app/models/channel/email_parser.rb b/app/models/channel/email_parser.rb index 7c87953c5..fb3fe64aa 100644 --- a/app/models/channel/email_parser.rb +++ b/app/models/channel/email_parser.rb @@ -134,9 +134,10 @@ class Channel::EmailParser if !data[:body].valid_encoding? data[:body] = data[:body].encode('utf-8', 'binary', invalid: :replace, undef: :replace, replace: '?') end + end # html attachment/body may exists and will be converted to text - else + if !mail.text_part || !data[:body] || data[:body] == '' filename = '-no name-' if mail.html_part && mail.html_part.body filename = 'message.html' diff --git a/test/fixtures/mail34.box b/test/fixtures/mail34.box new file mode 100644 index 000000000..36b39af43 --- /dev/null +++ b/test/fixtures/mail34.box @@ -0,0 +1,29 @@ +From: "Bay" +Subject: strange email with empty text/plain +To: bay@example.com +Date: 11 Nov 2015 12:07:51 +0000 +Priority: normal +X-Priority: 3 (Normal) +Importance: normal +X-David-SYM: 0 +X-David-Flags: 0 +Message-ID: <7ca10659-214c-4bd0-8438-b935a01c7601@stange> +MIME-Version: 1.0 +Content-Type: multipart/alternative; boundary="----_=_NextPart_000_06409CFC.56433DA7" + +This message is in MIME format. Since your mail reader does not understand +this format, some or all of this message may not be legible. + +------_=_NextPart_000_06409CFC.56433DA7 +Content-Type: text/plain; charset=iso-8859-1 +Content-Transfer-Encoding: quoted-printable + + + +------_=_NextPart_000_06409CFC.56433DA7 +Content-Type: text/html; charset=utf-8 +Content-Transfer-Encoding: quoted-printable + +some html text + +------_=_NextPart_000_06409CFC.56433DA7 diff --git a/test/unit/email_parser_test.rb b/test/unit/email_parser_test.rb index 7b3c723cf..5f01bef5a 100644 --- a/test/unit/email_parser_test.rb +++ b/test/unit/email_parser_test.rb @@ -308,7 +308,7 @@ Hof # spam email { data: IO.read('test/fixtures/mail15.box'), - body_md5: 'd41d8cd98f00b204e9800998ecf8427e', + body_md5: '5872ddcdfdf6bfe40f36cd0408fca667', attachments: [ # :preferences=>{"Message-ID"=>"", "Content-Type"=>"application/octet-stream; name=\"\xBC\xA8\xD0\xA7\xB9\xDC\xC0\xED,\xBE\xBF\xBE\xB9\xCB\xAD\xB4\xED\xC1\xCB.xls\"", "Mime-Type"=>"application/octet-stream", "Charset"=>"UTF-8"}} # mutt c1abb5fb77a9d2ab2017749a7987c074 @@ -466,14 +466,14 @@ Freemont and pulling out several minutes. }, { data: IO.read('test/fixtures/mail24.box'), - body_md5: 'd41d8cd98f00b204e9800998ecf8427e', + body_md5: '5872ddcdfdf6bfe40f36cd0408fca667', params: { from: 'oracle@IG0-1-DB01.example.com', from_email: 'oracle@IG0-1-DB01.example.com', from_display_name: '', subject: 'Regelsets im Test-Status gefunden: 1', to: 'support@example.com', - body: '', + body: 'no visible content', }, attachments: [ { @@ -672,9 +672,24 @@ Weil wir die Echtheit oder Vollständigkeit der in dieserNachricht enthaltenen I to: 'info@znuny.inc', }, }, + { + data: IO.read('test/fixtures/mail34.box'), + body_md5: 'b6e46176404ec81b3ab412fe71dff0f0', + params: { + from: 'Bay ', + from_email: 'memberbay+12345@members.somewhat', + from_display_name: 'Bay', + subject: 'strange email with empty text/plain', + to: 'bay@example.com', + body: 'some html text', + }, + }, ] + count = 0 files.each { |file| + count += 1 + #p "Count: #{count}" parser = Channel::EmailParser.new data = parser.parse( file[:data] )