Rails 3/delayed_job - 通缉:延迟邮件的基本示例

Pla*_*Ton 5 ruby-on-rails delayed-job

我一直试图找出如何使用rails 3使用delayed_job发送延迟邮件.我已经尝试了几乎所有可行的可能性组合 - 我可以让邮件在后台运行,我只是可以'让它延迟发送到未来的时间.数据库中的delayed_jobs表清除任务,日志显示"已发送",delayed_job任务处理器接收任务并说发送没有失败......但邮件是:

  • 立即发送,或
  • 根本没到

如果我试着将来发送.

如果有人能提供将来发送邮件的rails 3 delayed_job的简单例子,我真的很感激.我相信很多人这样做,所以我怀疑我错过了一些明显的东西.我在下面尝试过的一种(无数种)组合:

delayed_job:2.1.2 rails:3.0.3 actionmailer:3.0.3

控制器:

class TestmailerController < ApplicationController
  def index
    Testmailer.delay.test_mail
  end

end
Run Code Online (Sandbox Code Playgroud)

梅勒:

class Testmailer < ActionMailer::Base
  def test_mail
    mail(:to => '(myemailaddress@removedforprivacy.com', :from => '(removedforprivacy)@gmail.com', :subject => 'Testing Delayed Job', :content_type => 'text/plain').deliver
  end
  handle_asynchronously :test_mail, :run_at => Proc.new { 2.minutes.from_now }


end
Run Code Online (Sandbox Code Playgroud)

config/environments/development.rb的相关邮件部分:

  # Don't care if the mailer can't send
  config.action_mailer.raise_delivery_errors = true

  # Print deprecation notices to the Rails logger
  config.active_support.deprecation = :log

  config.action_mailer.default_url_options = { :host => 'localhost:3000' }

  ActionMailer::Base.smtp_settings = {
    :address => "smtp.gmail.com",
    :port => 587,
    :domain => "gmail.com",
    :user_name => "(removedforprivacy)",
    :password => "(removedforprivacy)",
    :authentication => "plain",
    :enable_starttls_auto => true
  }
Run Code Online (Sandbox Code Playgroud)

工作指令:

rake jobs:work
Run Code Online (Sandbox Code Playgroud)

小智 13

我同意andrea - 我遇到了这个问题,在将我的本地开发数据库从sqlite切换到mysql之后,我可以运行像

Emailer.delay({:run_at => 5.minutes.from_now}).welcome(@user)
Run Code Online (Sandbox Code Playgroud)

并在5分钟后发送电子邮件.请注意,您可能需要比五分钟更长的延迟(在时区怪异的情况下)以确保它正常工作.