How to make ActionMailer deliver all mail locally

words by Brian Racer

This is similar to my article on forcing PHP to deliver all mail locally, except this will focus on Action Mailer.

When doing local development we generally don’t want our test and development servers sending out mail to the world. And it would be ideal to be able to review the emails our application sends out before deploying the changes to the world. An easy way to achieve this functionality is to create a custom script that rewrites the outgoing message, and then passes that on to the local MTA such as sendmail or exim.

On my Ubuntu development machine I prefer to use exim over sendmail, and mutt to review the emails:

sudo apt-get install exim4 mutt

Next create the following script that will rewrite any messages generated by ActionMailer to the local user of your choice:

vi /usr/local/bin/trapmail
formail -R cc X-original-cc \
-R to X-original-to \
-R bcc X-original-bcc \
-f -A"To: username@localhost" \
| /usr/sbin/sendmail -t -i

Replace username@localhost with your local username or an external email address.

Now setup your development environment configuration file:

config/enviroments/development.rb

ActionMailer::Base.delivery_method = :sendmail
ActionMailer::Base.sendmail_settings = {
  :location       => '/usr/local/bin/trapmail',
}

Now all messages will be redirected to the local user.