Merge pull request #10687 from opf/fix/42377/mailer-interceptors

Fix mailer interceptors rejecting empty to addresses
pull/10690/head
ulferts 2 years ago committed by GitHub
commit 4557cb924d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 33
      app/mailers/application_mailer.rb
  2. 17
      app/mailers/interceptors/default_headers.rb
  3. 10
      app/mailers/interceptors/do_not_send_mails_without_recipient.rb
  4. 8
      config/initializers/register_mail_interceptors.rb
  5. 6
      spec/mailers/user_mailer_spec.rb

@ -173,36 +173,3 @@ class ApplicationMailer < ActionMailer::Base
host host
end end
end end
##
# Interceptors
#
# These are registered in config/initializers/register_mail_interceptors.rb
#
# Unfortunately, this results in changes on the interceptor classes during development mode
# not being reflected until a server restart.
class DefaultHeadersInterceptor
def self.delivering_email(mail)
mail.headers(default_headers)
end
def self.default_headers
{
'X-Mailer' => 'OpenProject',
'X-OpenProject-Host' => Setting.host_name,
'X-OpenProject-Site' => Setting.app_title,
'Precedence' => 'bulk',
'Auto-Submitted' => 'auto-generated'
}
end
end
class DoNotSendMailsWithoutReceiverInterceptor
def self.delivering_email(mail)
receivers = [mail.to, mail.cc, mail.bcc]
# the above fields might be empty arrays (if entries have been removed
# by another interceptor) or nil, therefore checking for blank?
mail.perform_deliveries = false if receivers.all?(&:blank?)
end
end

@ -0,0 +1,17 @@
module Interceptors
class DefaultHeaders
def self.delivering_email(mail)
mail.headers(default_headers)
end
def self.default_headers
{
'X-Mailer' => 'OpenProject',
'X-OpenProject-Host' => Setting.host_name,
'X-OpenProject-Site' => Setting.app_title,
'Precedence' => 'bulk',
'Auto-Submitted' => 'auto-generated'
}
end
end
end

@ -0,0 +1,10 @@
module Interceptors
class DoNotSendMailsWithoutRecipient
def self.delivering_email(mail)
receivers = [mail.to, mail.cc, mail.bcc]
# the above fields might be empty arrays (if entries have been removed
# by another interceptor) or nil, therefore checking for blank?
mail.perform_deliveries = false if receivers.all?(&:blank?)
end
end
end

@ -29,8 +29,6 @@
# Register interceptors defined in app/mailers/user_mailer.rb # Register interceptors defined in app/mailers/user_mailer.rb
# Do this here, so they aren't registered multiple times due to reloading in development mode. # Do this here, so they aren't registered multiple times due to reloading in development mode.
Rails.application.config.action_mailer.interceptors = [ ApplicationMailer.register_interceptor Interceptors::DefaultHeaders
"DefaultHeadersInterceptor", # following needs to be the last interceptor
# following needs to be the last interceptor ApplicationMailer.register_interceptor Interceptors::DoNotSendMailsWithoutRecipient
"DoNotSendMailsWithoutReceiverInterceptor"
]

@ -116,6 +116,12 @@ describe UserMailer, type: :mailer do
with_settings: { user_format: :lastname_coma_firstname } do with_settings: { user_format: :lastname_coma_firstname } do
it_behaves_like 'mail is sent' it_behaves_like 'mail is sent'
end end
context 'with the recipient being the system user' do
let(:recipient) { User.system }
it_behaves_like 'mail is not sent'
end
end end
describe '#wiki_content_added' do describe '#wiki_content_added' do

Loading…
Cancel
Save