Suppress images for short emails

This commit is contained in:
Arpit Jalan 2014-09-13 10:56:31 +05:30
parent 42104685f7
commit 0a64657fab
4 changed files with 24 additions and 1 deletions

View file

@ -932,6 +932,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."

View file

@ -405,6 +405,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:
@ -783,4 +785,3 @@ uncategorized:
privacy_topic_id:
default: -1
hidden: true

View file

@ -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 =~ /<img[^>]+>/
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

View file

@ -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