How should I wrap a deliver_now
method call so we can catch errors if they occur while delivering?
@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
Make sure you have config.action_mailer.raise_delivery_errors = true
set in your application config. Then you can catch exceptions with a rescue block. Something like this:
@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