My setup for running a drip email campaign in rails is to store each user's drip_stage
and when they were last_emailed_at
. And then I schedule a rake task for every ten minutes using the heroku scheduler. The rake task looks something this:
task :send_drip_email => :environment do
users = User.where(:last_emailed_at => Time.at(0)..Time.now)
users.find_each do |u|
UserMailer.send_drip(u, :stage => u.drip_stage)
u.update_attributes(:last_emailed_at => Time.now, :drip_stage => u.drip_stage + 1)
end
end
This way it distributes the emailing of all your users evenly, not all at once.
Find more about the heroku scheduler
https://devcenter.heroku.com/articles/scheduler
Another cool tip that I use is to store emails in the database. I made a model called Email
with a subject, body, and drip_stage
. And then I actually render the email inline so that I can access variables.
Note that this is highly insecure because database code is getting evaluated. You should never let your email creation interface be open to the public
All you have to do to render the email is
= render :inline => @email.body, :type => :haml