Reputation: 5840
I am developing a rails 3 app. When an email is sent, I want to save it to a database which is like a queue, and send the queued messages later from the database.
But I want to develop the actionmailer part in a normal way. I just want to catch the email message right after the view is rendered and before the email is delivered.
How can I accomplish this?
Thanks.
Sam
Upvotes: 3
Views: 2610
Reputation: 6525
Working from your comment to my previous answer, here's another approach. In Rails 3, ActionMailer makes it easy to capture an email as an object without sending it:
email = Notifier.account_activatation_instructions(current_user)
The returned object will have the subject, body, etc. as attributes. You can then pass this data on to your email queue database.
Upvotes: 2
Reputation: 6525
I realize that you want to catch the rendered email, and this is possible, but I think there's a simpler solution. Figure out what data your emails need. For example, let's say your messages require the recipient's email address and the recipient's full name. So, you create an ActiveRecord subclass (named, perhaps, ScheduledEmail
) to store these attributes. When you want to enqueue an email, just save a new record.
Then, you can have a cron script that calls a rake task. In the rake task, you can do something like this:
ScheduledEmail.all.each do |email|
Notifier.foo(email).deliver
email.destroy
end
Also, if you care to use them, there are a number of task scheduling plugins for Rails. Here's an article that mentions a few:
http://intridea.com/2009/2/13/dead-simple-task-scheduling-in-rails?blog=company
Upvotes: 0