OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Fix mailer interceptors rejecting empty to addresses
Oliver Günther064ff976b426
diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rbindex fede49e4df1..1e74942e854 100644--- a/app/mailers/application_mailer.rb+++ b/app/mailers/application_mailer.rb@@ -173,36 +173,3 @@ class ApplicationMailer < ActionMailer::Basehostendend--##-# 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-enddiff --git a/app/mailers/interceptors/default_headers.rb b/app/mailers/interceptors/default_headers.rbnew file mode 100644index 00000000000..b387445d8c6--- /dev/null+++ b/app/mailers/interceptors/default_headers.rb@@ -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+enddiff --git a/app/mailers/interceptors/do_not_send_mails_without_recipient.rb b/app/mailers/interceptors/do_not_send_mails_without_recipient.rbnew file mode 100644index 00000000000..a6d4192d772--- /dev/null+++ b/app/mailers/interceptors/do_not_send_mails_without_recipient.rb@@ -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+enddiff --git a/config/initializers/register_mail_interceptors.rb b/config/initializers/register_mail_interceptors.rbindex e47f9b9d5ae..b6c9329bb38 100644--- a/config/initializers/register_mail_interceptors.rb+++ b/config/initializers/register_mail_interceptors.rb@@ -29,8 +29,6 @@# 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.-Rails.application.config.action_mailer.interceptors = [- "DefaultHeadersInterceptor",- # following needs to be the last interceptor- "DoNotSendMailsWithoutReceiverInterceptor"-]+ApplicationMailer.register_interceptor Interceptors::DefaultHeaders+# following needs to be the last interceptor+ApplicationMailer.register_interceptor Interceptors::DoNotSendMailsWithoutRecipientdiff --git a/spec/mailers/user_mailer_spec.rb b/spec/mailers/user_mailer_spec.rbindex 72424ba8842..c0b40d5f85e 100644--- a/spec/mailers/user_mailer_spec.rb+++ b/spec/mailers/user_mailer_spec.rb@@ -116,6 +116,12 @@ describe UserMailer, type: :mailer dowith_settings: { user_format: :lastname_coma_firstname } doit_behaves_like 'mail is sent'end++ context 'with the recipient being the system user' do+ let(:recipient) { User.system }++ it_behaves_like 'mail is not sent'+ endenddescribe '#wiki_content_added' do