dshevc93
dshevc93

Reputation: 141

ruby on rails - send email in definite time

I want to realize next thing in my app: user want's that every day or every tuesday at 15:00 he get email from my app, and email sending time is controlled by the user. How can I do it in my app?

Thank's!

Upvotes: 2

Views: 173

Answers (2)

house9
house9

Reputation: 20614

You might want to use delayed_job or sidekiq. With delayed_job you can enqueue jobs to run in the future, so you could update the run_at to what the user specifies.

You will probably need to consider the timezone of the user, the delayed_job run_at will be UTC - I wrote a blog post on rails timezones if you are interested

Enqueue job with delayed job:

run_at = 'TODO: convert user specified params to UTC datetime'
job = SomeEmailJob.new(current_user.id)
Delayed::Job.enqueue(job, run_at: run_at)

Upvotes: 1

Amr Arafat
Amr Arafat

Reputation: 489

This is called a cron job where you specify a certain time to excute a certain action. For this you can use "Whenever" gem which is great with recurring tasks. With this gem you can write something like this

every :day, :at => '12:20am' do
  rake 'foo:bar'
end

For more details, check this out:

https://github.com/javan/whenever

Upvotes: 1

Related Questions