FEATURE: Add attachments to outgoing emails

This feature is off by default and can can be configured with the `email_total_attachment_size_limit_kb` site setting.

Co-authored-by: Maja Komel <maja.komel@gmail.com>
This commit is contained in:
Gerhard Schlager
2019-07-25 14:04:00 +02:00
parent 0e1d6151b9
commit 7e0eeed292
5 changed files with 101 additions and 0 deletions

View File

@ -100,6 +100,8 @@ module Email
# guards against deleted posts
return skip(SkippedEmailLog.reason_types[:sender_post_deleted]) unless post
add_attachments(post)
topic = post.topic
first_post = topic.ordered_posts.first
@ -239,6 +241,38 @@ module Email
private
def add_attachments(post)
max_email_size = SiteSetting.email_total_attachment_size_limit_kb.kilobytes
return if max_email_size == 0
email_size = 0
post.uploads.each do |upload|
next if FileHelper.is_supported_image?(upload.original_filename)
next if email_size + upload.filesize > max_email_size
begin
path = if upload.local?
Discourse.store.path_for(upload)
else
Discourse.store.download(upload).path
end
@message.attachments[upload.original_filename] = File.read(path)
email_size += File.size(path)
rescue => e
Discourse.warn_exception(
e,
message: "Failed to attach file to email",
env: {
post_id: post.id,
upload_id: upload.id,
filename: upload.original_filename
}
)
end
end
end
def header_value(name)
header = @message.header[name]
return nil unless header