I need to send a series of emails to new user signups as part of our onboarding process.
Since I’m already using Delayed Job for other tasks in the app I initially thought of firing a bunch of delayed calls to my mailer with the
run_at param set to the required time periods but this looks quite odd to me in the
User controller (example):
def create @user = User.new(params[:user]) MyMailer.delay(run_at: 1.day.from_now).send_email('one', @user) MyMailer.delay(run_at: 2.days.from_now).send_email('two', @user) MyMailer.delay(run_at: 5.days.from_now).send_email('three', @user) MyMailer.delay(run_at: 10.days.from_now).send_email('four', @user) if @user.save redirect_to dashboard_path else render action: "new" end end
I realize I could extract the mailer calls out to a new method or use a service object but I’m more concerned about this being a reasonable way to queue emails for timed delivery. Is anyone here using this method?
[Bonus Question!] If this does seem like a good method then how would I cancel outstanding emails if I needed to? A new user could cancel their account during the first 10 days and since the delayed jobs exist as independent Active Record objects they wouldn’t automatically be deleted when a User account is destroyed. I wouldn’t want to annoy cancelled users by sending them the remainder of their onboarding emails.