I had a look at the class but could not see a list of possible exceptions that can be raised from delivering smtp email in rails 3.
Has anyone any idea?
I had a look at the class but could not see a list of possible exceptions that can be raised from delivering smtp email in rails 3.
Has anyone any idea?
We've found this list works pretty well for standard errors that you might want to retry on:
[ EOFError,
IOError,
TimeoutError,
Errno::ECONNRESET,
Errno::ECONNABORTED,
Errno::EPIPE,
Errno::ETIMEDOUT,
Net::SMTPAuthenticationError,
Net::SMTPServerBusy,
Net::SMTPSyntaxError,
Net::SMTPUnknownError,
OpenSSL::SSL::SSLError
]
Note that I didn't include Net::SMTPFatalError
because it is often a permanent failure (like a blacklisted email address).
This post on thoughtbot summarises all the possible SMTP exceptions and gives you a fairly elegant way of dealing with all of them.
http://robots.thoughtbot.com/post/159806037/i-accidentally-the-whole-smtp-exception
Here are the possible exceptions:
SMTP_SERVER_ERRORS = [TimeoutError,
IOError,
Net::SMTPUnknownError,
Net::SMTPServerBusy,
Net::SMTPAuthenticationError]
SMTP_CLIENT_ERRORS = [Net::SMTPFatalError, Net::SMTPSyntaxError]
More errors possible depending on which delivery method you use. In case you are using Amazon SES service through the aws-ses gem, add the following error to your array
AWS::SES::ResponseError
You can use some code like this to catch the errors
# 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
© 2022 - 2024 — McMap. All rights reserved.