mirror of
https://github.com/discourse/discourse.git
synced 2026-08-13 06:25:11 -05:00
FEATURE: Limit maximum recipients for group emails (#17971)
New maximum_recipients_per_new_group_email site setting can be used to prevent spam group emails with many recipients.
This commit is contained in:
@@ -67,6 +67,7 @@ module Email
|
||||
when Email::Receiver::OldDestinationError then :email_reject_old_destination
|
||||
when Email::Receiver::ReplyNotAllowedError then :email_reject_reply_not_allowed
|
||||
when Email::Receiver::ReplyToDigestError then :email_reject_reply_to_digest
|
||||
when Email::Receiver::TooManyRecipientsError then :email_reject_too_many_recipients
|
||||
else :email_reject_unrecognized_error
|
||||
end
|
||||
|
||||
@@ -96,6 +97,11 @@ module Email
|
||||
template_args[:number_of_days] = SiteSetting.disallow_reply_by_email_after_days
|
||||
end
|
||||
|
||||
if message_template == :email_reject_too_many_recipients
|
||||
template_args[:recipients_count] = e.recipients_count
|
||||
template_args[:max_recipients_count] = SiteSetting.maximum_recipients_per_new_group_email
|
||||
end
|
||||
|
||||
if message_template
|
||||
# inform the user about the rejection
|
||||
message = Mail::Message.new(mail_string)
|
||||
|
||||
@@ -33,6 +33,14 @@ module Email
|
||||
class OldDestinationError < ProcessingError; end
|
||||
class ReplyToDigestError < ProcessingError; end
|
||||
|
||||
class TooManyRecipientsError < ProcessingError
|
||||
attr_reader :recipients_count
|
||||
|
||||
def initialize(recipients_count:)
|
||||
@recipients_count = recipients_count
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader :incoming_email
|
||||
attr_reader :raw_email
|
||||
attr_reader :mail
|
||||
@@ -156,6 +164,11 @@ module Email
|
||||
raise UserNotFoundError unless SiteSetting.enable_staged_users
|
||||
end
|
||||
|
||||
recipients = get_all_recipients(@mail)
|
||||
if recipients.size > SiteSetting.maximum_recipients_per_new_group_email
|
||||
raise TooManyRecipientsError.new(recipients_count: recipients.size)
|
||||
end
|
||||
|
||||
body, elided = select_body
|
||||
body ||= ""
|
||||
|
||||
@@ -230,6 +243,23 @@ module Email
|
||||
raise SilencedUserError if user.silenced?
|
||||
end
|
||||
|
||||
def get_all_recipients(mail)
|
||||
recipients = Set.new
|
||||
|
||||
%i(to cc bcc).each do |field|
|
||||
next if mail[field].blank?
|
||||
|
||||
mail[field].each do |address_field|
|
||||
begin
|
||||
address_field.decoded
|
||||
recipients << address_field.address.downcase
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
recipients
|
||||
end
|
||||
|
||||
def is_bounce?
|
||||
@mail.bounced? || bounce_key
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user