How to test mailers in Sinatra, Rails and Merb

Posted by Dan Sosedoff on May 03, 2010

Once you need to test mailers within your Sinara, Rails or Merb application you wish to see a real output. No need to setup delivery via SMTP. Just create an executable ruby script somewhere, for example: /usr/bin/fake-sendmail.sh with following content:

$ touch /usr/bin/fake-sendmail.sh 
$ chmod +x /usr/bin/fake-sendmail.sh  # make it executable (will require root priv.)
#!/usr/bin/ruby
path = "/tmp/fake-mailer"
Dir.mkdir(path) if !File.exists?(path)
File.open("#{path}/#{Time.now.to_i}.txt", "w") do |f|
    f.puts ARGV.inspect
    $stdin.each_line { |line| f.puts line }
end

And then configure your mailing system:

1. Sinatra

There is a plugin for Sinatra (ported from Merb, http://github.com/foca/sinatra-mailer),

Sinatra::Mailer.config = {:sendmail_path => "/usr/bin/fake-sendmail.sh"}
Sinatra::Mailer.delivery_method = :sendmail

2. Merb

Edit your development configuration file (config/environments/development.rb)

Merb::BootLoader.after_app_loads do
    Merb::Mailer.delivery_method = :sendmail
    Merb::Mailer.config = { :sendmail_path => '/usr/bin/fake-sendmail.sh' }
end

3. Rails.

Edit your development environment file with following options:

config.action_mailer.delivery_method = :sendmail 
config.action_mailer.sendmail_settings = {:location => "/usr/bin/fake-sendmail.sh"}
Trackbacks

Trackbacks are closed.

Comments

Comments are closed.