Project

General

Profile

Feature #30838 » 30838-preferred-part-multipart-email.patch

Yuichi HARADA, 2019-02-22 05:51

View differences:

app/models/mail_handler.rb
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/plain or text/html(the body with tags removed) part of the email
# The preferred of parse depends on Setting.mail_handler_preferred_body_part
# * 'html' - first text/html, then text/plain
# * 'plain'(Default) - first text/plain, then text/html
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
multipart_parse_order =
if Setting.mail_handler_preferred_body_part == 'html'
# HTML parts, plain-text parts
['text/html', 'text/plain']
else
# (Default) plain-text parts, HTML parts
['text/plain', 'text/html']
end
multipart_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
app/views/settings/_mail_handler.html.erb
<em class="info"><%= l(:text_comma_separated) %>
<%= l(:label_example) %>: smime.p7s, *.vcf</em>
</p>
<p><%= setting_select :mail_handler_preferred_body_part, [['Text', 'plain'], ['HTML', 'html']] %></p>
</div>
<div class="box tabular settings">
config/locales/en.yml
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
config/settings.yml
mail_handler_api_key:
default:
security_notifications: 1
mail_handler_preferred_body_part:
default: plain
issue_list_default_columns:
serialized: true
default:
test/fixtures/mail_handler/text_and_html_part.eml
From JSmith@somenet.foo Fri Mar 22 08:30:28 2013
From: John Smith <JSmith@somenet.foo>
Content-Type: multipart/mixed; boundary="Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9"
Message-Id: <BB533668-3CC8-41CA-A951-0A5D8EA37FB0@somenet.foo>
Mime-Version: 1.0 (Mac OS X Mail 6.3 \(1503\))
Subject: Test with an empty text part
Date: Fri, 22 Mar 2013 17:30:20 +0200
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww=
w.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=3D"http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dutf-8" />
</head>
<body>
<p>The html part.</p>
</body>
</html>
--Apple-Mail=_33C8180A-B097-4B87-A925-441300BDB9C9--
test/unit/mail_handler_test.rb
assert_equal '', issue.description
end
def test_preferred_body_part_setting
with_settings :mail_handler_preferred_body_part => 'plain' do
issue = submit_email('text_and_html_part.eml', :issue => {:project => 'ecookbook'})
assert_equal 'The text part.', issue.description
end
with_settings :mail_handler_preferred_body_part => 'html' do
issue = submit_email('text_and_html_part.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
(2-2/3)