What exceptions can be raised by action mailer
Asked Answered
G

4

9

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?

Gyp answered 8/2, 2012 at 13:40 Comment(0)
H
5

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).

Horrible answered 7/3, 2013 at 18:47 Comment(0)
C
3

Depends on your settings on how to send mails. If you're sending mails via smtp, ActionMailer uses Net::SMTP. There you will find the errors that could be raised.

If your application is configured to use sendmail, ActionMailer uses IO.

Caesar answered 21/5, 2012 at 10:39 Comment(0)
W
3

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]
Wingfield answered 14/8, 2012 at 0:20 Comment(0)
S
0

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
Sit answered 22/4, 2016 at 17:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.