Reputation: 627
I'm trying to use whenever gem (or maybe there's a better solution ?) I've been trying to make this work, but I'm not sure if I'm doing it right.
Here is my schedule.rb :
every :day, :at => '12pm' do # Use any day of the week or :weekend, :weekday
runner "UserMailer.daily_mail"
end
My UserMailer :
def daily_mail(user)
@user = user
mail to: user.email, subject: "Mail journalier"
end
And in my user.rb :
def send_daily_mail
UserMailer.daily_mail(self).deliver
end
The thing is, I already have mail activation in my project, which is working (I can test it in heroku). The app sends a mail on creation of an account, so in UserController, there is :
def create
@user = User.new(user_params)
if @user.save
@user.send_activation_email
flash[:info] = "Veuillez contrôler votre boîte mail pour activer votre compte."
redirect_to root_url
else
render 'new'
end
end
But here, it sends an email after an action. But If I want the mail to be sent with whenever, what is left to do to make it work, and how can I test it since I'm not in production (and I think whenever doesn't work with heroku) ?
The endgame would be to have a checkbox in the user preferences, and he would be able to check daily, weekly or monthly updates, and the mails would be sent accordingly.
Thanks
Upvotes: 3
Views: 2599
Reputation: 9747
You should create a rake
task something like:
desc "Send email to users"
task :email_sender => :environment do |_, args|
User.find_each do |user|
UserMailer.daily_mail(user).deliver if <YOUR_LOGIC_TO_CHECK_IF_YOU_NEED_TO_SEND_EMAIL>
end
end
In config/schedule.rb
:
every :day, :at => '12pm' do
rake "email_sender"
end
Upvotes: 4