diff --git a/app/errors/email_delivery_error.rb b/app/errors/email_delivery_error.rb new file mode 100644 index 0000000000..93b0cbfecd --- /dev/null +++ b/app/errors/email_delivery_error.rb @@ -0,0 +1 @@ +class EmailDeliveryError < StandardError; end diff --git a/app/mailers/core_application_mailer.rb b/app/mailers/core_application_mailer.rb index f33a604c6e..ff8a2f0870 100644 --- a/app/mailers/core_application_mailer.rb +++ b/app/mailers/core_application_mailer.rb @@ -42,7 +42,18 @@ def send_custom_email(recipient:, subject:, body_html:) # Make the request and handle the response response = http.request(request) + unless response.is_a?(Net::HTTPSuccess) + raise EmailDeliveryError, + "Email API failed (#{response.code}): #{response.body}" + end + Rails.logger.info "Email API Response: #{response.code} - #{response.body}" + rescue Net::OpenTimeout, Net::ReadTimeout => e + raise EmailDeliveryError, "Email API timeout: #{e.message}" + rescue SocketError, Errno::ECONNREFUSED => e + raise EmailDeliveryError, "Email API connection error: #{e.message}" + rescue JSON::ParserError => e + raise EmailDeliveryError, "Email API response parsing error: #{e.message}" end private diff --git a/config/application.rb b/config/application.rb index a206d705d5..9bfd17d03c 100644 --- a/config/application.rb +++ b/config/application.rb @@ -149,20 +149,6 @@ def self.parent_name # Mailer configuration for inquiries/requests config.limes_mail_server_endpoint = ENV["LIMES_MAIL_SERVER_API_ENDPOINT"] - config.action_mailer.raise_delivery_errors = true - config.action_mailer.delivery_method = :smtp - config.action_mailer.smtp_settings = { - domain: ENV['MONSOON_DASHBOARD_MAIL_DOMAIN'], - address: ENV['MONSOON_DASHBOARD_MAIL_SERVER'], - port: ENV['MONSOON_DASHBOARD_MAIL_SERVER_PORT'] || 25, - user_name: ENV['MONSOON_DASHBOARD_MAIL_USER'], - password: ENV['MONSOON_DASHBOARD_MAIL_PASSWORD'], - authentication: ENV['MONSOON_DASHBOARD_MAIL_AUTHENTICATION'] || 'plain', - enable_starttls_auto: true - } - config.action_mailer.default_options = { - from: ENV['MONSOON_DASHBOARD_MAIL_SENDER'].to_s - } config.middleware.use SessionCookiePathMiddleware config.middleware.insert_after ::Rack::Runtime, SameSiteCookieMiddleware diff --git a/config/environments/development.rb b/config/environments/development.rb index ef22a19ba4..c300d5c788 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -40,11 +40,6 @@ # Store uploaded files on the local file system (see config/storage.yml for options). config.active_storage.service = :local - # Don't care if the mailer can't send. - config.action_mailer.raise_delivery_errors = false - - config.action_mailer.perform_caching = false - # Print deprecation notices to the Rails logger. config.active_support.deprecation = :log diff --git a/config/environments/production.rb b/config/environments/production.rb index fc9a33c0c7..66d0965ec4 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -96,12 +96,6 @@ # config.active_job.queue_adapter = :resque # config.active_job.queue_name_prefix = "monsoon_dashboard_production" - config.action_mailer.perform_caching = false - - # Ignore bad email addresses and do not raise email delivery errors. - # Set this to true and configure the email server for immediate delivery to raise delivery errors. - # config.action_mailer.raise_delivery_errors = false - # Enable locale fallbacks for I18n (makes lookups for any locale fall back to # the I18n.default_locale when a translation cannot be found). config.i18n.fallbacks = true diff --git a/config/environments/test.rb b/config/environments/test.rb index bc267761a8..1fb6ea349b 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -37,12 +37,6 @@ # Store uploaded files on the local file system in a temporary directory. config.active_storage.service = :test - config.action_mailer.perform_caching = false - - # Tell Action Mailer not to deliver emails to the real world. - # The :test delivery method accumulates sent emails in the - # ActionMailer::Base.deliveries array. - config.action_mailer.delivery_method = :test config.active_job.queue_adapter = :test # Print deprecation notices to the stderr. diff --git a/plugins/inquiry/app/models/inquiry/inquiry.rb b/plugins/inquiry/app/models/inquiry/inquiry.rb index 027fe2fb6c..415f199884 100644 --- a/plugins/inquiry/app/models/inquiry/inquiry.rb +++ b/plugins/inquiry/app/models/inquiry/inquiry.rb @@ -395,7 +395,6 @@ def get_callback_action end end - # Note: for testing use 'deliver_now' def notify_requester # puts "######### NOTIFY REQUESTER #########" begin @@ -404,8 +403,8 @@ def notify_requester self.requester.full_name, self, self.process_steps.last, - ).deliver_later - rescue Net::SMTPError => e + ) + rescue EmailDeliveryError => e Rails.logger.error "InquiryMailer: Could not send email to requester #{@user_email}. Exception: #{e.message}" end end @@ -420,8 +419,8 @@ def notify_new_project inform_new_project_dl, self, self.requester.full_name, - ).deliver_later - rescue Net::SMTPError => e + ) + rescue EmailDeliveryError => e Rails.logger.error "InquiryMailer: Could not send email to #{inform_new_project_dl} Exception: #{e.message}" end end @@ -436,20 +435,9 @@ def notify_additional_recipients self, self.process_steps.last, self.requester, - ).deliver_later - rescue Net::SMTPError => e - emails.each do |email| - begin - InquiryMailer.notification_email_additional_recipients( - [email], - self, - self.process_steps.last, - self.requester, - ).deliver_later - rescue Net::SMTPError => ex - Rails.logger.error "InquiryMailer: Could not send email to #{email} Exception: #{e.message}" - end - end + ) + rescue EmailDeliveryError => e + Rails.logger.error "InquiryMailer: Could not send email to additional recipients #{self.additional_recipients}. Exception: #{e.message}" end end @@ -461,20 +449,9 @@ def notify_processors self, self.process_steps.last, self.requester, - ).deliver_later - rescue Net::SMTPError => e - self.processors.each do |p| - begin - InquiryMailer.notification_email_processors( - [p.email], - self, - self.process_steps.last, - self.requester, - ).deliver_later - rescue Net::SMTPError => ex - Rails.logger.error "InquiryMailer: Could not send email to requester #{p.email}. Exception: #{ex.message}" - end - end + ) + rescue EmailDeliveryError => e + Rails.logger.error "InquiryMailer: Could not send email to processors #{(self.processors.map { |p| p.email }).compact}. Exception: #{e.message}" end end