Reputation: 3529
I have a Rails app that I'm trying to create a drip email campaign for. Basically what I'm imagining is after someone signs up, I send them a specific email 2 days after signup, another a week after signup, and another a month after signup.
What's the best way to do this? Is there a gem that makes it easy, or some third-party email provider that does it well? I already use SendGrid to send email and looking through their API I didn't find anything that does exactly this. Thanks for any help!
Upvotes: 3
Views: 2002
Reputation: 8006
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
Upvotes: 2
Reputation: 5642
Delayed Job is probably your best bet:
https://github.com/collectiveidea/delayed_job
If you aren't able to run a separate worker process for some reason, then check out Faucet:
https://github.com/dshipper/Faucet
I'd highly recommend using a third party SMTP provider to send from instead of going through Postfix, otherwise you may have trouble with deliverability since most free email providers (Gmail, Yahoo Mail, Hotmail) all do filtering.
The best options in my opinion are Mailgun or Mandrill.
Upvotes: 0