A platform for community discussion. Free, open, simple.
FEATURE: Limit maximum recipients for group emails (#17971)
Bianca Nenciub082f459c9b7
diff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex b2b180f86bf..3e774e16f52 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -2043,6 +2043,7 @@ en:max_emails_per_day_per_user: "Maximum number of emails to send users per day. 0 to disable the limit"enable_staged_users: "Automatically create staged users when processing incoming emails."maximum_staged_users_per_email: "Maximum number of staged users created when processing an incoming email."+ maximum_recipients_per_new_group_email: "Block incoming emails with too many recipients."auto_generated_allowlist: "List of email addresses that won't be checked for auto-generated content. Example: [email protected]|[email protected]"block_auto_generated_emails: "Block incoming emails identified as being auto generated."ignore_by_title: "Ignore incoming emails based on their title."@@ -3359,6 +3360,16 @@ en:If you believe this is an error, [contact a staff member](%{base_url}/about).+ email_reject_too_many_recipients:+ title: "Email Reject Too Many Recipients"+ subject_template: "[%{email_prefix}] Email issue -- Too Many Recipients"+ text_body_template: |+ We're sorry, but your email message to %{destination} (titled %{former_title}) didn't work.++ You attempted to email more than %{max_recipients_count} people and our system automatically tagged your email as spam.++ If you believe this is an error, [contact a staff member](%{base_url}/about).+email_error_notification:title: "Email Error Notification"subject_template: "[%{email_prefix}] Email issue -- POP authentication error"diff --git a/config/site_settings.yml b/config/site_settings.ymlindex 02a5094bd0b..0d1ec4f1592 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -1202,6 +1202,9 @@ email:max_emails_per_day_per_user: 100enable_staged_users: truemaximum_staged_users_per_email: 10+ maximum_recipients_per_new_group_email:+ default: 10+ min: 1auto_generated_allowlist:default: ""type: listdiff --git a/lib/email/processor.rb b/lib/email/processor.rbindex d0a266eaaf4..4d9f5085d40 100644--- a/lib/email/processor.rb+++ b/lib/email/processor.rb@@ -67,6 +67,7 @@ module Emailwhen Email::Receiver::OldDestinationError then :email_reject_old_destinationwhen Email::Receiver::ReplyNotAllowedError then :email_reject_reply_not_allowedwhen Email::Receiver::ReplyToDigestError then :email_reject_reply_to_digest+ when Email::Receiver::TooManyRecipientsError then :email_reject_too_many_recipientselse :email_reject_unrecognized_errorend@@ -96,6 +97,11 @@ module Emailtemplate_args[:number_of_days] = SiteSetting.disallow_reply_by_email_after_daysend+ 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 rejectionmessage = Mail::Message.new(mail_string)diff --git a/lib/email/receiver.rb b/lib/email/receiver.rbindex ff01667ef19..a3a6a1d118c 100644--- a/lib/email/receiver.rb+++ b/lib/email/receiver.rb@@ -33,6 +33,14 @@ module Emailclass OldDestinationError < ProcessingError; endclass ReplyToDigestError < ProcessingError; end+ class TooManyRecipientsError < ProcessingError+ attr_reader :recipients_count++ def initialize(recipients_count:)+ @recipients_count = recipients_count+ end+ end+attr_reader :incoming_emailattr_reader :raw_emailattr_reader :mail@@ -156,6 +164,11 @@ module Emailraise UserNotFoundError unless SiteSetting.enable_staged_usersend+ 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_bodybody ||= ""@@ -230,6 +243,23 @@ module Emailraise 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_keyenddiff --git a/spec/lib/email/processor_spec.rb b/spec/lib/email/processor_spec.rbindex 4fd1c0c1121..01a0dc61612 100644--- a/spec/lib/email/processor_spec.rb+++ b/spec/lib/email/processor_spec.rb@@ -193,4 +193,26 @@ RSpec.describe Email::Processor do))endend++ describe 'when group email recipients exceeds maximum_recipients_per_new_group_email site setting' do+ let(:mail) { file_from_fixtures("cc.eml", "emails").read }++ it 'rejects the email with the right response' do+ SiteSetting.maximum_recipients_per_new_group_email = 3++ processor = Email::Processor.new(mail)+ processor.process!++ rejection_raw = ActionMailer::Base.deliveries.first.body.to_s++ expect(rejection_raw).to eq(+ I18n.t("system_messages.email_reject_too_many_recipients.text_body_template",+ destination: '["[email protected]"]',+ former_title: 'The more, the merrier',+ max_recipients_count: 3,+ base_url: Discourse.base_url,+ )+ )+ end+ endenddiff --git a/spec/lib/email/receiver_spec.rb b/spec/lib/email/receiver_spec.rbindex 306afa83e4e..b12aea30caa 100644--- a/spec/lib/email/receiver_spec.rb+++ b/spec/lib/email/receiver_spec.rb@@ -883,6 +883,11 @@ RSpec.describe Email::Receiver doexpect(Topic.last.ordered_posts[-1].post_type).to eq(Post.types[:moderator_action])end+ it "rejects messages with too many recipients" do+ SiteSetting.maximum_recipients_per_new_group_email = 3+ expect { process(:cc) }.to raise_error(Email::Receiver::TooManyRecipientsError)+ end+describe "reply-to header" dobefore doSiteSetting.block_auto_generated_emails = false