Rails 3 / delayed_job - Required: A basic example of expired mail

I tried to figure out how to send delayed mail using delayed_job with rails 3. I tried almost any combination of the possible possibilities that I can think of - I can make mail work in the background, I just can not make it delay sending in the future. The delayed_jobs table in db clears the tasks, the log says “sent”, the delayed_job task processor picks up the task and sends the message without fail ... but mail:

  • shipped immediately or
  • just doesn't come

if I try to send in the future.

If someone could offer an example from the bare side of rails 3 delayed_job, which sends mail in the future, I would be very grateful. I'm sure many people do this, so I suspect I'm missing out on something obvious. One of the countless combinations I've tried below:

delayed_job: 2.1.2 rails: 3.0.3 actionmailer: 3.0.3

Controller:

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

end

Mailer:

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

corresponding mail part config / environment / 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
  }

Job Command:

rake jobs:work
+5
source share
3 answers

I agree with andrea - I had this exact problem, and after switching my local development database from sqlite to mysql, I can run the code, for example

Emailer.delay({:run_at => 5.minutes.from_now}).welcome(@user)

5 . , , ( ), , .

+13

.delay handle_asynchronously :test_mail . .delay .

Testmailer.test_mail   # without .deliver due to a delayed_job issue

, , sqlite, run_at ( ), mysql2 .

+1

I found in Rails 3 with mongoid that removing the handle_asynchronously line allows it to work. I had all kinds of problems, and it turned out that delayed_job did not recognize any objects in my Emailer class. Removing handle_synchronously fixed.

+1
source

All Articles