动作邮件可以提出什么例外

时间:2012-02-08 13:40:15

标签: ruby-on-rails exception actionmailer raise

我查看了该课程,但看不到可以通过在rails 3中提供smtp电子邮件而引发的可能异常列表。

有人有任何想法吗?

4 个答案:

答案 0 :(得分:3)

取决于您如何发送邮件的设置。如果您通过smtp发送邮件,则ActionMailer会使用Net::SMTP。在那里你会发现可能出现的错误。

如果您的应用程序配置为使用sendmail,则ActionMailer使用IO

答案 1 :(得分:3)

我们发现此列表非常适合您可能要重试的标准错误:

[ EOFError,
IOError,
TimeoutError,
Errno::ECONNRESET,
Errno::ECONNABORTED,
Errno::EPIPE,
Errno::ETIMEDOUT,
Net::SMTPAuthenticationError,
Net::SMTPServerBusy,
Net::SMTPSyntaxError,
Net::SMTPUnknownError,
OpenSSL::SSL::SSLError
]

请注意,我没有包含Net::SMTPFatalError,因为它通常是永久性失败(如列入黑名单的电子邮件地址)。

答案 2 :(得分:2)

这篇关于thinkbot的帖子总结了所有可能的SMTP异常,并为您提供了一种相当优雅的方式来处理所有这些异常。

http://robots.thoughtbot.com/post/159806037/i-accidentally-the-whole-smtp-exception

以下是可能的例外情况:

SMTP_SERVER_ERRORS = [TimeoutError,
                      IOError,
                      Net::SMTPUnknownError,
                      Net::SMTPServerBusy,
                      Net::SMTPAuthenticationError]

SMTP_CLIENT_ERRORS = [Net::SMTPFatalError, Net::SMTPSyntaxError]

答案 3 :(得分:0)

根据您使用的送货方式,可能会出现更多错误。如果您通过aws-ses gem使用Amazon SES服务,请将以下错误添加到您的数组

AWS::SES::ResponseError

你可以使用这样的代码来捕捉错误

# some_utility_class.rb
# Return false if no error, otherwise returns the error
  def try_delivering_email(options = {}, &block)
    begin
      yield
      return false
    rescue  EOFError,
            IOError,
            TimeoutError,
            Errno::ECONNRESET,
            Errno::ECONNABORTED,
            Errno::EPIPE,
            Errno::ETIMEDOUT,
            Net::SMTPAuthenticationError,
            Net::SMTPServerBusy,
            Net::SMTPSyntaxError,
            Net::SMTPUnknownError,
            AWS::SES::ResponseError,
            OpenSSL::SSL::SSLError => e
      log_exception(e, options)
      return e
    end
  end

# app/controller/your_controller.rb

if @foo.save
  send_email
  ...


private

  def send_email
    if error = Utility.try_delivering_email { MyMailer.my_action.deliver_now }
      flash('Could not send email : ' + error.message)
    end
  end