我应该如何包装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
我应该如何包装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
确保您已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