Sidekiq和rails 4 actionmailer永远不会发送电子邮件

时间:2023-01-16 10:19:26

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 emailer. I am using gmail for sending emails.

我在rails 4应用程序中设置了sidekiq,用于发送电子邮件和处理后台作业。我还设计了我使用devise_async和典型的联系表单电子邮件。我正在使用Gmail发送电子邮件。

If I remove sidekiq, it sends the emails normally through gmail (both devise and contact form) but when I am enabling it, it doesn't work (neither devise_async neither contact form). Sidekiq shows that the background jobs starts and finishes successfully (I also see them through the sinatra app that it's been processed) but the email never been delivered.

如果我删除sidekiq,它通常通过gmail(设计和联系表单)发送电子邮件,但是当我启用它时,它不起作用(devise_async都不是联系表单)。 Sidekiq显示后台作业开始并成功完成(我也通过sinatra应用程序看到它们已被处理)但电子邮件从未发送过。

In development, the console shows that the emails sent (both with devise_async and from contact form, as they are processed with sidekiq successfully).

在开发过程中,控制台显示发送的电子邮件(使用devise_async和联系表单,因为它们已成功处理sidekiq)。

What I've already have try:

我已经尝试过的:

  • I've added the github branch for rails4 of sidekiq (I've also tried the master)
  • 我为sidekiq的rails4添加了github分支(我也尝过了主人)
  • I've updated and check redis version to be greater than 2.4 (it's 2.6.14)
  • 我已经更新并检查redis版本是否大于2.4(它是2.6.14)
  • I am running sidekiq with bundle exec sidekiq
  • 我正在使用bundle exec sidekiq运行sidekiq

but nothing worked. I am using ruby 2.0.0-p247.

但没有任何效果。我使用的是ruby 2.0.0-p247。

I have also setup image processing with sidekiq in this app and works perfectly both in development and in production.

我还在这个应用程序中使用sidekiq设置图像处理,在开发和生产中都能很好地工作。

I am not doing anything fancy but I am adding the code for completion:

我没有做任何花哨的事情,但我正在添加完成代码:

My mailer code:

我的邮件代码:

  def send_message
   @message = Message.new(message_params)
   if @message.save
     ContactMailer.delay.contact_form(@message.id)
   end
 end

My mailer:

我的邮件:

  def contact_form(message_id)
    message = Message.where(id: message_id).first
    @email = message.email
    @message = message.text
    mail(to: "myemail@gmail.com", subject: "Message from contact form")
  end

and my configuration for production (which it works without sidekiq):

和我的生产配置(没有sidekiq它的工作原理):

  config.action_mailer.delivery_method = :smtp
  ActionMailer::Base.smtp_settings = {
    :address              => "smtp.gmail.com",
    :port                 => 587,
    :domain               => "gmail.com",
    :authentication       => "plain",
    :enable_starttls_auto => true,
    :user_name            => "myusername",
    :password             => "mypassword"
  }
  config.action_mailer.default_url_options = { :host => "mydomain.com" }

And here a typical sidekiq queue:

这里有一个典型的sidekiq队列:

    /home/john/.rvm/gems/ruby-2.0.0-p247@cluey/bundler/gems/sidekiq-4ed6eba8aff1/lib/sidekiq/rails.rb:14: warning: toplevel constant Queue referenced by Sidekiq::Client::Queue
    2013-07-19T09:47:56Z 20112 TID-1a5mkc INFO: Booting Sidekiq 2.5.2 with Redis at redis://localhost:6379/0
    2013-07-19T09:47:56Z 20112 TID-1a5mkc INFO: Running in ruby 2.0.0p247 (2013-06-27 revision 41674) [i686-linux]
    2013-07-19T09:47:56Z 20112 TID-1a5mkc INFO: See LICENSE and the LGPL-3.0 for licensing details.
    2013-07-19T09:47:56Z 20112 TID-1a5mkc INFO: Starting processing, hit Ctrl-C to stop
    2013-07-19T09:48:11Z 20112 TID-1u2z02 Devise::Async::Backend::Sidekiq JID-4a7e66a14deab112191e4b49 INFO: start
    2013-07-19T09:48:12Z 20112 TID-1u2z02 Devise::Async::Backend::Sidekiq JID-4a7e66a14deab112191e4b49 INFO: done: 1.214 sec
    2013-07-19T09:48:50Z 20112 TID-1u2z02 Devise::Async::Backend::Sidekiq JID-32191822b789f5b6896a5353 INFO: start
2013-07-19T09:48:51Z 20112 TID-1u2z02 Devise::Async::Backend::Sidekiq JID-32191822b789f5b6896a5353 INFO: done: 0.143 sec
2013-07-19T09:49:29Z 20112 TID-1u2z02 Sidekiq::Extensions::DelayedMailer JID-c1911e0c4b72295dc067d57f INFO: start
2013-07-19T09:49:29Z 20112 TID-1u2z02 Sidekiq::Extensions::DelayedMailer JID-c1911e0c4b72295dc067d57f INFO: done: 0.152 sec

I think it has to do with actionmailer and sidekiq but I don't know how to debug, everything seems to work but the emails never delivered.

我认为它与actionmailer和sidekiq有关,但我不知道如何调试,一切似乎都有效但电子邮件从未发送过。

4 个解决方案

#1


15  

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

我找到了,你必须像这样运行sidekiq:

RAILS_ENV=production bundle exec sidekiq -D

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

在生产中运行。这与资产预编译的问题相同。

#2


2  

My setup is the same as this:

我的设置与此相同:

  • Rails 4
  • Rails 4
  • Ruby 1.9.3
  • Ruby 1.9.3
  • actionmailer
  • 的ActionMailer
  • sidekiq
  • sidekiq
  • devise_async
  • 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.

该代码是从sidekiq作业调用的。我可以在rails日志中看到来自调试行的消息,确认调用了这行代码。我还看到sendmail_settings的值,确认设置是来自development.rb的设置。

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

所以,只是为了咯咯笑,我在development.rb中将设置更改为this:

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.

当然,在每次更改之后,我清除webroot并完全重新加载应用程序,然后重新启动我的服务器。

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.

在配置中设置(并确认为由调试行设置)。我的邮件模板被渲染,整个过程都有效,但sendmail命令从未执行过,甚至没有尝试过。

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

那么我查看了Mail和ActionMailer中/ var / lib / gems中的源代码

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.

...我发现mail(...)函数只以“m”结尾 - 所以它返回Mail对象。我无法在邮件实际发送的邮件(...)功能中看到任何地方。

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.

我将调试日志行添加到ActionMailer gem中的send(...)函数中,我在日志中看到了这一点。我将调试添加到Mail gem中的smtp传递函数,并且从未调用过。

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

所以我试着在返回的对象上调用“deliver”:

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.

现在,我的邮件有效。我不知道这里发生了什么。如果这对其他人有用,我希望它有所帮助。我浪费了一天的开发时间,所以我将把它当作“它工作”并检查它。

#3


1  

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!

实际上我使用rails 4版本时遇到同样的问题。上面的解决方案不适合我。所以,我已经尝试了sidekiq邮件宝石https://github.com/andersondias/sidekiq_mailer并让它运作良好!

#4


0  

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

对于任何可能从中受益的人,我像这样运行sidekiq:

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

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

on the Procfile using heroku

在使用heroku的Procfile上

#1


15  

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

我找到了,你必须像这样运行sidekiq:

RAILS_ENV=production bundle exec sidekiq -D

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

在生产中运行。这与资产预编译的问题相同。

#2


2  

My setup is the same as this:

我的设置与此相同:

  • Rails 4
  • Rails 4
  • Ruby 1.9.3
  • Ruby 1.9.3
  • actionmailer
  • 的ActionMailer
  • sidekiq
  • sidekiq
  • devise_async
  • 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.

该代码是从sidekiq作业调用的。我可以在rails日志中看到来自调试行的消息,确认调用了这行代码。我还看到sendmail_settings的值,确认设置是来自development.rb的设置。

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

所以,只是为了咯咯笑,我在development.rb中将设置更改为this:

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.

当然,在每次更改之后,我清除webroot并完全重新加载应用程序,然后重新启动我的服务器。

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.

在配置中设置(并确认为由调试行设置)。我的邮件模板被渲染,整个过程都有效,但sendmail命令从未执行过,甚至没有尝试过。

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

那么我查看了Mail和ActionMailer中/ var / lib / gems中的源代码

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.

...我发现mail(...)函数只以“m”结尾 - 所以它返回Mail对象。我无法在邮件实际发送的邮件(...)功能中看到任何地方。

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.

我将调试日志行添加到ActionMailer gem中的send(...)函数中,我在日志中看到了这一点。我将调试添加到Mail gem中的smtp传递函数,并且从未调用过。

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

所以我试着在返回的对象上调用“deliver”:

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.

现在,我的邮件有效。我不知道这里发生了什么。如果这对其他人有用,我希望它有所帮助。我浪费了一天的开发时间,所以我将把它当作“它工作”并检查它。

#3


1  

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!

实际上我使用rails 4版本时遇到同样的问题。上面的解决方案不适合我。所以,我已经尝试了sidekiq邮件宝石https://github.com/andersondias/sidekiq_mailer并让它运作良好!

#4


0  

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

对于任何可能从中受益的人,我像这样运行sidekiq:

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

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

on the Procfile using heroku

在使用heroku的Procfile上