From a92fd9d701d1ae0d0bcdfda26c5bd33e5d39543e Mon Sep 17 00:00:00 2001 From: David McClure Date: Thu, 26 May 2016 01:13:01 -0700 Subject: [PATCH] Add Site Setting to use HTML from incoming email when available. (#4236) --- config/locales/server.en.yml | 1 + config/site_settings.yml | 2 ++ lib/email/receiver.rb | 23 ++++++++++++++--------- spec/components/email/receiver_spec.rb | 12 ++++++++++++ 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index ee9d9ebb1..db341f15c 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1166,6 +1166,7 @@ en: reply_by_email_enabled: "Enable replying to topics via email." reply_by_email_address: "Template for reply by email incoming email address, for example: %{reply_key}@reply.example.com or replies+%{reply_key}@example.com" + incoming_email_prefer_html: "Use the HTML instead of the text for incoming email. May cause unexcpeted formatting issues!" disable_emails: "Prevent Discourse from sending any kind of emails" diff --git a/config/site_settings.yml b/config/site_settings.yml index f58e42c6a..18c732a2b 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -544,6 +544,8 @@ email: pop3_polling_username: '' pop3_polling_password: '' log_mail_processing_failures: false + incoming_email_prefer_html: + default: false email_in: default: false client: true diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb index bc2b98005..d71cf8ca7 100644 --- a/lib/email/receiver.rb +++ b/lib/email/receiver.rb @@ -212,16 +212,21 @@ module Email text = fix_charset(@mail) end - # prefer text over html - text = trim_discourse_markers(text) if text.present? - text, elided = EmailReplyTrimmer.trim(text, true) if text.present? - return [text, elided] if text.present? + use_html = html.present? && (!text.present? || SiteSetting.incoming_email_prefer_html) + use_text = text.present? unless use_html - # clean the html if that's all we've got - html = Email::HtmlCleaner.new(html).output_html if html.present? - html = trim_discourse_markers(html) if html.present? - html, elided = EmailReplyTrimmer.trim(html, true) if html.present? - return [html, elided] if html.present? + if use_text + text = trim_discourse_markers(text) + text, elided = EmailReplyTrimmer.trim(text, true) + return [text, elided] + end + + if use_html + html = Email::HtmlCleaner.new(html).output_html + html = trim_discourse_markers(html) + html, elided = EmailReplyTrimmer.trim(html, true) + return [html, elided] + end end def fix_charset(mail_part) diff --git a/spec/components/email/receiver_spec.rb b/spec/components/email/receiver_spec.rb index f996028ec..7a802acd1 100644 --- a/spec/components/email/receiver_spec.rb +++ b/spec/components/email/receiver_spec.rb @@ -171,6 +171,18 @@ describe Email::Receiver do expect(topic.posts.last.raw).to eq("This is the *text* part.") end + it "prefers html over text when site setting is enabled" do + SiteSetting.incoming_email_prefer_html = true + expect { process(:text_and_html_reply) }.to change { topic.posts.count } + expect(topic.posts.last.raw).to eq('This is the html part.') + end + + it "uses text when prefer_html site setting is enabled but no html is available" do + SiteSetting.incoming_email_prefer_html = true + expect { process(:text_reply) }.to change { topic.posts.count } + expect(topic.posts.last.raw).to eq("This is a text reply :)") + end + it "removes the 'on , wrote' quoting line" do expect { process(:on_date_contact_wrote) }.to change { topic.posts.count } expect(topic.posts.last.raw).to eq("This is the actual reply.")