Patrick
Patrick

Reputation: 475

how to raise/rescue from ActionMailer deliver method

I am sending emails with the following method:

class Communicate < ActionMailer::Base
  def message(sub,msg,people)
    subject    sub
    bcc        people
    from       '[email protected]'
    sent_on    Time.now

    body       :greeting => msg
  end
end

bcc contains 4 or 5 email addresses.

During my testing I've noticed two things:

In both scenarios the error that is thrown is:

Redirected to http://localhost:3000/
Completed in 2601ms (DB: 1) | 302 Found [http://localhost/notifications]

    [2010-08-04 00:49:00] ERROR Errno::ECONNRESET: Connection reset by peer
        /usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:56:in `eof?'
        /usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:56:in `run'
        /usr/local/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'

Questions:

Upvotes: 4

Views: 4352

Answers (2)

Mohammad Shahnawaz
Mohammad Shahnawaz

Reputation: 876

If I am writing the code to sent the email in controller i can write like these to handle the rescue in my application I'm using something like this in the controller:

if @user.save
  begin
  UserMailer.welcome_email(@user).deliver
  flash[:success] = "#{@user.name} created"
  rescue Net::SMTPAuthenticationError, Net::SMTPServerBusy, Net::SMTPSyntaxError, Net::SMTPFatalError, Net::SMTPUnknownError => e
  flash[:success] = "User #{@user.name} creating Problems sending mail"
  end
  redirect_to home_index_path
end

Upvotes: 0

Salil
Salil

Reputation: 47482

Add or uncomment following line in the config/environments/development.rb and restart your server.

# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false

I assume you check for the development for production add line in config/environments/production.rb

Upvotes: 2

Related Questions