discourse/lib/email/cleaner.rb
Krzysztof Kotlarek c32bd8ae48 FEATURE: Remove attachments and truncate raw field for incoming emails (#8253)
Adds the settings: 

raw_email_max_length, raw_rejected_email_max_length, delete_rejected_email_after_days. 

These settings control retention of the "raw" emails logs.

raw_email_max_length ensures that if we get incoming email that is huge we will truncate it removing uploads from the raw log.

raw_rejected_email_max_length introduces an even more aggressive truncation for rejected incoming mail. 

delete_rejected_email_after_days controls how many days we will keep rejected emails for (default 90)
2019-10-30 16:54:35 +11:00

43 lines
1.0 KiB
Ruby

# frozen_string_literal: true
module Email
class Cleaner
def initialize(mail, remove_attachments: true, truncate: true, rejected: false)
@mail = Mail.new(mail)
@mail.charset = 'UTF-8'
@remove_attachments = remove_attachments
@truncate = truncate
@rejected = rejected
end
def execute
@mail.without_attachments! if @remove_attachments
truncate! if @truncate
remove_null_byte(@mail.to_s)
end
def self.delete_rejected!
IncomingEmail.delete_by('rejection_message IS NOT NULL AND created_at < ?', SiteSetting.delete_rejected_email_after_days.days.ago)
end
private
def truncate!
parts.each { |part| part.body = part.body.decoded.truncate(truncate_limit, omission: '') }
end
def parts
@mail.multipart? ? @mail.parts : [@mail]
end
def truncate_limit
@rejected ? SiteSetting.raw_rejected_email_max_length : SiteSetting.raw_email_max_length
end
def remove_null_byte(message)
message.gsub!("\x00", "")
message
end
end
end