ActionMailer in rake task

1,355 views
Skip to first unread message

Sven Schott

unread,
Jul 30, 2010, 2:36:45 AM7/30/10
to rails-...@googlegroups.com
Hi guys

I'm trying to use ActionMailer to send notifications outside of my controllers from a rake task but I'm getting an error saying the method is undefined.

NoMethodError: undefined method `hour_record_expiry_notification' for Notifier:Class

The ActionMailer class looks like this:

class Notifier < ActionMailer::Base
  ADMIN_ADDRESS = "XXXXX@XXXXX"
  def hour_record_expiry_notification(recipient,hour_record)
    recipients recipient.email_address_with_name
    from       ADMIN_ADDRESS
    subject    "Freetime: WARNING - Hours expire in 10 days or less"
    body       :user => recipient, :hour_record => hour_record
    content_type "text/html"
  end
end

and the rake task looks like this:

namespace :freetime do
  desc "Find hours that will expire soon and notify the owners"
  task :notify_expiry => :environment do
    a = User.find(5);b = HourRecord.find(129)
    Notifier.hour_record_expiry_notification(a,b)
    Rails.logger.info "Sent users expiry notifications."
  end
end

Notifier.instance_methods shows the method but I'm thinking there's some tricker involved since it isn't an instance. :)

I've googled around but I can't find anyone having the same problem which leads me to believe I'm doing something stupid or nobody does mail notifications with rake (This is a one off that runs from a cron job). Any ideas? I normally would crack my head a bit more but it's 4:30 and if I go home without solving it, I'll have to use the old shotgun.

Keith Pitt

unread,
Jul 30, 2010, 2:41:26 AM7/30/10
to rails-...@googlegroups.com
Hi Sven,

Try:

Notifier.deliver_hour_record_expiry_notification(a,b)

Cheers,

Keith

--
You received this message because you are subscribed to the Google Groups "Ruby or Rails Oceania" group.
To post to this group, send email to rails-...@googlegroups.com.
To unsubscribe from this group, send email to rails-oceani...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rails-oceania?hl=en.



--
Keith Pitt
Web: http://www.keithpitt.com
Twitter: @keithpitt
Skype: keithpitt
Phone: +61 432 713 987

Dan Cheail

unread,
Jul 30, 2010, 2:42:04 AM7/30/10
to rails-...@googlegroups.com
Which veresion of ActionMailer are you using? 2.x or 3.x?

If you're using 3.x, you need to call .deliver after the mailer method name:

Notifier.hour_record_expiry_notification(a, b).deliver

If you're using 2.x, it's slightly different:

Notifier.deliver_hour_record_expiry_notification(a, b)

- Dan

--
You received this message because you are subscribed to the Google Groups "Ruby or Rails Oceania" group.
To post to this group, send email to rails-...@googlegroups.com.
To unsubscribe from this group, send email to rails-oceani...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rails-oceania?hl=en.



--
Dan Cheail
Big Geek,

Sven Schott

unread,
Jul 30, 2010, 2:44:46 AM7/30/10
to rails-...@googlegroups.com
Ah sorry. Very late in the day. Thanks heaps.

sivakumar ganesan

unread,
Jun 21, 2013, 4:11:25 PM6/21/13
to rails-...@googlegroups.com
I'm planning to write the rake task to query few details from the model and sending the notification using ActionMailer object

I'm getting below error when I invoke the action mailer class in my rake task,

rake aborted!
#<NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]>
C:/Rails application/TEST/lib/tasks/notifier.rak

My code:
notifier.rake file under lib/tasks

task :notify => :environment do 
   begin

   **************
Notifier.deliver_submitted_list_notification(email_settings[RAILS_ENV], records )
end

notifier.rb model/notifier.rb
class Notifier < ActionMailer::Base
def submitted_list_notification(settings,records)

**********
end
end

Any help would be appreciated!!

Nigel Sheridan-Smith

unread,
Jun 22, 2013, 2:49:29 AM6/22/13
to rails-...@googlegroups.com

Are you declaring your notification method as an instance method or a class method? 

def instance_method(params)
  ...
end

notifier = Notifier.new
notifier.instance_method(params)


OR

def self.class_method(params)
  ...
end

Notifier.class_method(params)




Cheers,

Nigel



--
You received this message because you are subscribed to the Google Groups "Ruby or Rails Oceania" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rails-oceani...@googlegroups.com.

To post to this group, send email to rails-...@googlegroups.com.



--
 
Get together for fun activities at www.joinsomeone.com
 
Like us on Facebook www.facebook.com/JoinSomeone
Follow us on Twitter @JoinSomeone

Malcolm Locke

unread,
Jun 22, 2013, 4:58:18 AM6/22/13
to rails-...@googlegroups.com
Hi,

Try running your rake task with --trace so you can find out which line
of your code is raising that error.

Malc
Reply all
Reply to author
Forward
0 new messages