Using Sanitize Email to Preview HTML Emails Locally

John Trupiano has a great post to get you started with sanitize_email for Ruby on Rails.

I wanted to preview my HTML emails without having to fill up my online email inboxes with tons of email (and then I'd have to make filters too). I also didn't want to manage actually sending real email. So, I set up my machine for local delivery. Read on for instructions.

I am running Ubuntu 9.04, however these instructions should be fine at least back to 8.04.

Step 1 - Install Postfix

sudo aptitude install postfix

During the install, choose "Local Delivery Only"

Step 2 - Configure Evolution for Receiving Locally

Open up Evolution "Applications->Internet->Evolution Mail"

Set up a new account and choose local delivery. When it asks you for a path, put in:

/var/mail/<username>

Note that this path may not exist if you've never received mail.

Step 3 - Configure Sanitize Email

In your rails test environment.rb put:

config.action_mailer.delivery_method = :sendmail

Then, in the initializer where you set up sanitize email:

ActionMailer::Base.sanitized_recipients = ["@"] ActionMailer::Base.local_environments = %w( test )

If you need to know your username or hostname, just open up a terminal. The command "whoami" will give you your username, and "hostname" will give you the hostname.

Step 4 - Run your tests

Now, run your rails tests. You should have a test for every email you send, and that should trigger them to be sent locally. You may have to tell Evolution to Send/Receive. Keep in mind your tests will probably fail because they will expect email to be in test mode.

Step 5 - Set it back

Remember, when you're done previewing your email, reset config.action_mailer.delivery_method!