Browse Source

Fixes #4304 - Mails cannot be processed if the special character "+" appears...

Mantas Masalskis 1 year ago
parent
commit
c9620cdc0f

+ 24 - 0
lib/core_ext/email_address/lib/email_address/local.rb

@@ -0,0 +1,24 @@
+# Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
+
+# EmailAddress gem reports email addresses starting with + as invalid.
+# However, email addresses starting with + are quite common.
+# For example as phone numbers which start + in international format.
+#
+# There is a PR to fix this issue in EmailAddress gem
+# However, it is not merged for months
+# https://github.com/afair/email_address/pull/93
+#
+# This monkeypatch shall be removed once above PR is merged
+# and EmailAddress gem is updated.
+
+module EmailAddressValidator
+  class Local
+    def parse_tag(raw)
+      separator = @config[:tag_separator] ||= '+'
+
+      return raw if raw.start_with? separator
+
+      raw.split(separator, 2)
+    end
+  end
+end

+ 6 - 0
spec/lib/email_address_validation_spec.rb

@@ -106,5 +106,11 @@ RSpec.describe EmailAddressValidation do
 
       include_examples 'email address validity', valid: false, check_mx: false
     end
+
+    describe 'which starts with +' do
+      let(:email_address) { '+123@example.org' }
+
+      include_examples 'email address validity', valid: true, check_mx: false
+    end
   end
 end

+ 1 - 1
spec/models/channel/email_parser_spec.rb

@@ -37,7 +37,7 @@ RSpec.describe Channel::EmailParser, type: :model do
       end
 
       it 'ensures tests were dynamically generated' do
-        expect(tests.count).to eq(107)
+        expect(tests.count).to eq(108)
       end
     end
 

+ 8 - 0
test/data/mail/mail108.box

@@ -0,0 +1,8 @@
+From: +123@example.com
+To: +123@example.com
+Date: Fri, 07 Apr 2023 17:30:44 +0200Received: from m241-41.example.com (m241-41.example.com [1.1.1.1])
+	by in45.mail.example.com (Postfix) with ESMTPS id 4PgzCH5GFlz279jM6
+	for <service@example.com>; Tue, 21 Mar 2023 17:30:35 +0000 (UTC)
+Subject: test
+
+some text

+ 11 - 0
test/data/mail/mail108.yml

@@ -0,0 +1,11 @@
+--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
+from: "+123@example.com"
+from_email: "+123@example.com"
+from_display_name: ''
+to: "+123@example.com"
+subject: test
+body: 'some text
+
+  '
+content_type: text/plain
+attachments: []