I recommend setting up a cron that will run once every hour and send out e-mails. You'll have to create a new model, QueuedEmail
or something similar, and then instead of using ActionMailer right away, save it as a QueuedEmail
. This is basically the same thing that delayed_job does, but you'll have more control over when it gets run, and crons don't take up much memory.
The cron should be to script/rails runner, and you should have a method in your QueuedEmail
model that will send out all pending e-mails. I recommend whenever to generate crontabs (quick to setup and very easy to use). The cron will look something like this (this example is set to run once a day at 2am, you can look up how to adjust the intervals, I don't know off the top of my head):
0 2 * * * /bin/bash -l -c 'cd /RAILS_ROOT/ && script/rails runner -e production '\''QueuedEmail.send_pending'\'' >> log/cron.log 2>&1'
Or in Whenever:
every :day, :at => '2 am' do
runner "QueuedEmail.send_pending"
end
QueuedEmail#send_pending
class QueuedEmail < ActiveRecord::Base
def send_pending
all.each do |email|
params = your_parsing_method(email.params)
record = your_parsing_method(email.record)
email.destroy if UserMailer.registration_confirmation(record, params).deliver
end
end
end
UserController#create
if @user = User.create(params[:user])
User.delay_email(@user, params[:user])
redirect_to user_path(@user), :notice => "Created User. E-mail will be sent within the hour."
end
User#delay_email
def delay_email(record, params)
QueuedEmail.create(:record => your_db_formatting_method(record), :params => your_db_formatting_method(params.to_s)) # or use something built in, like to_s.
end
None of this code is tested and is probably quite broken, but it's the general idea that matters. You could also go one step further and extend the Rails ActionMailer, but that is far more advanced.