April 26, 2008

postfix / procmail / courier-imap for dummies

To install a mail server on a unix machine, you can use the following stack:

  • postfix : listens for incoming emails and delegates to...

  • procmail : delivers emails to each user's mailbox

  • Courier-IMAP : listens for pop and imap requests and serves emails



This is just a high-level installation procedure for a mail server where the users are simply the local users.

Install postfix and make sure it delegates to procmail by adding the following line to the appropriate section in postfix's main.cf:
mailbox_command = /usr/bin/procmail -a "$EXTENSION" DEFAULT=$HOME/Maildir/

After you make any changes to main.cf, you should run:
postfix reload


Make sure /etc/procmailrc or $HOME/.procmailrc contains the following lines:
MAILDIR=$HOME/Maildir
VERBOSE=on
LOGFILE=$MAILDIR/from

:0
new


Next, build the maildir by issuing
maildirmake ~/Maildir


At this point, we should have a working system, that we can test with something like:
mail -s hello $USER
Hi,
This is a test
^D


If everything works fine, the email should be received by postfix, sent to procmail, and delivered to $HOME/Maildir/new.

To access the emails through POP or IMAP, we need to install Courier-IMAP. Once this is done, start the authdaemond and the courier daemon. Finally, netstat -a should reveal everyone listening: one smtp connection, one POP connection and one IMAP connection.
tcp 0 0 *:smtp *:* LISTEN
tcp 0 0 *:imaps *:* LISTEN
tcp 0 0 *:pop3s *:* LISTEN
tcp 0 0 *:pop3 *:* LISTEN
tcp 0 0 *:imap *:* LISTEN
tcp 0 0 localhost.localdom:imap localhost.localdo:34187 ESTABLISHED


You can fire up thunderbird and create an account with the local hostname as the POP or IMAP server. You should then be able to send yourself emails.


Once this works, you can use and test the fetcher plugin for Ruby on Rails (more info here).