IMAP server for home use
Jan Stocker <Jan.Stocker@t-online.de>
Abstract
Here you find a short instruction how to install an IMAP server and configure procmail with fetchmail to receive mails from an external mail server and distribute them automatically to different mail fiolders.
Introduction
I am using at home more than one OS and also have a PDA and Notebook. And I need all my e-mails accessable from any computer under any OS. Also I want to have a central storage for backup reasons. So the only solution is to have my own mail sever. I use IMAP cause it can handle more than one folder for a mail box.
After having a mail server ready for use, the next question is how to obtain the mails. You can make your mail server a part of the internet but for home use, that is in most cases not the best solution. A port named fetchmail is a small program polling new mails from an existing POP3/IMAP server and piping it through any other application.
With procmail you can filter your mails and send the incoming mails (from fetchmail) to a foilder of your choice.
IMAP Server
Let us start with the IMAP server. My favorite is Cyrus IMAPD found in the ports in three different versions, you should use at least something bigger than 2. The latest version 2.2 is working here quite good.
cd /usr/ports/mail/cyrus-imapd22 && make install clean
The next step is to enable the IMAP server in the /etc/rc.conf file, you only need to add the following line
cyrus_imapd_enable="YES"
The port has installed two standard configuration files for the server, both can be found in /usr/local/etc. They are called cyrus.conf.dist and imapd.conf.dist. You need to remove or rename the files to a filename excluding the extension .dist
cp -p /usr/local/etc/cyrus.conf.dist /usr/local/etc/cyrus.conf cp -p /usr/local/etc/imapd.conf.dist /usr/local/etc/imapd.conf
Please edit the imapd.conf file and add the user id for administrative jobs on the IMAP server. To simplify matters, I will use "root" here.
# The list of userids with administrative rights. Separate each userid # with a space. We recommend that administrator userids be separate from # standard userids. Sites using Kerberos authentication may use separate # "admin" instances. # admins: root
Cyrus IMAPD uses SASLDB for user authorization by default. You can also make Cyrus working with an LDAP server but this is out of topic for this article and not neccessary for home use. So first make sure your mail administrator has a password
saslpasswd2 root
In the same way you can add more users, just create a password for an username you want.
saslpasswd2 jstocker
After you created the users and their passwords, you can create additional folders beside the standed inbox. A good example is a folder for each maillist you have subscribed. I need FreeBSD-current and FreeBSD-SCSI. Let us start the cyrus administrator for my user jstocker:
cyradm --user jstocker localhost
Enter your password you had defined with saslpasswd for root before. Now you can create a new mailbox:
cm FreeBSD-current
Later in this article I show you how to fill these mail folders from the outside automatically. For this reason we need to change the ACL for these mailboxes, we will allow anyone to "p"ost to the folders.
setacl FreeBSD-current anyone p
Procmail
Next part is the installation of procmail.
cd /usr/ports/mail/procmail && make install clean
This is not a service, it is only a small program need to be called manually. In this article it get its input from fetchmail I will describe later, but fetchmail itself needs also to be called manually. I do this with a cronjob for each FreeBSD user. This need to be decided now, because the configuration file ".procmailrc" is been saved in the home directory of the user.
The following example is my current working file. It examines the from-address and puts the mails from the three FreeBSD mailinglists in a separate folder. All not matching ones are landing in the main inbox folder. I use here directly 'cyrdeliver' which one saves the mail in the local Cyrus IMAP Server. You can also setup a local mail system with sendmail.
PATH=/usr/bin:/bin:/usr/local/bin:. MAILDIR=$HOME/Mail DEFAULT=$MAILDIR/mbox LOGFILE=$MAILDIR/from LOCKFILE=$HOME/.lockmail :0 * List-ID:.*freebsd-current.FreeBSD.ORG | /usr/local/cyrus/bin/deliver -m FreeBSD-current jstocker :0 * List-ID:.*freebsd-hardware.FreeBSD.ORG | /usr/local/cyrus/bin/deliver -m FreeBSD-hardware jstocker :0 * List-ID:.*freebsd-scsi.FreeBSD.ORG | /usr/local/cyrus/bin/deliver -m FreeBSD-scsi jstocker :0 * .* | /usr/local/cyrus/bin/deliver -r "Jan.Stocker@t-online.de" jstocker
Fetchmail
Now we need to poll the mails from a real internet server. This is the job of fetchmail.
cd /usr/ports/mail/fetchmail && make install clean
Like procmail it has a configuration file in the users home directory, called ".fetchmailrc". My example polls all mails from two real internet servers (first one IMAP, second one POP3) and pipes them through procmail, which you know, delivers them to the different mail folder in the local IMAP server.
poll imh.informatik.uni-bremen.de proto imap user "MyUserName" pass "MyPassword" ssl fetchall mda "/usr/local/bin/procmail" poll pop.t-online.de proto pop3 user "AnotherUsername" pass "AnotherPasswd" fetchall mda "/usr/local/bin/procmail "
You can test this by calling
fetchmail
Every action is written to the console. For testing purposes it is good to change the configuration for fetchmail for leaving mails on the server.
Now you need to add a cronjob for fetchmail.
crontab -e
It can look like this:
*/2 * * * * /usr/local/bin/fetchmail
But I recommend to make a separate shell script with output redirection to a logfile rather than calling fetchmail directly, because your local user will get the output from fetchmail as a mail and this can be annoying if you call fetchmail each two minutes.
Conclusion
The combo cyrus / procmail / fetchmail is a simple solution for having a small home use mail storage with automatic mail distribution and polling.
Links
- Home of Cyrus IMAPD - http://asg.web.cmu.edu/cyrus/
- Home of Procmail - http://www.procmail.org/
- Home of Fetchmail - http://www.catb.org/~esr/fetchmail/
- Latest version of this article - http://www.shellbang.org
Copyright
(C) 2004
Jan Stocker
Redistribution of this text is permitted as long as the copyright is
retained and all text altering is marked by author and date.
Version 1.0 - 2004-08-18