I'm trying to prevent emails from being delivered so I can inspect the contents of ActionMailer::Base.deliveries
for testing in a staging environment. I have config.action_mailer.delivery_method = :test
in my environment settings but Rails is still attempting to deliver the emails and the deliveries array is empty. What am I doing wrong?
Asked
Active
Viewed 520 times
1

kid_drew
- 3,857
- 6
- 28
- 38
-
In which environment settings specifically? – Dave Newton Mar 01 '21 at 21:14
-
I'm testing it out in my dev environment but I'm ultimately going to use it in a staging environment. I want to log the emails to the database instead of delivering them. – kid_drew Mar 01 '21 at 21:20
-
In which file did you set the `delivery_method` config? Does your runtime config match w/ the environment you set it in? What version of RoR? etc. – Dave Newton Mar 01 '21 at 21:24
-
`config/environments/development.rb`. I even double checked in the rails console that `Rails.application.config.action_mailer.delivery_method` is set properly. Rails 6.1.1 – kid_drew Mar 01 '21 at 21:30
-
is `config.action_mailer.perform_deliveries` set to `false`? – engineersmnky Mar 01 '21 at 21:41
-
No. Won't that prevent deliveries completely? – kid_drew Mar 01 '21 at 21:44
-
Sorry, I misread. It's set to nil. – kid_drew Mar 01 '21 at 21:45
-
Does fixing that resolve the issue? – Dave Newton Mar 01 '21 at 22:41
-
No. Setting it to true does the same thing. Setting it to false prevent the mail from sending at all. – kid_drew Mar 02 '21 at 16:57
-
Scratch that. Setting it to true does deliver and get caught in the deliveries array. But reloading the app clears that array, so that's a different issue. Interesting. – kid_drew Mar 02 '21 at 17:30
1 Answers
0
I agree with the other commenter that the best way to prevent all deliveries is through the action_mailer config.
However, if you want an alternative approach that would allow you to redirect any mail, or halt delivery based on a dynamic set of conditions, etc., then I would encourage you to check out Mail Interceptors:
http://guides.rubyonrails.org/action_mailer_basics.html#intercepting-emails

Bryce
- 2,802
- 1
- 21
- 46