24

Newly signed up users to my little app must be approved by the admin (me) before they can gain access to the site. I've succeeded in generating such emails in development with an after_create :send_admin_email in my user model which works great. My problem is that I'm generating multiple users during my tests (using FactoryGirl) and each test user created sends off a real email. Running my tests is like pouring molasses in January and I've got to delete hundreds of emails sent to my inbox. How do I turn that off?

Action Mailer Basics in the Rails Guides tells me that "By default Action Mailer does not send emails in the test environment. They are just added to the ActionMailer::Base.deliveries array."

Moreover, in config/environments/test.rb I've got:

config.action_mailer.delivery_method = :test

That's in addition to in config/environment.rb having:

# Configuration for using SendGrid on Heroku
ActionMailer::Base.smtp_settings = {
  :address        => 'smtp.sendgrid.net',
  :port           => '587',
  :authentication => :plain,
  :user_name      => 'app[my app number]@heroku.com',
  :password       => '[something super secret]',
  :domain         => '[let's get this party started!.com]',
  :enable_starttls_auto => true
}
ActionMailer::Base.delivery_method = :smtp

I'm sure that I'm missing something simple and basic. I've searched around and related questions and posts deal with how to test that ActionMailer actually sent email.

Humble gratitude in advance for any thoughts or help.

Addendum: Following answer to similar question found at Is it possible to turn off ActionMailer emails when cucumber testing is happening on development? I was able to stop the email sending madness. Still, I had to add ActionMailer::Base.delivery_method = :test to several rspec files. Is there a way I can shut this down globally? Anyone have any thoughts on what's going on?

Community
  • 1
  • 1
BenU
  • 2,287
  • 1
  • 22
  • 35
  • With all of those emails, I exceeded my 200/day allowed by Send Grid. I've found [How to write features that don't actually use Sendgrid?](https://groups.google.com/group/heroku/browse_thread/thread/7a4044f299ccf1fc/a0d687041fd97389?pli=1) which says that `ActionMailer::Base.delivery_method = :smtp' in 'config/environment.rb` is overriding `config.action_mailer.delivery_method = :test` in `config/environments/test.rb` I've moved `ActionMailer::Base.delivery_method = :smtp' into `config/environments/development.rb` and `config/environments/production.rb` which may work.Will update tomorrow. – BenU Jun 09 '12 at 20:13

3 Answers3

40

So I've figured it out. Having the line ActionMailer::Base.delivery_method = :smtp in config/environment.rb overrides ActionMailer::Base.delivery_method = :test in config/environments/test.rb.

So, delete that line, ActionMailer::Base.delivery_method = :smtp' from config/environment.rb and place it in config/environments/production.rb. That allows you to place ActionMailer::Base.delivery_method = :test in config/environments/test.rb and the version you want in config/environments/development.rb. I made development.rb :test as I populated my database using Faker and changed it to smtp so I was sure that real emails were sent as an additional check.

Note: You must restart your server for these changes to take effect.

Another note: Heroku's current SendGrid Instructions put the SendGrid Heroku configuration code in a new config/initializers/mail.rb file which will likely require removing its last line and placing the desired version in each config/environments/[production.rb, development.rb, test.rb]

jogojapan
  • 68,383
  • 11
  • 101
  • 131
BenU
  • 2,287
  • 1
  • 22
  • 35
16

Perhaps useful...

My config/environment.rb did not contain ActionMailer::Base.delivery_method = :smtp and my config/environments/test.rb did contain ActionMailer::Base.delivery_method = :test but Rails still delivered mailers while testing.

I simply added the following to config/environments/test.rb to fix:

config.action_mailer.perform_deliveries = false
josliber
  • 43,891
  • 12
  • 98
  • 133
brntsllvn
  • 931
  • 11
  • 18
  • This worked for me as well, thanks! I'm wondering why it did though when everywhere else I've read says all we needs is to set config.action_mailer.delivery_method = :test in the test environment file. – Fralcon Aug 25 '18 at 08:08
0

I faced the similar situation in Rails4.2 (ActiveJob integration with ActionMailer) even if I didn't write delivery_method = :smtp in config/environment.rb.

In my case, The issue here happened after using "resque" as background worker. Finally, I found out that the following config was WRONG:

config/initializers/active_job.rb:

Rails.application.config.active_job.queue_adapter = :resque   # here is WRONG

...because it effected to test mode as well.

So, I set "queue_adapter = :resque" only in config/environments/{development,production.rb}. Now, that works as I expect.

Fumisky Wells
  • 1,150
  • 10
  • 21