From b982992ef762a373b1680ea8f2fedec2c28b6283 Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Tue, 5 Apr 2022 13:18:49 +1000 Subject: [PATCH] FIX: Auto-generated emails causing group SMTP email storm (#16372) When emailing a group inbox and including other support-type emails (or even just regular ones with autoresponders) in the CC field, each automated reply to the group inbox triggered more emails to be sent out to all CC addresses to notify them of the new reply, which in turn caused more automated emails to be sent to the group inbox. This commit fixes the issue by preventing any emails being sent by the PostAlerter when the new post has an incoming email record which is_auto_generated, which we detect in Email::Receiver. --- app/services/post_alerter.rb | 7 +++++++ spec/services/post_alerter_spec.rb | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rb index ba091af0a0a..da681499fb0 100644 --- a/app/services/post_alerter.rb +++ b/app/services/post_alerter.rb @@ -719,6 +719,13 @@ class PostAlerter # email to the user who was just added by CC. In this case the OP probably # replied and CC'd some people, and they are the only other topic users. return if post.incoming_email.cc_addresses_split.include?(to_address) + + # We don't want to create an email storm if someone emails the group and + # CC's 50 support addresses from various places, which all then respond + # with auto-responders saying they have received our email. Any auto-generated + # emails should not propagate notifications to anyone else, not even + # the regular topic user notifications. + return email_addresses.dup.uniq if post.incoming_email.is_auto_generated? end # Send a single email using group SMTP settings to cut down on the diff --git a/spec/services/post_alerter_spec.rb b/spec/services/post_alerter_spec.rb index ebcfd4ef51c..ddc26a448be 100644 --- a/spec/services/post_alerter_spec.rb +++ b/spec/services/post_alerter_spec.rb @@ -1710,6 +1710,16 @@ describe PostAlerter do ) end + it "does not send a group smtp email for anyone if the reply post originates from an incoming email that is auto generated" do + incoming_email_post = create_post_with_incoming + topic = incoming_email_post.topic + post = Fabricate(:post, topic: topic) + Fabricate(:incoming_email, post: post, topic: topic, is_auto_generated: true) + expect_not_enqueued_with(job: :group_smtp_email) do + expect { PostAlerter.new.after_save_post(post, true) }.to change { ActionMailer::Base.deliveries.size }.by(0) + end + end + it "skips sending a notification email to the group and all other email addresses that are _not_ members of the group, sends a group_smtp_email instead" do NotificationEmailer.enable