10

I'm trying to delay a notification email to be sent to users upon signing up to my app. The emails are sent using an ActionMailer which I call InitMailer. The way I am trying to delay the jobs is using collectiveidea's delayed_job https://github.com/collectiveidea/delayed_job. To do this you can see that i specify handle_asynchronously after defining the method initial_email:

class InitMailer < ActionMailer::Base
  default :from => "info@blahblahblah.com"

  def initial_email(user)
    @user = user
    @url = "http://www.blahblahblah.com"
    mail(:to => user.email,
         :subject => "Welcome to my website!"
         ) 
  end

  handle_asynchronously :initial_email
end

However, I encounter an argument error in my log file "delayed_job.log":

Class#initial_email failed with ArgumentError: wrong number of arguments (1 for 0) - 5 
failed attempts

For your information, the email is sent in a controller using the line:

@user = InitUser.new(params[:init_user])
InitMailer.delay.initial_email(@user)

Additionally, when I set up my code without the delay, the emails were sent out without problem (except for the fact that it slowed down my app waiting for gmail servers)

Where is causing the errors here? How can I get the delayed mail to send properly?

skaffman
  • 398,947
  • 96
  • 818
  • 769
geb2011
  • 461
  • 5
  • 15

1 Answers1

10

Due to the way that Rails3 implements mailers, there are some unusual workarounds for delayed_jobs. For instance, you have seen that to delay the mailing, you write

 ExampleMailer.delay.example(user)

While typically you would have to write handle_asynchronously after the method definition, in the case of mailers this declaration (for some reason) prevents that delayed job from working.

So in this code, drop the declaration entirely:

 class InitMailer < ActionMailer::Base
   default :from => "info@blahblahblah.com"

   def initial_email(user)
     @user = user
     @url = "http://www.blahblahblah.com"
     mail(:to => user.email,
          :subject => "Welcome to my website!"
          ) 
   end

   #No handle_asynchronously needed here
 end
Michael Geary
  • 28,450
  • 9
  • 65
  • 75
jay
  • 12,066
  • 16
  • 64
  • 103
  • Thank you! This is the answer to my troubles. Wish that I worked with someone like yoU! – geb2011 Jul 05 '11 at 18:41
  • Awesome. I just wasted 2 days trying to figure this out (also was a problem with an interceptor causing a failure and Gmail not wanting to accept emails from my development environment). – Clay Dec 24 '11 at 03:12
  • I'd use Mandrill for email sending instead of Gmail, even for development environment. – netwire Aug 28 '12 at 11:37