Search code examples
ruby-on-railsrubyruby-on-rails-3cron

Rails cron not sending emails


I'm using the whenever gem to have a rails cron job send emails. Everything seems to work just fine and i have no errors in my cron.log or my production.log file, but i never receive an email. I've checked that the email address is correct also.

Any help is appreciated.

The production.log file contains this:

Connecting to database specified by database.yml
  Rendered email_mailer/send_birthday_reminders.html.erb (5.3ms)

Sent mail to [email protected] (409ms)

Here's my whenever gem schedule.rb file

set :output, "#{path}/log/cron.log"

every :hour do
    runner "BirthdayRemindersController.send_birthday_email_reminders"
end

birthday_reminders_controller.rb

class BirthdayRemindersController < ApplicationController

    # cron job that sends birthday reminders
    def self.send_birthday_email_reminders 
        users = User.all

        email_addresses = []

        users.each_with_index do |user, i|
            if user.user_details.birthday_reminders == true
                email_addresses[i] = get_primary_email(user)
            end
        end
        p "email_addresses to send to:"
        p email_addresses

        users.each do |user|
            p "this user is"
            p user.user_details.full_name
            if user.user_details.birthday.try(:strftime, "%m") == Date.today.strftime("%m") && user.user_details.birthday.try(:strftime, "%d") == Date.today.strftime("%d")
                p "reminder sent"
                EmailMailer.send_birthday_reminders(user, email_addresses).deliver
            end
        end
    end
end

email_mailer.rb snippet

class EmailMailer < ActionMailer::Base
  include ApplicationHelper
  default :from => "\"FamNFo\" <[email protected]>"

  def send_birthday_reminders(birthday_person, email_addresses)
    p "we in send_birthday_reminders mailer"
    p email_addresses
    @birthday_person = birthday_person
    mail(:subject => "Birthday Reminder For The Caflisch Family", :to => email_addresses, :reply_to => email_addresses)
  end
end

capistrano's deploy.rb contains this

# needed for the 'whenever' gem
set(:whenever_command) { "RAILS_ENV=#{rails_env} bundle exec whenever"}
require "whenever/capistrano"

Solution

  • Check your spam folder. To make sure emails don't end up there, add an "Unsubscribe" link in each email.