31

sometimes when I am developing, I do not have an internet connection. This results in an error wherever my app is supposed to send an email:

getaddrinfo: nodename nor servname provided, or not known

Is there a simple and quick way where i can change a config value to make ActionMailer just not try to actually send out an email and not throw an error? Maybe something thats scoped to the development environment. Or some other way I can avoid the error being thrown and my code passing wherever I call the actionmailer deliver?

I'm using Rails 3.1

AnFi
  • 10,493
  • 3
  • 23
  • 47
alik
  • 3,820
  • 9
  • 41
  • 55

2 Answers2

67

It's common practice to just let Rails ignore the mail errors. In your config/environments/development.rb file add, uncomment or modify:

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

You can also set this:

config.action_mailer.perform_deliveries = false

See the documentation here http://edgeguides.rubyonrails.org/action_mailer_basics.html#action-mailer-configuration

You can also set the delivery method to :test, but I have not actually tried that

config.action_mailer.delivery_method = :test
Wizard of Ogz
  • 12,543
  • 2
  • 41
  • 43
  • 4
    For people curious about `.delivery_method = :test`: instead of sending any emails, ActionMailer just stores them in an array available at `ActionMailer::Base.deliveries`. This is probably the preferred option for most people, since it enables you to e.g. put a breakpoint in the code and make sure that ActionMailer received your send request (and that it looks like it should), or of course utilize it in tests. (src: https://api.rubyonrails.org/classes/ActionMailer/Base.html) – fgblomqvist Dec 13 '18 at 21:22
17

If you want to disable mail deliveries after your rails app has been initialized (while creating sample data, during migrations, etc.):

ActionMailer::Base.perform_deliveries = false
dhulihan
  • 11,053
  • 9
  • 40
  • 45