Postfix ♥ scripts

So my friend called me yesterday. He asked me to set up a mailing list service for his domain I manage.

"One thing though: I only want specific people to be able to send e-mail to the list."

Could I have found a system that has a nice GUI and does this and more? Sure. The real question is: was I going to? Absolutely not.


In this article I will tell you how to point an e-mail address to a script.
We will deal with all the python stuff later.

The project

An e-mail address that forwards all incoming mail to a script that can process the message.

Setup

Make sure you have a domain name pointed at your server (eg: example.com), otherwise you won't be able to receive mail.

  1. Install postfix

    sudo apt-get install postfix
    sudo service postfix start
    
  2. Set up main.cf:

    Open postfix's main configuration file with:

    /etc/postfix/main.cf
    

    Make sure you have the following lines:

    virtual_alias_maps = hash:/etc/postfix/virtual
    alias_maps = hash:/etc/aliases
    alias_database = hash:/etc/aliases.db
    
  3. Set up aliases

    We are going to use aliases to forward the incoming e-mail to our script.

    With aliases messages sent to a non-existent UNIX user will be forwarded to a destination we specify (another user or in this case a script)

    Open the aliases file with sudo nano /etc/aliases

    On the last line type:

    script: "|/path/to/your/script"
    

    Where script is the alias name you want to use (it can be anything). Make sure to type a valid path, we will put the actual script there later.

  4. Set up virtual alias maps

    Virtual alias maps will let you specify incoming e-mail addresses and what you want to do with them.

    To create your virtual alias file issue:

       sudo touch /etc/postfix/virtual
    

    Then open it for editing:

       sudo nano /etc/postfix/virtual
    

    In the file enter an e-mail address and forward it to the alias we created in step 3.

       youremail@example.org script
    
  5. Load all the stuff in postfix

    To let postfix know we changed a bunch of stuff issue these commands one-by-one:

    sudo postmap /etc/postfix/virtual
    sudo newaliases
    sudo postalias /etc/aliases
    sudo postfix reload
    

Postfix is now ready to forward e-mail to your script.

-

In the second part of this article I will show you how to process the email using python.


I have a tiny newsletter you could subscribe to.

I am also on twitter, see you there.
@fonorobert