How to catch error exception in ActionMailer

前端 未结 5 777
误落风尘
误落风尘 2020-12-05 00:58

The problem is how can I catch exception in delivering mail by ActionMailer. For me it sounds impossible, because in this case ActionMailer should sent mail to mailserver, a

5条回答
  •  离开以前
    2020-12-05 01:26

    This is inspired by the answer given by @sukeerthi-adiga

    class ApplicationMailer < ActionMailer::Base
      # Add other exceptions you want rescued in the array below
      ERRORS_TO_RESCUE = [
        Net::SMTPAuthenticationError,
        Net::SMTPServerBusy,
        Net::SMTPSyntaxError,
        Net::SMTPFatalError,
        Net::SMTPUnknownError,
        Errno::ECONNREFUSED
      ]
    
      rescue_from *ERRORS_TO_RESCUE do |exception|
        # Handle it here
        Rails.logger.error("failed to send email")
      end
    
    end
    

    The * is the splat operator. It expands an Array into a list of arguments.

    More explanation about (*) splat operator

提交回复
热议问题