TheGlide\’s blog

Ramblings, ramblings, ramblings…

Mac: configuring a mail server (part 2 – getmail, procmail)

leave a comment »

Hello and welcome to the second installment of these articles dedicated to setting up a small email server on a Mac for personal use by a local user. In this second part I’m going to complete the setup with getmail and procmail.


1. Configuring getmail

1.1 getmailrc
Getmail is developed by Charles Cazabon and in his own words is…

“a mail retriever designed to allow you to get your mail from one or more mail accounts on various mail servers to your local machine for reading with a minimum of fuss. getmail is designed to be secure, flexible, reliable, and easy-to-use. getmail is designed to replace other mail retrievers such as fetchmail.”

Compared to the famous fetchmail developed by Eric S. Raymond, IMO getmail is easier to configure and to maintain, thanks to a clearly documented configuration file format.

In my case all I need is to:

  • run getmail periodically (say every X minutes) to download emails from 2 POP3 servers (i.e. 2 different mail accounts).
  • let getmail pass the emails along to procmail so they can be processed before being delivered to the final destination in my local Maildir folder.

In the first part of this series, I explained how to install getmail from the .tar.gz which is downloadable from the project home page. After that step is done, all we need to do is to prepared the configuration scripts to download emails and deliver them appropriately. I say scripts (plural) because with getmail you have to create a single script for each email server you are going to poll for new emails.

I have to poll 2 POP3 servers, which will make the configuration files very similar. For the time being I also prefer to leave email on the servers (I’m still considering this setup as a test – once I’m fully satisfied I’ll let getmail delete emails from the POP3 servers).

Here is the template of the two configuration files I have:

[retriever]
type = SimplePOP3Retriever
server = pop3.server.name
username = pop3username
password = pop3userpassword
timeout = 120

[destination]
type = MDA_external
path = /usr/bin/procmail
arguments = ("-d", "localusername")

[options]
verbose = 2
read_all = false
delete = false
delivered_to = false
message_log = ~/.getmail/logfilename

I created two of them and placed them in ~/.getmail/getmailrc-pop3-1 and ~/.getmail/getmailrc-pop3-2: they only differ for the server, username, password and message_log field.

The [retriever] section specifies the type of server (POP3 in this case), its IP address, and the necessary information to log in (username and password). I also specified a timeout of 120sec.

The [destination] section is where you specify how to deliver the mail locally. In this case I chose to use an external MDA (Mail Delivery Agent), i.e. type = MDA_external, that can be found in /usr/bin/procmail (the path for procmail in Mac OS X). Arguments to the MDA can be specified with the arguments keyword: in this case I only enforced the username (which is the local Unix user that will receive the mail).

The final section is the [options] section that allows to control the way getmail operates. Since I want to leave the mail on the POP3 server and I want to download only new messages, I added the read_all = false and delete = false settings. I also added the delivered_to = false setting but… I don’t remember why :-) (I should check back my notes!).

1.2 getmail and cron
Now that getmailrc* files are ready, we need to schedule regular runs of getmail to download email from all the POP3 servers. Even though Mac OS X has a new mechanism called launchd which offers extended functionality over cron, I still decided to setup a cron job. I will play with launchd in a later article.

First I prepared a shell script (checkmail.sh) to run getmail on the 2 getmailrc* files I previously created:

getmail --rcfile ~/.getmail/getmailrc-pop3-1 >> ~/.getmail/log-pop3-1 2>& 1
getmail --rcfile ~/.getmail/getmailrc-pop3-2 >> ~/.getmail/log-pop3-2 2>& 1

Then by running crontab -e from the Terminal I created a crontab entry to run the checkmail.sh script every 15 minutes:

0,15,30,45 * * * * <yourpath>/checkmail.sh >> ~/.getmail/log-checkmail 2>& 1

This will run the checkmail.sh script every 15 minutes and dump any error messages in the ~/.getmail/log-checkmail file. Also regularly check the log files dumped by getmail for any error accessing the servers and/or downloading emails.

Note: for the system to find the getmail executable, the directory /opt/bin should be in the PATH.

2. Configuring procmail
Why use procmail? Well first because I wanted to try it and understand it. Second because I wanted to do some processing on the incoming mails, e.g. drop them in a different folder in the Maildir directory depending on the incoming POP3 mail server.

When procmail is invoked by getmail, it will look for a file named ~/.procmailrc which contains all the necessary setup and procmail-rules to process incoming mail.

Teaching how to properly configure procmail is way beyond the scope of my articles so I suggest you take a look at the Procmail QuickStart document which is really well done and detailed.
Here I will just show you how I setup procmail to drop mails in different folders depending on the POP3 server they came from. First let’s begin with the ~/.procmailrc header, which contains some important variables:

SHELL=/bin/sh
DEFAULT=$HOME/Maildir/
MAILDIR=$DEFAULT
PMDIR=$HOME/.procmail
LOGFILE=$PMDIR/pmlog
VERBOSE=yes

The most important here are MAILDIR, which specifies the location of the Maildir folder, and DEFAULT, which gives the location of the default folder (i.e. the INBOX). LOGFILE specified the location for procmail’s logfile. I also enabled detailed messaging with VERBOSE=yes.

Now on to the rules section:

:0 c
.INBOX.backup/

:0
* ^Received:.*from pop3\.server\.one.*with POP3
{
:0
.INBOX.Account1/
}

# DEFAULT
:0
$DEFAULT

The first rule (:0 c) basically makes sure that the first thing that is done by procmail is to put a copy of the incoming mail into a backup folder (.INBOX.backup/ in Maildir naming format).

The second rule is actually a nested rule that is activated each time the mail comes from “pop3.server.one”: I apply a regular expression to the Received: part of the mail message and look for the evenlope which is added by getmail that includes the name of the POP3 server where the mail came from. This way I can do some actions (in this case a simple move to .INBOX.Account1/) on mail as they come from different POP3 servers.

Lastly if the mail doesn’t fit into any of the preceding rules, I just put it into the default incoming box.

3. That’s it!

All the pieces are now together. In part1 we have set up postfix and dovecot to act as servers to send emails to an external SMTP server (postfix) and to make the mail in the local folder available to clients through a local IMAP server (dovecot). In this article I discussed how I set up getmail and procmail to gather email from 2 POP3 servers and deliver it in the local Maildir folder (which is the folder that the IMAP server is going to access).

To test the current getmail configuration you could simply run by hand the configuration scripts, check the log files to see that both getmail and procmail worked correctly, then check your mail folder for any new mail.

I’ve not covered in detail the setup of mail clients like Mail.app and Thunderbird but since I’ve run out of time I’ll do this into a soon-to-come article.

Please let me know if you have any comments, questions or corrections to what I wrote.

Thanks!

Advertisements

Written by theglide

October 12, 2006 at 12:00 pm

Posted in Mac

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: