Rails : how send an email at a specific date with Rails 7?

203 Views Asked by At

I have already read this thread (Send emails at specific times in Rails) which looks interesting but was written 8 years ago.

Here is my need : I need to send an email at a specific date (a user birthday for instance).

  • I would like to do it with Action Mailer, but can't find a way : is it possible ?
  • I don't know a lot about Active Job but seems to fit my needs : what do you think ?
  • What would you recommend ?

Thanks a lot, and have a good day.

Julien

1

There are 1 best solutions below

0
Ryan Kopf On

It is possible to send emails at a specific date using Action Mailer. To achieve this, you can use Active Job which is a framework for declaring jobs and making sure they run on either the same application or a remote one.

Active Job is a perfect fit for your current need. Active Job supports different job backends for queuing and processing jobs. Active Job will help you deliver your emails asynchronously, enabling you to set a delivery time in the future using job scheduling tools.

You'll need to install several things as well, like Sidekiq, to actually manage the job queue. Otherwise you'll lose the jobs when your server restarts.

Sometimes it's a good pattern to instead mark the email with a "send_on" field, and use a cron job to call a rake task to schedule upcoming emails with ActiveJob or to send the emails directly. This is because if your job queue gets corrupted, you could lose all saved pending emails.