Browse Source

Improved email parsing.

Martin Edenhofer 8 years ago
parent
commit
3e07697954

+ 12 - 3
app/models/channel/email_parser.rb

@@ -108,9 +108,18 @@ class Channel::EmailParser
       data[:from_display_name] = Mail::Address.new(from).display_name ||
                                  (Mail::Address.new(from).comments && Mail::Address.new(from).comments[0])
     rescue
-      data[:from_email]  = from
-      data[:from_local]  = from
-      data[:from_domain] = from
+      from.strip!
+      if from =~ /^(.+?)<(.+?)@(.+?)>$/
+        data[:from_email]        = "#{$2}@#{$3}"
+        data[:from_local]        = $2
+        data[:from_domain]       = $3
+        data[:from_display_name] = $1.strip
+        data[:from_display_name].delete!('"')
+      else
+        data[:from_email]  = from
+        data[:from_local]  = from
+        data[:from_domain] = from
+      end
     end
 
     # do extra decoding because we needed to use field.value

+ 18 - 0
test/fixtures/mail45.box

@@ -0,0 +1,18 @@
+To: James-Max ROGER <james-max.roger@example.com>, Support <zammad@example.com>
+Subject: Nouveau message contact élégibilité Zammad
+X-PHP-Originating-Script: 111:contact.php
+From: Ups Rémi <r.ordonaud@example.com>
+Message-Id: <20170111111111.8CEF15FB28@01.localdomain>
+Date: Fri,  6 Jan 2017 19:23:41 +0100 (CET)
+
+Nouveau message :
+=============================
+Nom: Orberer
+Prénom: Rémi
+Téléphone: xxxx
+Email: xxx@example.com
+Societé: KG
+Adresse: RdL
+CP: 11111
+Ville: Olèchena
+

+ 14 - 2
test/unit/email_parser_test.rb

@@ -680,13 +680,25 @@ end
         body_md5: '2f0f5a21e4393c174d4670a188fc5548',
         params: {
           from: '"Clement.Si" <Claudia.Shu@yahoo.com.>',
-          from_email: '"Clement.Si" <Claudia.Shu@yahoo.com.>',
-          from_display_name: '',
+          from_email: 'Claudia.Shu@yahoo.com.',
+          from_display_name: 'Clement.Si',
           subject: '精益生产闪婚,是谁的责任',
           to: 'abuse@domain.com',
           cc: nil,
         },
       },
+      {
+        data: IO.binread('test/fixtures/mail45.box'),
+        body_md5: '1d847e3626145a9e886914ecf0d89368',
+        params: {
+          from: '"Ups Rémi" <r.ordonaud@example.com>',
+          from_email: 'r.ordonaud@example.com',
+          from_display_name: 'Ups Rémi',
+          subject: 'Nouveau message contact élégibilité Zammad',
+          to: 'James-Max ROGER <james-max.roger@example.com>, Support <zammad@example.com>',
+          cc: nil,
+        },
+      },
     ]
 
     count = 0

+ 27 - 1
test/unit/email_process_test.rb

@@ -2015,7 +2015,33 @@ Some Text',
       },
       {
         data: IO.binread('test/fixtures/mail44.box'),
-        success: false,
+        success: true,
+        result: {
+          0 => {
+            priority: '2 normal',
+            title: '精益生产闪婚,是谁的责任',
+          },
+          1 => {
+            sender: 'Customer',
+            type: 'email',
+          },
+        },
+        verify: {
+          users: [
+            {
+              firstname: 'Clement.Si',
+              lastname: '',
+              fullname: 'Clement.Si',
+              email: 'claudia.shu@yahoo.com.',
+            },
+            {
+              firstname: '',
+              lastname: '',
+              fullname: 'abuse@domain.com',
+              email: 'abuse@domain.com',
+            },
+          ],
+        }
       },
     ]
     assert_process(files)