Monday, January 7, 2008

IMAP Mail Server

An IMAP mail server stores your email on a server and allows you to view it over a network on any computer with an email client that is configured to access the IMAP server. All of your email, including sent items and drafts is available from any computer. If you have more than one computer in your household this can be very handy. You could send an email from your wife's computer, then go back to your computer and it would be there.

The basic idea is that you have two programs. The first gets the mail from your POP3 accounts (yahoo, gmail, hotmail, etc) and saves them in a local directory. Emails in their raw form are just text. Attachments are encoded as text as well.

A second program looks at the email files and makes them available to email clients using the IMAP protocol. They are not copied to the client, although they can be synchronized if you desire, so no matter what computer you use to access your email, it will be the same. You can conceivably use this to implement remote access to your email, but I have not done that yet.

I use the setup recommended by Linux Reality episode 61. This uses Getmail to get the mail and Dovecot as the IMAP server. Dovecot can serve IMAP from either maildir or mbox format. Basically, maildir creates a directory structure and saves each email as separate file whereas mbox creates a single file with all of your email messages concatenated together. This makes some operations slower and can create file access problems. I am using maildir.

GETMAIL

Getmail will not create the maildir folders, so you will need to manually create the following four folders:
/home/user/Maildir
/home/user/Maildir/new
/home/user/Maildir/cur
/home/user/Maildir/tmp

Installing getmail in Debian is easy. You can either use Synaptic or from the command line:
#apt-get install getmail4

Setting up getmail is very easy. You create a config file called getmailrc and put it in the following folder: /home/user/.getmail/getmailrc
This file contains two sections. The first tells getmail where to get mail from and the second tells getmail where to put the mail. Here is my getmailrc file.

**********
#This is the getmail configuration file for my-server
#01 October 2007
#for a complete, commented file, see http://pyropus.ca/software/getmail/configuration.html
#this file should be located in /home/user/.getmail

[retriever]
type = SimplePOP3SSLRetriever
server = pop.mail.yahoo.com
username = username
port = 995
password = password
use_apop = False

[destination]
type = Maildir
path = ~/Maildir/

[options]
verbose = 1
read_all = true
delete = true
message_log = ~/.getmail/log
**********

Getmail does not run automatically (as a daemon). To execute getmail you use the command:
$getmail
Getmail will run once and then it is done.
To automate getmail, you have to create a cron job. This is discussed in Linux Reality episode 39 with the basic steps spelled out below

Login as the user you want to run the cron job.
$crontab -e
The basic format for a crontab entry is as follows (separated by spaces)
minutes hours dayofmonth month dayofweek command
minutes range is 0-59
hours range is 0-23
dayofmonth range is 1-31
month range is 1-12
day of week range is 0-6, 0=Sunday
In order to for getmail to check mail every minute, add the following line:
* * * * * getmail
or, to check email every hour on the hour, add the following line
0 * * * * getmail
save and exit

I have had no problems running getmail for my Yahoo account. For my gmail account I have had an issue where gmail was hiding messages that had been read or downloaded but not deleted. To fix this open gmail and then under Setting choose Forwarding and POP/IMAP and select "Enable POP for all mail (even mail that's already been downloaded)". This will download all messages and then revert to the default setting.

DOVECOT

The first time I tried to install dovecot it failed completely. I never found out what was wrong and abandoned it. I searched the internet and came up with courier as an alternative. I installed courier and used it successfully for a while until one day I was editing the config file and the whole thing crapped out. I couldn't uninstall it, I couldn't fix it, I couldn't do anything. I ended up reformatting the hard drive and rebuilding the whole server. I installed dovecot the second time and it works great.

Installing dovecot is simple using Synaptic or simply type the command:
#apt-get install dovecot???

Configuring dovecot is simply a matter of editing the following file:
/etc/dovecot/dovecot.conf
Below are the lines that I had to edit in my dovecot.conf file. Note that I am using dovecot behind a NAT firewall where I have absolutely no risk of exposure so I have enabled plain text authorization. If your network is not secure do not do this.

**********
protocols = imap #imaps
disable_plaintext_auth = no
login_greeting = Dovecot ready on my-server
mail_location = maildir:/home/user/Maildir
**********

No comments: