Agile Web Development

Build it. Launch it. Love it.

Mail Queue

MailQueue
===========

Want to send out your mail asynchronously without having to touch any code that uses the Action Mailer?

With the plugin installed, all mail send from Mailer gets put into a table called QueuedMails. Here's my migration to add that table:

class AddQueuedMailTable < ActiveRecord::Migration
def self.up
create_table :queued_mails do |t|
t.column :object, :text
t.column :mailer, :string
end
end

def self.down
drop_table :queued_mails
end
end

Now everytime you call YouMailer.deliver_something(*params), that mail object will be stored in the QueuedMails table. Just periodically call script/runner from a cron job to process your new mail queue:

"script/runner 'MailQueue.process' -e production"

If you want to bypass the queue just called the deliver_method_name with an exclamation point at the end. Like:

YouMailer.deliver_something!(*params)

WARNING:

This feature to bypass the queue isn't the same way it was done in the original release of this plugin, so it isn't backwards compatible. Not a huge thing to change, and it probably wasn't a very popular thing used by people using this plugin anyways.

-Nate
Inkling Inc.
http://www.inklingmarkets.com

Vitals

Home http://code.google.com/p/mail-queue/
Repository http://mail-queue.googlecode.com/svn/mail_queue
License Rails' (MIT)
Tags Tag_red email
Rating (20 votes)
Owner Nate Kontny
Created 21 July 2006

Comments

  • Avatar
    9 March 2007

    Hello,

    In your code you ask: "Is there a better way to override this behavior but then get at it again from non-queue mode?"

    I would suggest the following:

    Create an alias for the original method

    alias deliver! non_queue_deliver!

    Overwrite it...

    def deliver!(mail = @mail) if queue

    # code...
    

    else

    non_queue_deliver!(mail)
    

    end end

    GJ for the plugin, its very simple and REALLY useful, since the web server won't get stuck while sending mails. ;)

  • Avatar
    Nate
    11 April 2007

    Yup, this was written awhile ago, and yes I would edit it with an alias now. Also someone brought to my attention that if this plugin is installed from subversion it will install as "mail-queue" in your vendor dir. You will have to rename it to "mail_queue" for this to work right.

    Soon, I should clean this thing up and host it at google's subversion repos.

    Thanks for taking a look at it and commenting on it.

  • Avatar
    Robetr
    30 May 2007

    Your plugin looks great, and I would love to use it. However, it fails for me in a special case: when I have e-mails with attachments, the TMail::Mail object is not unserialized correctly and ActionMailer fails to send it (It works fine when my e-mail is not serialized). I get this error msg: The error occurred while evaluating nil.empty? from /usr/lib/ruby/gems/1.8/gems/actionmailer-1.3.3/lib/action_mailer/vendor/tmail/mail.rb:94:in `accept'

        from /usr/lib/ruby/gems/1.8/gems/actionmailer-1.3.3/lib/action_mailer/vendor/tmail/mail.rb:131:in `with_multipart_encoding'
        from /usr/lib/ruby/gems/1.8/gems/actionmailer-1.3.3/lib/action_mailer/vendor/tmail/mail.rb:128:in `each'
        from /usr/lib/ruby/gems/1.8/gems/actionmailer-1.3.3/lib/action_mailer/vendor/tmail/mail.rb:128:in `with_multipart_encoding'
        from /usr/lib/ruby/gems/1.8/gems/actionmailer-1.3.3/lib/action_mailer/vendor/tmail/mail.rb:94:in `accept'
        from /usr/lib/ruby/gems/1.8/gems/actionmailer-1.3.3/lib/action_mailer/vendor/tmail/encode.rb:66:in `accept_strategy'
        from /usr/lib/ruby/gems/1.8/gems/actionmailer-1.3.3/lib/action_mailer/vendor/tmail/encode.rb:55:in `encoded'
        from ./script/../config/../vendor/plugins/mail_queue/lib/mail_queue.rb:24:in `deliver!'
    

    Have you experienced it? Any ideas of how to solve it? Thanx Robert

  • Avatar
    17 September 2007

    This plugin does not work. I am getting the same error as robetr, then I renamed the directory to mail_queue, and got another error. Then I added this to the plugins init.rb: require File.dirname(FILE) + '/lib/queued_mail.rb' And now I get this error (snippet): uninitialized constant ActionMailer::Base::QueuedMail

    /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:477:in `const_missing'

    {RAILS_ROOT}/vendor/plugins/mail-queue/lib/mail_queue.rb:13:in `deliver!'

    /usr/lib/ruby/gems/1.8/gems/actionmailer-1.3.3/lib/action_mailer/base.rb:333:in `method_missing'

    So... after fixing several bugs and it still not working, I gave up!

  • 25 October 2007

    I can confirm Robert's problem with attachments. I hacked together a simple replacement: http://gwyn.dezyne.net/page/mailtrain

  • Avatar
    14 December 2007

    I've moved the mail_queue to a google repos. The directory's name was changed to make installing the plugin a no brainer since it will install with the correct name now.

    I've tested a bit with attachments, even updated the tests in this plugin to send the Google logo as an attachment and it still works great as is. Can someone create a test case that I can add to this plugin that replicates where attachments don't work please?

    Gwyn, thanks for trying to help everyone out. A couple things I noticed though of the MailTrain is that the parameters you expect people to be using have to be simple Ruby types. I believe this won't work when you try to pass down say ActiveRecord objects down into the mail templates. I had this same problem trying to serialize stuff for another asynch plugin I've worked on.

    Also, I still prefer this plugins method of not making people alter their current code to have the joy of sending asynchronous mail. But it still includes a way to bypass the thing to send synchronously.

    This plugin seems like a pretty good concept so I'd love to be able to figure out with you guys' help where it doesn't work.

Add a comment