dannymcc
dannymcc

Reputation: 3824

Delay email sending in Rails?

I am using delayed_job to delay sending information from my application to FreeagentCentral (an accountancy package). At the same time it also sends two emails using PostmarkApp's gem.

# POST /kases
# POST /kases.xml
def create
@company = Company.find(params[:kase][:company_id])
@kase = @company.kases.create!(params[:kase])

respond_to do |format|
UserMailer.delay.deliver_makeakase("[email protected]", "Highrise", @kase) if    params[:sendtohighrise]
UserMailer.delay.deliver_makeakaseteam("[email protected]", "Highrise", @kase) if params[:notify_team_of_creation]
@kase.delay.create_freeagent_project(current_user) if params[:send_to_freeagent]

#flash[:notice] = 'Case was successfully created.'
flash[:notice] = fading_flash_message("Case was successfully created.", 5)

format.html { redirect_to(@kase) }
format.xml  { render :xml => @kase, :status => :created, :location => @kase }
end
end

The above code doesn't throw any errors - but the emails don't get sent. I was wondering if I have done something wrong?

Should it be like this, or am I missing something:

UserMailer.delay.deliver_makeakaseteam("[email protected]", "Highrise", @kase) if params[:notify_team_of_creation]

Thanks,

Danny

UPDATE

--- 
- !ruby/object:Delayed::Backend::ActiveRecord::Job 
  attributes: 
    created_at: 2011-02-21 13:30:38
    locked_by: 
    failed_at: 
    updated_at: 2011-02-21 13:30:38
    handler: |
      --- !ruby/struct:Delayed::PerformableMethod 
      object: LOAD;UserMailer
      method: :deliver_makeakase
      args: 
      - [email protected]
      - Highrise
      - LOAD;Kase;19

    priority: "0"
    id: "1"
    run_at: 2011-02-21 13:30:38
    locked_at: 
    attempts: "0"
    last_error: 
  attributes_cache: {}

- !ruby/object:Delayed::Backend::ActiveRecord::Job 
  attributes: 
    created_at: 2011-02-21 13:30:38
    locked_by: 
    failed_at: 
    updated_at: 2011-02-21 13:30:38
    handler: |
      --- !ruby/struct:Delayed::PerformableMethod 
      object: LOAD;UserMailer
      method: :deliver_makeakaseteam
      args: 
      - [email protected]
      - Highrise
      - LOAD;Kase;19

    priority: "0"
    id: "2"
    run_at: 2011-02-21 13:30:38
    locked_at: 
    attempts: "0"
    last_error: 
  attributes_cache: {}

=> nil

Upvotes: 0

Views: 2354

Answers (1)

andrea
andrea

Reputation: 3505

from (collectiveidea) delayed_job docs

Due to how mailers are implemented in Rails 3, we had to do a little work around to get delayed_job to work.

without delayed_job

Notifier.signup(@user).deliver

with delayed_job

Notifier.delay.signup(@user)

Remove the .deliver method to make it work. It’s not ideal, but it’s the best we could do for now.

hope this could help

Upvotes: 1

Related Questions