Wrestling with Postfix: Mail Delivery Agent

webhat
3 min readMay 6, 2015

--

Postfix is a mail server first written as an alternative to Sendmail. Rather than being a monolithic program which does everything, it has a Unix philosophy:

Write programs that do one thing and do it well.
Write programs to work together.
Write programs to handle text streams, because that is a universal interface.

This means that every component can be replaced with the custom component you need for your specific project.

The first time I started working with Postfix was in 2000, we built a custom mail delivery agent to store all the mail in an LDAP database (Directory Server) so it could easily be distributed over a large number of back-end servers.

The primary advantage of storing it in a replicating database rather than on disk means that it’s easy to add more front-end servers for receiving mail and easier to add more back-end servers to store mail while preserving redundancy.

Another is that LDAP is ideally suited the for the kind of authentication and authorisation needed for compartmentalizing the mail.

There are, of course, a host of other reasons.

Run Your Own Mail Delivery Agent

Running your own mail delivery agent is relatively simple, in the master.cf add a line that points to your local delivery agent. You can see an example for Postfix’s maildrop below:

maildrop unix - n n - - pipe flags=DRhu user=vmail
argv=/usr/bin/maildrop -d ${recipient}

You can see in the configuration that it adheres to all three of the Unix philosophy items mentioned above, most important it handles text streams with pipe. The raw mail message comes in from standard in, much in the same way it is delivered to the SMTP server:

<header>
<empty line ending in a line-feed>
<body>

MIME-Version: 1.0
Received: by 10.202.173.200 with HTTP; Tue, 2 May 2015 14:52:30 -0800 (PST)
X-Originating-IP: [2002:983:5d9f:1:90eb:cd82:435f:4425]
Date: Tue, 2 May 2015 23:52:30 +0100
Delivered-To: alice@example.com
Message-ID: <CAGeHRozYA@example.com>
Subject:
From: Alice <alice@example.com>
To: bob@example.com
Content-Type: text/plain; charset=UTF-8

Hi Bob,
Thanks for mailing.Alice

Among other things, flags determine what additional headers Postfix will add to the mail. It will also add additional command line variables which can be passed to your tool.

In argv= you add your own deliverer and pass any of the preprocessed variables to the command, such as the ${recipient}, ${user} or ${domain}, as well as some arguments that are commonly used (-d for deliver to, -a for authenticated, etc.)

Now you write the code for your custom mail delivery agent.

Sending mail to your Mail Delivery Agent

Once you have your deliverer command ready you need to direct the mail there, the easiest way to do this is to add a virtual_transport to your main.cf.

virtual_transport = maildrop            # This name needs to match
# the master.cf entry
virtual_mailbox_domains = example.com

This will redirect all the mail that is sent to Postfix with the domain @example.com to your mail delivery agent.

Want to work on cool stuff like this?

Join Oplerno as a Developer

By Daniël W. Crompton (@webhat) at Oplerno — a global institution empowering real-world practitioners, adjunct lecturers, professors, and aspiring instructors to offer affordable, accessible, high-quality education to students from all corners of the globe.

http://www.postfix.org/
http://en.wikipedia.org/wiki/Unix_philosophy

--

--

webhat

Former Security Consultant. Developer with a love of Education, Mashups and Folksonomy. Serial Entrepreneur. (+31646783584) Tech @Oplerno and @HigherEdRev