Closed davidmles closed 8 years ago
You can put them in the environment-specific configuration files, so for development it is config/environments/development.rb
.
# Turn off deliveries
config.action_mailer.perfom_deliveries = false
Thanks, it worked. I hope this helps people coming here with the same issue :)
Uhm... it's working because I have disabled it for development. So if it's disabled for development, what's the point for letter_opener? It's intended for the development environment, so this solution is not ideal.
I'm reopening the issue.
Well, you can disable letter_opener
when seeding your db and re-enable it again after the seeding is done. How often do you "seed" your db? The only time I seed is when I am creating a new database. Am I missing out on something here?
I seed it more often, because I create and update my models several times. What's more, other (non-technical) people in the team follow the instructions in the Readme to seed the DB.
That's what we are going to do, but since it's not ideal I would love to know another more suitable solution.
@davidmles can you try to change delivery method from :smtp
to :test
?
@nashby I just tried it, same problem.
Let me know if you need me to do something to debug this, please.
@davidmles that's strange. Actually it works with adding only ActionMailer::Base.perform_deliveries = false
to the seeds file for me. Can you please provide a sample application that reproduces the error?
I have tried it with a new application, and it actually works.
I'm thinking it's because I use Sidekiq. Maybe that cannot be controlled by the perform_deliveries
option? Disabling Sidekiq when seeding obviously works, and that's the fix for my issue for now.
If that's the reason, I'm happy to close the issue.
@davidmles hmm, that's strange. Looking at the code I can see that sidekiq uses deliver
method which respects perform_deliveries
option. What version of rails and sidekiq are you using?
I'm using the latest version of Rails and Sidekiq, and using the deliver_later method. I will test it better tomorrow.
Steps to reproduce the issue:
gem 'sidekiq'
gem 'sinatra', require: nil
group :development do
gem 'letter_opener'
end
bundle
---
:concurrency: 5
:queues:
- mailers
- default
config.action_mailer.delivery_method = :letter_opener
class ApplicationMailer < ActionMailer::Base
def sample_email
mail from: 'example@example.org', to: 'example@example.com', subject: 'This is an example'
end
end
This is an example.
ActionMailer::Base.perform_deliveries = false
ApplicationMailer.sample_email.deliver_later
redis-server /usr/local/etc/redis.conf
bundle exec sidekiq
rails s
bundle exec rake db:seed
You should see the email actually being sent and letter opener opening it in a browser.
Hmmm deliver_now
doesn't seem to deliver the email for me. I modified the sample_email method to take in an optional parameter to differentiate the emails sent out (if there are 2).
ActionMailer::Base.perform_deliveries = false
ApplicationMailer.sample_email.deliver_later
ApplicationMailer.sample_email("Now").deliver_now # no email
Yes, as @jwgoh says, deliver_now
does not send the email, but deliver_later
does.
Is this bug related to letter_opener? Or is it something Sidekiq is not doing correctly? Or maybe a Rails bug? What do you think @nashby ?
@davidmies Sidekiq will be running a separate instance of the environment so it will still have the usual configuration of perform_deliveries=true
.
You've only set that deliveries won't be performed in the instance of the environment where your seeds are being executed. That's why deliver_now isn't sending them: because it is running in the thread where you've set mails not to be delivered.
Thanks @shadwell, that makes sense. So, isn't it possible to deny it to send them using deliver_later
?
No, not by temporarily changing the configuration I think. You've have to temporarily change it in the thread that actually sends the emails. You may have more luck skipping the actual delivery of the emails when seeding rather than trying to get letter_opener to suppress its delivery.
@davidmles is it still an issue for you? I think you can try to disable sidekiq in the seed file somehow since you don't really need it there, do you?. Probably this would work https://github.com/mperham/sidekiq/wiki/Testing. Closing this for now.
I'm late to the party on this one, but for what it's worth, my solution was to create a separate Rails environment seed.rb
that inherits from development as per a technique from DHH. Within this env file, I changed the relevant settings in a way that can handle running separate processes (although I also disable the job queue and make it inline
instead therefore reduced the problem).
require Rails.root.join('config/environments/development_base')
Rails.application.configure do
# Avoid async difficulties when seeding data
config.active_job.queue_adapter = :inline
# Do not pop up emails in browser when seeding (due to letter_opener gem)
config.action_mailer.delivery_method = :test
config.action_mailer.perform_deliveries = false
end
Rather than having to rely on an extra environment, I just disable these in my seed script:
ActiveJob::Base.queue_adapter = :inline
ActionMailer::Base.delivery_method = :test
ActionMailer::Base.perform_deliveries = false
Is it possible to stop letter_opener when seeding the DB?
I have added this snippet at the beginning of my
seeds.rb
file:But lots of windows open. No matter where I move that snippet, I have tried to place it just before the code that triggers those emails, but I have the same problem.
This is happening on my development environment.