Looks like my emails raising errors and sending.
I’m receiving emails and also I get this error on “rake jobs:work” console:
Class#deliver_and_save failed with ArgumentError: A sender (Return-Path, Sender or From) required to send a message – 1 failed attempts
But when I step through my debugger, I can see that there is a from:
> email.from
=> ["my@email.com"]
> email_draft.From
=> Me <my@email.com>
> email_draft.sender
=> nil
> email_draft.Sender
=> nil
> email_draft.reply_to
=> ["my@email.com"]
Is it possible that ActionMailer is throwing the error and still sending? Is this a known issue? The problem I have is that delayed_job keeps sending the email repeatedly.
UPDATE:
def EmailEngine < ActionMailer::Base
# Called with EmailEngine.delay.deliver_and_save(template)
def deliver_and_save(template)
# This appears to be raising the error
email_draft = EmailEngine.send(template) # this will invoke 'mail'
# Saved here to have a better record than the logs provide
Email.create(...
# Yet this still sends
email_draft.deliver
Found the problem!
delayed_job doesn’t play super well with rails 3.1 and you have to use this new syntax:
which it automatically tries to calls
.deliveron the Mail::Message object thatdeliver_and_savereturns. (So there’s my problem: trying to call.delivermyself.)SOLUTION: I rearranged my code and to call
Email.create(..somewhere outside ofEmailEngineand then I calledEmailEngine.delay.send(template)directly and completely removeddeliver_and_save.Woo!
Btw, any method invoked on a class that inherits from ActionMailer will return a Mail::Message object. ActionMailer does some black magic to make this happen (overwriting Ruby’s
method_missingso it can render views just like a controller, even though it behaves like a model in many ways).