Niall
Niall

Reputation: 138

What exceptions can be raised by action mailer

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?

Upvotes: 9

Views: 3819

Answers (4)

Cyril Duchon-Doris
Cyril Duchon-Doris

Reputation: 13949

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

Upvotes: 0

David
David

Reputation: 956

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

Upvotes: 5

LukeGT
LukeGT

Reputation: 2352

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]

Upvotes: 3

pdu
pdu

Reputation: 10413

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.

Upvotes: 3

Related Questions