Dealing with email looks simple in theory, but becomes more and more complex the more you look at it. Postel's Law — be conservative in what you do; be liberal in what you accept from others — seems to have been ignored by many when it comes to email.
Many implementations seemingly only follow parts of the specifications. At the same time, tools for developers who need to interface with email haven't really changed in the last 10-15 years. The result: People making the same mistakes again and again.
This is where the Lamson project comes in. It's written by Zed Shaw, author of the Mongrel server made famous by Ruby on Rails. Lamson's design is model view controller (MVC) based, making it very similar to the newer web frameworks popular with developers these days, such as Ruby on Rails and Django.
The standard setup involves having one of the standard mail servers in charge of basic sending and receiving of emails and letting Lamson deal with the content and destination.
Coding in Lamson is actually quite simple. Begin by creating a new handler and coding up some behaviour for it. Handlers start by defining the route that they match and finish by returning a state.
A regular expression is used in the config file to define what parts, such as listname or host, should look like. This makes for routes that are easier to maintain than giant regular expressions.
def START(message, listname=None, host=None):
... process handling
You have noticed that the function in the example above ended by returning "POSTING". All the processing functions in the handler return the name of another function. This is because Lamson uses finite state machines to represent the status of email senders, allowing you to build complex paths for email systems relatively easily.
Another advantage of Lamson is the ease with which emails can be put in or taken out of the system. By default, Lamson can talk SMTP and use a Maildir as a queue.
There is a simple API to extend if you need other methods. For example, this would allow for a queue in a SQL to interface with another application, or legacy code.
As Lamson is written in Python, it has out of the box support for several handy features, including easy access to different storage back-ends (things like Django ORM, SQLAlchemy, or CouchDB).
There is also support for Jinja2 and Mako, which is good for email templating as required. The architecture of Lamson also makes it easy to do things such as templating in the Django template language.
Lamson is a great framework for any projects that involve processing emails, whether they're incoming, outgoing or both.
The sample applications provided with Lamson provide good examples of how to interact with it and are quite useful in themselves.
The mailing list (librelist) and the mail cleaner (myinboxisnotatv) in particular show how easy it is to implement some more complex functionality with simple code.
This story first appeared in the October/November print edition of Computerworld