Understanding the Postfix content_filter Mechanism

The Postfix mail transfer agent (MTA) provides content_filter, a flexible mechanism designed to allow the integration of arbitrary mail filters through a well-defined interface. Postfix passes the message into the content filter at the queuing stage, which in the diagrams in the Postfix documentation is labelled qmgr, which is the name of the internal process that maintains queues for incoming and outgoing messages.

The simplest of the content_filter interfaces is a UNIX pipe, where Postfix writes to the filter's standard input and checks the filter's return value for pre-defined status codes. Mail is injected back into Postfix using the sendmail command, which is the Postfix replacement for the same Sendmail command. The example in the FILTER_README document that ships with Postfix uses cat and acts as a simple pass-through filter.

The best interface for sophisticated mail filtering is not the pipe mechanism, but rather the SMTP proxy mechanism. This provides a well-defined interface between Postfix and the filter and allows a persistent scanning daemon, which reduces overhead and allows the filtering proxy to deliver reliably. Because the SMTP proxy mechanism operates over TCP/IP, the content filter can be located on a separate host than the Postfix service that receives the mail.

The content_filter requires Postfix to connect to the proxy as an SMTP client, which handles the message and then connects back into a special SMTP server which handles the final routing and delivery of the message. As mentioned before, Postfix sends the message to the content filter at the queuing stage; this will be called the "sending process"; the message is re-injected into the Postfix system in the smtpd stage, which will be called the "receiving process."