Skip to main content

How can I send emails 30 days before the time limit in rails?

Rails • Asked by Francisco Muñoz
I have a system that can send email every time I create a post, but I also need to notify 30 days before the expiration date through email, as you may know, the expiration date is variable and I could not find a way to solve this problem. Thank you in advance!

The easiest way is to run a cronjob to check the expiration date is in 30 days. If so, it sends an email. 
Let us know if you have specific issues implementing such thing.

Yes, I have implemented cron jobs before, but the most complicated, I think, is the condition, the calculation that must be done to send emails 30 days before the expiration date, if there is an example or some documentation a little more detailed, I would appreciate if you could share it.

P.D .: I have used gems like delayed_jobs and whenever to use cron jobs, and I have implemented them well.
In conclusion, I would like to know which is the optimal and precise way to solve this problem, which is basically the condition to send the emails 30 days before the deadline, thank you very much for your response.


Hey Francisco, 

As Jack stated, you just need a cron job to check for records that expire in 30 days or less. So on your Post model you would store an expiration date or just check the created_at timestamp and then in your cron you could check for all records that have an expiration date that is less than 30 days from now with a scope. Eg:

scope :expires_in_30_days, -> {where("expiration_date < ?", Time.now + 30.days)}

Post.expires_in_30_days.find_each do |post|
  Post.send_expiration_email
end



Nothing more to add to above post from Jacob! 🙂

Thank you very much both of you! love you all!


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 22,346+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.