Rails: Delayed Job --> Not picking up 'from' field when sending asynchronous mail

十年热恋 提交于 2019-12-06 06:39:37

问题


I'm running 2.1.1, Rails 3, and having a heckuva time getting the delayed_job gem working. If I strip out handle_asynchronously on a mailer, everything works fine...but if I put it back in, I get:

undefined method `name' for nil:NilClass (where 'name' comes from @contact.name ...which works fine when handle_asynchronously is disabled).

If I strip out all the @contact template info, I get:

"A sender (Return-Path, Sender or From) required to send a message"?

Is this me doing something wrong or some sorta bug? Relevant code below (my@email.here replaced with legit email address)

class ContactMailer < ActionMailer::Base
  default :from => "my@email.here"  

  def contact_mail(contact)
    @contact = contact
    mail(:to => ENV['MANAGER_EMAIL'], :subject => 'Delayed Job Test', :from => 'my@email.here', :content_type => 'text/plain')
  end

  handle_asynchronously :contact_mail, :run_at => Proc.new { 2.seconds.from_now }
end

Any suggestions very appreciated.


回答1:


Try calling the method with the actual email address:

def contact_mail(contact_email)
  mail(:to => ENV['MANAGER_EMAIL'], :subject => 'Delayed Job Test', :from => contact_email, :content_type => 'text/plain')
end

That's the only thing I can think of which might help without seeing your actual code. Your error says you're calling name on a nil object, but I can't see anywhere where you're calling .name...




回答2:


I had the same problem and solved it by removing this line:

default :from => "my@email.here" 

But I don't know why it crashed with this line..




回答3:


Rails 3 Mailers

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

https://github.com/collectiveidea/delayed_job#rails-3-mailers



来源:https://stackoverflow.com/questions/4314439/rails-delayed-job-not-picking-up-from-field-when-sending-asynchronous-mai

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!