Rails comes with a useful library for generating emails called ActionMailer. ActionMailer has three delivery modes test, sendmail, and SMTP.

SMTP is what, by and large, everybody uses in production but who wants to setup a local SMTP server for development work? I mean you don't really want send the mail, just to know it would have been delivered. Even thinking about sendmail makes me shudder and the test delivery method just sticks the things in an array that you then need to deal with. That's fine in a test case but you don't want code for that in your app.

So what do you do? Well yesterday I mocked up the simplest, dumbest, Ruby SMTP server you can imagine. It's called Mailtrap and that's exactly what it does. It speaks just enough SMTP to allow ActionMailer to make a connection and send it a message. Mailtrap writes the message into a file for you (and you can tail -f the file if you're interested in seeing it).

Mailtrap has, essentially, zero configuration. By default it creates an SMTP server on localhost port 2525 that writes messages into /var/tmp/mailtrap.log. If you're happy with the default you can be up and running with:

  sudo gem install -y mailtrap
  mailtrap start

then just configure your ActionMailer for development (using RAILS_ROOT/config/environment/development.rb) settings as:

ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
  :domain => "",
  :address => "localhost",
  :port => 2525,

and you're good to go.

By default Mailtrap runs as a daemon so, when you're done with it, use:

mailtrap stop

to have it exit. If you want more control you can change the host, port, directory to write to, and so on as well as overriding daemonization. Check out the various config options in the docs.

Mailtrap was written specifically to talk just enough SMTP to allow ActionMailer to send it a message. I have no idea whether it's pidgeon-SMTP is usable for any other purpose. YMMV.

Released under an MIT license and with the assistance of Daemons, Trollop, Hoe, and Rubyforge.

04/10/2007 11:28 by Matt Mower | Permalink | comments:
More about: