Rails 3 / delayed_job-通缉:延迟邮件的基本示例 [英] Rails 3/delayed_job - Wanted: Basic example of delayed mail
问题描述
我一直在尝试找出如何使用带有rails 3的delay_job发送延迟的邮件。我已经尝试了几乎所有可以想到的可行组合-我可以让邮件在后台运行,我只是无法让它延迟发送到将来的时间。 db中的delay_jobs表清除了任务,日志显示已发送,delayed_job任务处理器选择了任务&说发送没有失败...但是邮件是:
I've been trying to figure out how to send delayed mail using delayed_job with rails 3. I've tried pretty much every combination of feasible possibilities I can think of - I can get the mail to run in the background, I just can't get it to delay the sending to a future time. The delayed_jobs table in the db clears the tasks, the log says 'sent', the delayed_job task processor picks up the task & says sent without failure...but the mail is either:
- 立即发送,或
- 如果我以后尝试发送邮件,就不会到达
。
如果有人能提供将来发送邮件的rails 3 delay_job的简单例子,我将非常感激。我确定很多人都会这样做,所以我怀疑我缺少明显的东西。我尝试过以下一种(无数)组合:
If anyone could offer a bare-bones example of a rails 3 delayed_job that sends mail in the future, I'd be really appreciative. I'm sure lotsa folks do this so I suspect I'm missing something obvious. One (of countless) combinations I've tried below:
delayed_job:2.1.2
滑轨:3.0.3
actionmailer:3.0.3
delayed_job: 2.1.2 rails: 3.0.3 actionmailer: 3.0.3
控制器:
class TestmailerController < ApplicationController
def index
Testmailer.delay.test_mail
end
end
邮件程序:
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
config / environments / development.rb的相关邮件部分:
relevant mail part of 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
}
作业命令:
rake jobs:work
推荐答案
我同意andrea-我遇到了这个确切的问题,将本地开发数据库从sqlite切换到mysql之后,我可以运行类似的代码
I agree with andrea - I was having this exact problem, and after switching my local development database from sqlite to mysql, I can run code like
Emailer.delay({:run_at => 5.minutes.from_now}).welcome(@user)
,并在5分钟后发送电子邮件。请注意,您可能需要大于五分钟的延迟(以防时区怪异),以确保其正常工作。
and it sends the email 5 minutes later. Note that you might need a bigger delay than five minutes (in case of time zone weirdness) to be sure it is working.
这篇关于Rails 3 / delayed_job-通缉:延迟邮件的基本示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!