diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index bcc18ea10b3..9ac7b05bbaa 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -934,6 +934,9 @@ en: disable_emails: "Prevent Discourse from sending any kind of emails" + strip_images_from_short_emails: "Strip images from emails having size less than 2800 Bytes" + short_email_length: "Short email length in Bytes" + pop3_polling_enabled: "Poll via POP3 for email replies." pop3_polling_ssl: "Use SSL while connecting to the POP3 server. (Recommended)" pop3_polling_period_mins: "The period in minutes between checking the POP3 account for email. NOTE: requires restart." diff --git a/config/site_settings.yml b/config/site_settings.yml index beed0f0723c..c8c40e0de25 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -407,6 +407,8 @@ email: email_prefix: '' email_site_title: '' disable_emails: false + strip_images_from_short_emails: true + short_email_length: 2800 files: max_image_size_kb: @@ -785,4 +787,3 @@ uncategorized: privacy_topic_id: default: -1 hidden: true - diff --git a/lib/email/sender.rb b/lib/email/sender.rb index 1e02ab151f2..640d5163a33 100644 --- a/lib/email/sender.rb +++ b/lib/email/sender.rb @@ -94,6 +94,12 @@ module Email @message.header['X-Discourse-Post-Id'] = nil @message.header['X-Discourse-Reply-Key'] = nil + # Suppress images from short emails + if SiteSetting.strip_images_from_short_emails && @message.html_part.body.to_s.bytesize <= SiteSetting.short_email_length && @message.html_part.body =~ /]+>/ + style = Email::Styles.new(@message.html_part.body.to_s) + @message.html_part.body = style.strip_avatars_and_emojis + end + begin @message.deliver rescue *SMTP_CLIENT_ERRORS => e diff --git a/lib/email/styles.rb b/lib/email/styles.rb index ecb7afb0b9c..2f62084d355 100644 --- a/lib/email/styles.rb +++ b/lib/email/styles.rb @@ -137,6 +137,19 @@ module Email end end + def strip_avatars_and_emojis + @fragment.css('img').each do |img| + if img['src'] =~ /user_avatar/ + img.remove + end + + if img['src'] =~ /plugins\/emoji/ + img.replace img['title'] + end + end + return @fragment.to_s + end + private def replace_relative_urls