From 77f35cb81841b4747ee54862c7ad0dc6b69ab1ca Mon Sep 17 00:00:00 2001
From: Go MAEDA
Date: Sun, 3 Mar 2019 08:17:59 +0000
Subject: [PATCH] Option to parse HTML part of multipart (HTML) emails first
(#30838).
Patch by Yuichi HARADA.
git-svn-id: http://svn.redmine.org/redmine/trunk@17913 e93f8b46-1217-0410-a6f0-8f06a7374b81
---
app/models/mail_handler.rb | 21 ++++++----
app/views/settings/_mail_handler.html.erb | 1 +
config/locales/en.yml | 1 +
config/settings.yml | 2 +
.../different_contents_in_text_and_html.eml | 38 +++++++++++++++++++
test/unit/mail_handler_test.rb | 11 ++++++
6 files changed, 67 insertions(+), 7 deletions(-)
create mode 100644 test/fixtures/mail_handler/different_contents_in_text_and_html.eml
diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rb
index be4b363b1..de59d6397 100755
--- a/app/models/mail_handler.rb
+++ b/app/models/mail_handler.rb
@@ -447,16 +447,23 @@ class MailHandler < ActionMailer::Base
end
end
- # Returns the text/plain part of the email
- # If not found (eg. HTML-only email), returns the body with tags removed
+ # Returns the text content of the email.
+ # If the value of Setting.mail_handler_preferred_body_part is 'html',
+ # it returns text converted from the text/html part of the email.
+ # Otherwise, it returns text/plain part.
def plain_text_body
return @plain_text_body unless @plain_text_body.nil?
- # check if we have any plain-text parts with content
- @plain_text_body = email_parts_to_text(email.all_parts.select {|p| p.mime_type == 'text/plain'}).presence
-
- # if not, we try to parse the body from the HTML-parts
- @plain_text_body ||= email_parts_to_text(email.all_parts.select {|p| p.mime_type == 'text/html'}).presence
+ parse_order =
+ if Setting.mail_handler_preferred_body_part == 'html'
+ ['text/html', 'text/plain']
+ else
+ ['text/plain', 'text/html']
+ end
+ parse_order.each do |mime_type|
+ @plain_text_body ||= email_parts_to_text(email.all_parts.select {|p| p.mime_type == mime_type}).presence
+ return @plain_text_body unless @plain_text_body.nil?
+ end
# If there is still no body found, and there are no mime-parts defined,
# we use the whole raw mail body
diff --git a/app/views/settings/_mail_handler.html.erb b/app/views/settings/_mail_handler.html.erb
index 36fa864bb..d14593da1 100644
--- a/app/views/settings/_mail_handler.html.erb
+++ b/app/views/settings/_mail_handler.html.erb
@@ -18,6 +18,7 @@
<%= l(:text_comma_separated) %>
<%= l(:label_example) %>: smime.p7s, *.vcf
+ <%= setting_select :mail_handler_preferred_body_part, [['Text', 'plain'], ['HTML', 'html']] %>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 38447e067..8995800c3 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -422,6 +422,7 @@ en:
setting_mail_handler_enable_regex: "Enable regular expressions"
setting_mail_handler_api_enabled: Enable WS for incoming emails
setting_mail_handler_api_key: Incoming email WS API key
+ setting_mail_handler_preferred_body_part: Preferred part of multipart (HTML) emails
setting_sys_api_key: Repository management WS API key
setting_sequential_project_identifiers: Generate sequential project identifiers
setting_gravatar_enabled: Use Gravatar user icons
diff --git a/config/settings.yml b/config/settings.yml
index e0f49ea2b..4ae9c7487 100644
--- a/config/settings.yml
+++ b/config/settings.yml
@@ -195,6 +195,8 @@ mail_handler_api_enabled:
mail_handler_api_key:
default:
security_notifications: 1
+mail_handler_preferred_body_part:
+ default: plain
issue_list_default_columns:
serialized: true
default:
diff --git a/test/fixtures/mail_handler/different_contents_in_text_and_html.eml b/test/fixtures/mail_handler/different_contents_in_text_and_html.eml
new file mode 100644
index 000000000..5db852d5b
--- /dev/null
+++ b/test/fixtures/mail_handler/different_contents_in_text_and_html.eml
@@ -0,0 +1,38 @@
+From JSmith@somenet.foo Sun Mar 02 23:30:00 2019
+From: John Smith
+Content-Type: multipart/mixed; boundary="Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9"
+Message-Id:
+Mime-Version: 1.0 (Mac OS X Mail 6.3 \(1503\))
+Subject: Different contents in text part and HTML part
+Date: Sun, 03 Mar 2019 08:30:00 +0900
+To: redmine@somenet.foo
+X-Mailer: Apple Mail (2.1503)
+
+
+
+--Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9
+Content-Transfer-Encoding: quoted-printable
+Content-Type: text/plain;
+ charset=us-ascii
+
+The text part.
+
+
+--Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9
+Content-Transfer-Encoding: quoted-printable
+Content-Type: text/html;
+ charset=us-ascii
+
+
+
+
+
+
+
+The html part.
+
+
+
+
+--Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9--
diff --git a/test/unit/mail_handler_test.rb b/test/unit/mail_handler_test.rb
index 9d7fd6d12..e5d009633 100644
--- a/test/unit/mail_handler_test.rb
+++ b/test/unit/mail_handler_test.rb
@@ -662,6 +662,17 @@ class MailHandlerTest < ActiveSupport::TestCase
assert_equal '', issue.description
end
+ def test_preferred_body_part_setting
+ with_settings :mail_handler_preferred_body_part => 'plain' do
+ issue = submit_email('different_contents_in_text_and_html.eml', :issue => {:project => 'ecookbook'})
+ assert_equal 'The text part.', issue.description
+ end
+ with_settings :mail_handler_preferred_body_part => 'html' do
+ issue = submit_email('different_contents_in_text_and_html.eml', :issue => {:project => 'ecookbook'})
+ assert_equal 'The html part.', issue.description
+ end
+ end
+
def test_attachment_text_part_should_be_added_as_issue_attachment
issue = submit_email('multiple_text_parts.eml', :issue => {:project => 'ecookbook'})
assert_not_include 'Plain text attachment', issue.description