When setting up your own e-mail system, there can be some difficulties even after you have the server set up on your network, in terms of sending and receiving mail for local users. At this juncture, you have to make sure that the mail is going to the appropriate user. For instance, Postfix accepts incoming mail for local users and relays mail from local users out to wherever it’s going. When it receives mail, it drops it into a mailbox file for the UNIX user account. To read this mail, you need to read the mailbox file directly, with a program that is capable of reading the raw file. However, even with the powerful multiuser capabilities of Linux, you don’t want to force your users to log on to the server and use a text-based mail reader, as you may end up on the street.
Fortunately, there are several e-mail protocols for retrieving e-mail from a server. By far, the most common is Post Office Protocol, otherwise known as POP3. Nearly all mail clients speak POP3. With POP3, the mail client connects to the server, downloads all new e-mail, and disconnects. This works fine for people who use only a single computer for all of their e-mail, but if they want to check their e-mail from any other computer, they won’t be able to get to all of their messages. They also risk having their messages scattered across multiple computers. Using Internet Message Access Protocol (IMAP) as an alternative is a good solution to this problem. In this article, I am going to add an IMAP server to my current e-mail setup and configure virtual users using a MySQL database.
Read the previous installments in this series
To catch up on the creation of this e-mail setup, check out the previous articles in this series:
What is IMAP?
IMAP provides a compelling alternative to POP3. With IMAP, all mail stays on the server, and clients manipulate the status. When you connect with IMAP clients from different computers, you see the same set of folders and all of your mail. If you read a message on one computer, it is read when you check it from another. If you delete it on one, it’s deleted everywhere.
For people who work with e-mail offline or over a dialup connection, IMAP has many solutions for managing e-mail. Mail clients can cache messages and then update message status on the server when they reconnect. Messages can be left on the server and deleted while still being unread, without having to download first. The main downside of IMAP is that leaving mail on the server consumes a lot more disk space in one place, rather than being distributed across a bunch of clients. This makes backups much easier, but you may have to add disk space or implement quotas.
Courier-IMAP
Courier-IMAP is the IMAP server I’ve chosen for this e-mail system. It provides both IMAP and POP3 servers, so your users can choose either. It can access mail for users who don’t have a local UNIX account (“virtual” users), and by putting the account information in a MySQL database, it can share these accounts with Postfix.
Courier-IMAP uses a directory-based file format called Maildir instead of traditional UNIX mailboxes. By using individual files for each mail, Maildir mailboxes don’t have locking issues, and opening a large mailbox is fast.
You can download the source tarball for Courier-IMAP. Courier-IMAP is one part of a complete mail suite, but most of the other parts of the suite are not as widely used as the IMAP server. It also supports SSL-encrypted connections if you compile against the OpenSSL libraries.
Compile and install Courier-IMAP
Follow the instructions of the README file in the Courier-IMAP archive to compile and install Courier-IMAP. In the configure step, make sure you provide the paths to the MySQL headers, as described here:
$ ./configure –with-authmysql –without-authpgsql \
> –with-mysql-libs=/usr/local/mysql/lib/mysql \
> –with-mysql-includes=/usr/local/mysql/include/mysql
Courier-IMAP installs everything in /usr/lib/courier-imap by default. You will want to add this to your list of files to back up, especially the etc subdirectory.
Create the user database
Now you need to create a MySQL database to store the user accounts. The User table in MySQL sidebar shows a create table statement that sets up the required fields in the database for your e-mail system.
Configure Courier-IMAP
The main file you need to edit to configure Courier-IMAP is /usr/lib/courier-imap/etc/authmysqlrc. This file contains the connection and column settings for the user table you just added to MySQL. Change these settings where they are in the file, rather than moving them around, so that if you upgrade Courier-IMAP later, it will preserve your settings. Set the values as follows:
MYSQL_SERVER dbserver
MYSQL_USERNAME user
MYSQL_PASSWORD password
MYSQL_PORT 3306
MYSQL_DATABASE maildb
MYSQL_USER_TABLE users
MYSQL_CRYPT_PWFIELD crypt
MYSQL_UID_FIELD uid
MYSQL_GID_FIELD gid
MYSQL_LOGIN_FIELD id
MYSQL_HOME_FIELD home
MYSQL_NAME_FIELD name
MYSQL_MAILDIR_FIELD CONCAT(home,’/’,maildir)
Next, edit the authdaemonrc file in the same directory, removing the extra authentication services from the authmodulelist setting:
authmodulelist=’authmysql authpam’
Courier-IMAP comes with a single System V script to start all of the servers. However, the services are disabled by default, so you have to edit the configuration files for the services you want to run to enable them. To enable the IMAP service, edit the imapd file in this directory, and set:
IMAPDSTART=YES
To enable the POP3 service, edit the pop3d file and set:
POP3DSTART=YES
Now you’re ready to start Courier-IMAP. You can start each of these services individually, using the scripts in /usr/lib/courier-imap/libexec, or if you have a System V system, you can use the init.d script from the source directory to configure Courier-IMAP to run when the server starts. To set up the System V script, change back to your source directory and type the following as root:
# cp courier-imap.sysvinit /etc/rc.d/init.d/courier-imap
# chmod 744 /etc/rc.d/init.d/courier-imap
# /etc/rc.d/init.d/courier-imap start
Now Courier-IMAP is up and running and using the MySQL database that was created to authenticate users. But you still need to get Postfix to deliver mail to the virtual users.
What’s next?
In the next article, I will configure Postfix to deliver mail to virtual users in the MySQL database that I created in this article, set up the UNIX environment for the virtual accounts, and discuss the DNS issues I need to resolve to get the e-mail delivered correctly to the new server. By the end of the next article, you will have a fully-functioning e-mail server capable of sending and receiving e-mail for multiple domains.