Sidekiq and rails 4 actionmailer never delivers emails

后端 未结 4 719
星月不相逢
星月不相逢 2021-02-05 19:06

I\'ve setup sidekiq in a rails 4 application for sending emails and processing background jobs. I have also devise which I am using devise_async and a typical contact form email

相关标签:
4条回答
  • 2021-02-05 19:25

    My setup is the same as this:

    • Rails 4
    • Ruby 1.9.3
    • actionmailer
    • sidekiq
    • devise_async

    My code to run the mailer runs just fine and exits without any errors - but nothing is done and no email is sent. From my mailer:

    logger.debug "about to mail"
    logger.debug Rails.application.config.action_mailer.sendmail_settings.inspect
    mail(to: purchase.email, subject: 'Your key')
    logger.debug "mail done!"
    

    That code is called from a sidekiq job. I can see in the rails logs the messages from the debug lines, confirming that this line of code is called. I also see the values of the sendmail_settings, confirming that the settings are the ones from development.rb.

    So, just for giggles, I changed the settings to this in development.rb:

    config.action_mailer.sendmail_settings = { :location => '/bogus/doesnt/exist' }
    

    Of course after every one of these changes I purge the webroot and completely reload the app, and restart my servers.

    Now I see the debug line print out the bogus config value, and still the mail(...) function is executing without any error. This is despite this

    config.action_mailer.raise_delivery_errors = true
    config.action_mailer.perform_deliveries = true
    

    being set in the config (and being confirmed as being set by debug lines). My mail templates are rendered and the whole thing works, but the sendmail command is never executed, or even tried.

    So then I looked at the source code in /var/lib/gems for both Mail and ActionMailer

    Action Mailer source

    ...and I see that the mail(...) function ends with just "m" - so it returns the Mail object. I could not see anywhere in the mail(...) function where the mail is actually sent.

    I added debug log lines to the send(...) function in the ActionMailer gem, and I see that in the logs. I added debug to the smtp deliver function in the Mail gem, and its never called.

    So I tried calling "deliver" on that returned object thus:

    outmail = mail(to: purchase.email, subject: 'Your key')
    outmail.deliver
    

    Now, my mail works. I have no idea what is going on here. If this is useful to anyone else I hope it helps. I have wasted a day of dev time on this, so I'm going to just take it as "it works" and check it in.

    0 讨论(0)
  • 2021-02-05 19:26

    Actually i have same problem when use rails 4 version. The solution above not working for me. So, i have try sidekiq mailer gem https://github.com/andersondias/sidekiq_mailer and get it work well!

    0 讨论(0)
  • 2021-02-05 19:27

    I found it, you have to run sidekiq like this:

    RAILS_ENV=production bundle exec sidekiq -D
    

    to run in production. It's the same issue as the assets precompile.

    0 讨论(0)
  • 2021-02-05 19:34

    For anyone that might benefit from this, I ran sidekiq like this:

    worker: bundle exec sidekiq -q default -q mailers -t 25 -c 3 ...

    on the Procfile using heroku

    0 讨论(0)
提交回复
热议问题