Send it hence

Leave a comment

May 23, 2013 by Rob Nichols

Application development: an email strategy

Summary
Configure applications to send all development emails to developer@localhost via SMTP.

Background

Many applications require the facility to send out emails, and there are many ways of achieving this. However, when a team of developers are involved in creating and maintaining those applications, it is worth defining a strategy that all can use to safely test the email system within the development environment.

Use standard protocols

It is fairly easy to set up an SMTP server on any OS, and so to set up one in any development environment. Therefore, it is easy to set up a development system that will use the same protocols as those used in the production environment. This means that as far as the application is concerned, all that changes between development and production is the address of the SMTP server.

Keep development emails local

Here are three good reasons to keep the emails local:

  1. Remove dependence on network infrastructure when testing emails, thereby ensuring that the application’s email mechanisms are being tested rather than the efficiency of the network
  2. Reduce the likelihood that test emails will get out of the development environment.
  3. Give control to the developer as to how the emails are handled.

.
Use a simple generic mailbox name: developer

If the application framework provides a mechanism to forward all development emails to a single email mailbox, use a generic mailbox name. If you do not use a generic mailbox name, each developer working on the application code will need to change that code to work with their own mailbox. ‘developer’ is an obvious descriptive generic mailbox name.

The developer can choose how to set up their local mail server, and can either create a developer mailbox, or use aliasing to allow another mailbox to retrieve developer emails.

Configure the server to send all emails to a single mailbox

If the application framework does not provide a mechanism to forward all mail to a single mailbox, the local server can be configured to forward all incoming mail to a single mailbox.

Developers control how the emails are handled via the configuration of their local mail server

By using a local SMTP server, each developer can choose to handle the emails in the way that suits them. For example, they can choose to forward the emails to their usual provider, or they can configure an email client to access the local server. The key point being that the configuration they use modifies the local SMTP server, and not the application code.

A Rails example

In this blog post, I describe how I set up my own system to receive developer@localhost emails. Below are the configuration changes, that I used to get a rails app to work with this set up. This code can be used as a template for any rails 3 app:

Added this to /config/environments/development.rb:


config.action_mailer.smtp_settings = {
:address => "localhost",
:port => 25,
:domain => 'localhost',
:authentication => 'none',
:openssl_verify_mode => 'none'
}

And added this to the end of that file


class OverrideMailReciptient
def self.delivering_email(mail)
mail.to = "developer@localhost"
end
end
ActionMailer::Base.register_interceptor(OverrideMailReciptient)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: