2012-02-08 78 views

回答

3

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

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

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,因为它通常是一个永久性的失败(如列入黑名单的电子邮件地址)。

0

根据您使用的传输方式,可能出现更多错误。如果你是通过AWS-SES宝石使用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