2

我应该如何包装deliver_now方法调用,以便我们可以在交付时捕获错误?

@user = User.new(user_params)
if @user.valid?
  MyMailer.user_email(@user).deliver
  # hmmm....  how do we catch if the email is not sent?  
else
  ...
end
4

1 回答 1

2

确保您已config.action_mailer.raise_delivery_errors = true在应用程序配置中进行设置。然后,您可以使用救援块捕获异常。像这样的东西:

@user = User.new(user_params)
if @user.valid?
  begin
    MyMailer.user_email(@user).deliver
  rescue StandardError => e
    # do something with the messages in exception object e
    flash[:error] = 'Problems sending email'
  end
else
  ...
end
于 2015-10-07T19:04:08.480 回答