diff --git a/.gitignore b/.gitignore index 5ba2c65..cbd1d9e 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ test/dummy/db/*.sqlite3-journal test/dummy/log/*.log test/dummy/tmp/ test/dummy/.sass-cache +**/.DS_Store diff --git a/app/mailers/ep_postmaster/postmaster.rb b/app/mailers/ep_postmaster/postmaster.rb index 09b32af..507c2cc 100644 --- a/app/mailers/ep_postmaster/postmaster.rb +++ b/app/mailers/ep_postmaster/postmaster.rb @@ -2,7 +2,10 @@ module EpPostmaster class Postmaster < ::ActionMailer::Base def bounced_email(options = {}) - @to = MailgunPost.unfurl(options.fetch(:original_sender) { options.fetch(:reply_to) }) + @to = Array.wrap(MailgunPost.unfurl(options.fetch(:original_sender) { options.fetch(:reply_to) })) + @to -= EpPostmaster.configuration.ignored_bounce_emails + return if @to.empty? + @recipient = options.fetch(:original_recipient) @subject = options[:original_subject] @error = options[:error] diff --git a/lib/ep_postmaster/configuration.rb b/lib/ep_postmaster/configuration.rb index 29b6133..87da464 100644 --- a/lib/ep_postmaster/configuration.rb +++ b/lib/ep_postmaster/configuration.rb @@ -1,9 +1,10 @@ module EpPostmaster class Configuration - attr_accessor :mailgun_api_key, :mailer_sender, :mailer_deliverer + attr_accessor :mailgun_api_key, :mailer_sender, :mailer_deliverer, :ignored_bounce_emails def initialize @mailgun_api_key = "" + @ignored_bounce_emails = [] self.mailer_deliverer = ->(message) { message.deliver_now } self.bounced_email_handler = Class.new do def self.handle_bounced_email!(*) diff --git a/mise.toml b/mise.toml new file mode 100644 index 0000000..becf770 --- /dev/null +++ b/mise.toml @@ -0,0 +1,2 @@ +[tools] +ruby = "3.4" diff --git a/test/unit/mailers/ep_postmaster/postmaster_test.rb b/test/unit/mailers/ep_postmaster/postmaster_test.rb index 3881dab..675bb3e 100644 --- a/test/unit/mailers/ep_postmaster/postmaster_test.rb +++ b/test/unit/mailers/ep_postmaster/postmaster_test.rb @@ -40,6 +40,15 @@ class PostmasterTest < ActionMailer::TestCase should "say something in the body of the email" do assert_match "unable to deliver", mail.body.encoded end + + should "not send anything when the original sender is in a list of ignored addresses" do + EpPostmaster.configure { |config| config.ignored_bounce_emails = [ "sender@test.test" ] } + message = Postmaster.bounced_email(options).message + EpPostmaster.configure { |config| config.ignored_bounce_emails = [] } + + assert message.is_a?(ActionMailer::Base::NullMail) + + end end private