Creating a Postfix Auto Responder in Perl


There are many reasons! Basically, you would do this if you have a need to intercept and process incoming email. In my case, I wanted to be able to send out my resume in response to a very specific type of email request.



You will need to have Postfix up and running on a server. You'll also need administrative access to the server, namely the ability to modify Postfix's configuration. Make sure to take your time and identify the many ways that hooking in to a suid'd daemon can expose your box to massive security holes.

I'll assume that your Postfix is located at /etc/postfix. We'll need to operate on several files. If a file does not exist, create it. Otherwise assume it is safe to append the new data.

Postfix Setup

  1. Create a new alias for which all incoming messages will get passed to our new hook. The file to edit is /etc/postfix/virtual_addresses,
    The above code creates the alias 'request', which will eventually be delivered to 'username+request' (any real account) and an address on a new subdomain.

    This subdomain is the key. It will allow us to target specific messages. It does not have to be a real subdomain.
    When you are done, execute postmap /etc/postfix/virtual_addresses to generate the hashed file for postfix.

  2. Map the transport layer through which Postfix should pass incoming mail directed to our new alias. The file to edit is /etc/postfix/transport myhook:
    This tells postfix that all mail to '' should be passed through the 'myhook' layer (which we'll get to in a bit). Again, run postmap to generate the hash file postmap /etc/postfix/transport
  3. Add the transport layer definition to Postfix. Add the following line to /etc/postfix/
    myhook unix - n n - - pipe
    flags=F user=nobody argv=/usr/bin/perl /path/to/ ${sender} ${size} ${recipient}
    Be very careful to get all those spaces in. The flags and ${..} arguements are documented in the unix manual under pipe(8).
  4. Verify that Postfix is reading our new settings. The following lines should be present in /etc/postfix/
    virtual_alias_maps = hash:/etc/postfix/virtual_addresses
    transport_maps = hash:/etc/postfix/transport

The Script

The rest is fairly simple to figure out. Below you will find a link to a sample script that attaches a file and sends it back. The script does a very basic anti-spam check to see if a keyword is in the subject line. You get access to the raw email text as STDIN. The parameters we asked for from pipe(8) are obviously in @ARGV. It is possible to "reject" email in this script by printing an error message to STDOUT and exiting with a non-zero code. Postfix will drop the email and notify the sender with your error message. This capability alone makes this excersize potentially very valuable.
sample script

Thats It!

With your new script written, restart Postfix so that the changes take effect. Send a test email to your new address and correct any errors your find. Enjoy!


Adam J. Kaplan
adkap [at) adkap {dot> com