How to raise / rescue from ActionMailer delivery method

I send emails in the following way:

class Communicate < ActionMailer::Base
  def message(sub,msg,people)
    subject    sub
    bcc        people
    from       'my_email.s@gmail.com'
    sent_on    Time.now

    body       :greeting => msg
  end
end

bcc contains 4 or 5 email addresses.

During my testing, I noticed two things:

  • If even one of the email messages is not an actual email address (for example fake_email_no_domain), it does not send emails to any of the recipients.
  • if the Bcc list has a bad email address (for example, nonexistent@gmail.com), it still sends emails to other recipients, but it still gives an error to the logs.

In both scenarios, an error is thrown:

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:

  • Is there any way to catch this error? I am showing the user to the flash[:notice]user, and I would like to mention that something bad happened.
  • BCC 5 , 4 , 5- , - , ? flash[:notice]. , , , , .
+3
2

config/environments/development.rb .

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

, development production config/environments/production.rb

+2

, , ,   - :

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
0

Source: https://habr.com/ru/post/1757945/


All Articles