Search code examples
ruby-on-railsrakeactionmailerrake-task

Rails 5.2 Rake Task Not Sending Mailer (Despite inner mailer working perfectly)


I have the following mailer set up in a rake task in lib/tasks/scheduler.rake:

  desc "Send capsule reminder emails"
  task send_capsule_reminders: :environment do
    require 'time'
    t = Time.now.in_time_zone("Pacific Time (US & Canada)").next_day
    month = t.month
    day = t.day
    Capsule.all.each do |capsule|
      if month == capsule.capsule_date.month && day == capsule.capsule_date.day && capsule.active && capsule.reminder_email
        NewCapsuleReminderMailer.send_capsule_reminder_email(capsule).deliver
      end
    end
  end

I have tested NewCapsuleReminderMailer.send_capsule_reminder_email(capsule).deliver from my console and it sends the mailer correctly.

I checked and month == capsule.capsule_date.month and day == capsule.capsule_date. Also capsule.active is true as is capsule.reminder_email.

However, when I run rake send_capsule_reminders nothing happens.

Can anyone help me figure out why this isn't sending and how to fix it?


Solution

  • I believe that NewCapsuleReminderMailer.send_capsule_reminder_email(capsule).deliver should be NewCapsuleReminderMailer.send_capsule_reminder_email(capsule).deliver_now.

    https://guides.rubyonrails.org/action_mailer_basics.html