Every repository with this icon (
Every repository with this icon (
| name | age | message | |
|---|---|---|---|
| |
MIT-LICENSE | Sun Jul 20 15:50:02 -0700 2008 | |
| |
README.markdown | Tue Oct 06 05:29:15 -0700 2009 | |
| |
init.rb | Mon Oct 05 15:11:26 -0700 2009 | |
| |
lib/ | Tue Oct 06 11:09:23 -0700 2009 | |
| |
spec/ | Tue Oct 06 11:09:23 -0700 2009 |
Mr. Postman
Send e-mail in real time is the dumbest thing you can do. This Ruby on Rails plugin creates a database queue so you can deliver mail on the background.
Mail Queue replicates several TMail attributes on the database while Mr. Postman saves a Marshaled TMail object. The advantage is that you can queue mails with attachment and multipart mails.
Mr. Postman will replace Mail Queue some time in the future. The queue method didn't change; so you can easily replace the old version by this new one! Keep in mind that Mail Queue won't be maintained anymore. Check it out the Troubleshooting section to know how to migrate.
INSTALATION
1) Install the plugin with script/plugin install http://github.com/fnando/mr-postman.git
2) Generate a migration with script/generate migration create_mails and add the following code:
class CreateMails < ActiveRecord::Migration
def self.up
create_table :mails do |t|
t.binary :mail, :limit => 4.megabytes
t.boolean :locked, :default => false, :null => false
t.integer :priority, :default => 3, :null => false
t.integer :maximum_tries, :default => 3, :null => false
t.integer :tries, :default => 0, :null => false
t.timestamps
end
add_index :mails, :locked
add_index :mails, :priority
add_index :mails, :tries
add_index :mails, :maximum_tries
add_index :mails, [:created_at, :priority]
end
def self.down
drop_table :mails
end
end
3) Run migrations with rake db:migrate
USAGE
- Create your mailer using
script/generate mailer <name> - To queue a message, call
Mail.queue(<tmail>)or<Mailer>.queue(<method>, <*args>) - To deliver the messages, call
Mail.process(:limit => <limit>)
SAMPLE
Generate a mailer called UserNotifier by running script/generate mailer UserNotifier.
Here's what it looks like:
class UserNotifier < ActionMailer::Base
def activation(user)
subject "Confirm your subscription"
recipients user.email
from "mail@cool-app.com"
body :user => user
cc "cc-recipient@cool-app.com"
bcc "bcc-recipient@cool-app.com"
end
end
# you can queue a TMail
Mail.queue(UserNotifier.create_activation(@user))
# or just call the method queue
UserNotifier.queue(:activation, @user)
# the last argument can be a hash with the following options
UserNotifier.queue(:activation, @user, :priority => 0, :tries => 5)
# to provide a hash as argument you have to separate the options
UserNotifier.queue(:activation, {:user => @user}, {})
# To deliver queued mails you should
# use the `Mail.process` method
Mail.process(:limit => 300)
You can run this Mail.process method on a background job. I use the simple-daemon
gem with something like this:
require File.join(File.dirname(__FILE__), "..", "..", "config", "environment")
require "simple-daemon"
class MailDaemon < SimpleDaemon::Base
SimpleDaemon::WORKING_DIRECTORY = "#{RAILS_ROOT}/tmp"
def self.start
loop do
Mail.process
sleep(30)
end
end
def self.stop
end
end
MailDaemon.daemonize
TROUBLESHOOTING
MySQL Errors
If you're getting packet too large or server has gone away errors, read this post:
http://skipmeamadeus.blogspot.com/2008/01/in-which-i-try-to-save-someone-from.html
Delivery Errors
If you're getting [Mr Postman] Error when trying to deliver message: marshal data too short
try to slightly increase the BLOB field.
create_table :mails do |t|
t.binary :mail, :limit => 5.megabytes
end
Migration
To migrate from mail_queue plugin you need to process all your queued mail and
create a new migration. Run script/generate migration MigrateToMrPostman:
class MigrateToMrPostman < ActiveRecord::Migration
def self.up
# Remove old table
drop_table :mails
# Create table and add indices
create_table :mails do |t|
t.binary :mail, :limit => 4.megabytes
t.boolean :locked, :default => false, :null => false
t.integer :priority, :default => 3, :null => false
t.integer :maximum_tries, :default => 3, :null => false
t.integer :tries, :default => 0, :null => false
t.timestamps
end
add_index :mails, :locked
add_index :mails, :priority
add_index :mails, :tries
add_index :mails, :maximum_tries
add_index :mails, [:created_at, :priority]
end
def self.down
# Remove table
drop_table :mails
# Recreate old table
create_table :mails do |t|
t.string :subject, :from, :to, :cc, :bcc, :charset, :content_type
t.text :body, :data
t.boolean :locked, :default => false, :null => false
t.integer :priority, :default => 3, :null => false
t.integer :tries, :default => 0, :null => false
t.integer :maximum_tries, :default => 3, :null => false
t.timestamps
end
add_index :mails, :locked
add_index :mails, :priority
add_index :mails, :tries
end
end
Run rake db:migrate and you're good to go!
MAINTAINER
- Nando Vieira (http://simplesideias.com.br)
LICENSE:
(The MIT License)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Copyright (c) 2007-2009 Nando Vieira, released under the MIT license







