# Howto: Easy IMAP for a home/small network

## arrowood

What?

This is a short write up of how to install and configure a simple IMAP solution for a small home network.  It only took about 30 minutes on my 1Ghz machine, including compile time and configuration.  My requirements were to be easy to install and configure, and low maintenance ongoing.

Why?

I have swapped email clients several times over the last few years.  Outlook to Evolution to KMail to Outlook Express and finally Thunderbird.  Each time I had to painfully convert my mail to various formats.

Using IMAP allows me to use any mail client that supports IMAP without changing the format that the email is stored in. This is especially nice since I can use my Windows box or one of my Linux boxes and access the same email, using different clients.  Before IMAP I had a network share of my email files and made sure I always used the same client (Thunderbird recently).  It was slow and could get corrupted easily.

Another benefit is that after doing this, it is easy to setup a web-mail interface to your IMAP server so you can retrieve your email anywhere.

How?

If you are used to a desktop mail client then some of this may be unfamiliar to you so I will explain it first before we jump in.  We will use "fetchmail" to retrieve the mail from our mail server (my ISP's pop3 server in my case).  Fetchmail then will hand off the email to "procmail" which can do various processing of the email.  We will just set it up to dump it in a Maildir folder, but you can add a lot of functionality here if you like (sorting, virus scanning, etc). We will set up a cronjob to pull the email every so often.  Finally, we will install an IMAP server so that our mail client(s) can access the email.

Everyone has their favorite software.  With IMAP, there are a few choices: 

# Courier IMAP (http://www.inter7.com/courierimap/)

# University of Washington (UW) IMAP (http://www.washington.edu/imap/)

# Cyrus (http://asg.web.cmu.edu/cyrus/imapd/).

# BincIMAP (http://www.bincimap.org/)

The first three are the "big three" while Binc is relatively new.  Here are some details and opinions on the feature/pros/cons of the big three:

http://www.coruscant.demon.co.uk/mike/imap/software.html

http://lists.debian.org/debian-isp/2005/03/msg00146.html

I choose Binc because:

- it is much smaller than the other choices

- its only job in life is to handle IMAP; Nothing else.

- has a focus on security

- is easy to configure

- is easy to maintain

If you are considering Binc, I highly recommend reading the FAQ on their website.  I found it very informative and useful.

You may disagree with me on some points above; Please - no flames; I am simply telling you why this was the right choice for *me*.   You are free to install whatever you wish.  :Smile: 

Okay, let's get busy.

Steps

NOTE: In the code below, please note that some commands are to be done as root and other as non-root.  Look for "#" for root and "$" for non-root commands.

1. Install fetchmail

```
# emerge fetchmail
```

```
$ touch ~/.fetchmailrc

$ chmod 600 ~/.fetchmailrc

$ vi ~/.fetchmailrc
```

Add the following to the file, changing the fields to the relevant values

```

poll your-mail-server

protocol pop3

username your-username

password your-password

mimedecode

mda "/usr/bin/procmail -d %s"
```

I have had success pulling my gmail account via fetchmail also;  Be sure and enable the pop options in your gmail settings on their website.

```
poll pop.gmail.com with proto POP3 port 995 and options uidl

user 'YOUR_USER_NAME@gmail.com' there with password 'YOUR_PASS' is 'LOCAL_USERNAME' here ssl fetchall keep

mda "/usr/bin/procmail -d %s"
```

To download from multiple email accounts just repeat the above lines in the file.

2. Install procmail

```
# emerge procmail
```

```
$ mkdir ~/Maildir

$ touch ~/.procmailrc

$ chmod 600 ~/.procmailrc

$ vi ~/.procmailrc
```

Add this to the file:

```
DEFAULT=$HOME/Maildir/INBOX/

LOGFILE=$HOME/.procmaillog
```

3. Install bincimap

```
# emerge bincimap

# vi /etc/bincimap/bincimap.conf
```

Change the depot value in the file

```
depot = "IMAPdir",
```

Set bincimap to start on startup:

```
# ln -s /etc/bincimap/service/bincimap /service/bincimap

# /etc/init.d/svscan restart
```

(OPTIONAL) This command will setup bincimap-ssl on your system: (IF YOU CHOOSE NOT TO USE SSL, then you will need to edit bincimap.conf to allow non-ssl connections.)

```
# ebuild /var/db/pkg/net-mail/bincimap-1.2.11/bincimap-1.2.11.ebuild config
```

4. Finishing up

You can now download your email manually:

```
$ fetchmail
```

Since you probably want it happen auto-magically, lets add a cron job to pull it.

```
$ crontab -e
```

Add this line to pull every 15 minutes or modify it to a different time interval:

```
00/4 * * * * fetchmail
```

Don't forget to reconfigure your mail clients to use IMAP.

Now you can install a web mail interface if you like. 

Credit

http://forums.whirlpool.net.au/forum-replies.cfm?t=316099

Large chunks of this were taken from this similar writeup. I wanted to reproduce it (with enhancements) here because I think it is valuable to the Gentoo community and would be easier to find here.

Many thanks to vorticon for sharing that.  You made my life much easier when I did my install. :Very Happy: Last edited by arrowood on Mon Apr 04, 2005 12:24 am; edited 1 time in total

----------

## RBH

I'd been considering doing this - now I definitely will! Many thanks  :Smile: 

----------

## Physaro

Woow, this guide rocks.

Its soo easy to setup a working IMAP serverr with your guide, thank you!!!

----------

## tekknokrat

Perhaps there some changes in development within the last month but i couldn't get it to work with this short stepbystep guide.

After some 10h  :Rolling Eyes:  investigation i found out that the messages must be placed in the "cur" directory under "INBOX/" which was never mentioned here.

Can someone tell me how to smoothly workaround that within the config of fetch/procmail/bincimap. 

Should i change mda's default path or should I create a symlink from INBOX -> cur? For what reasons do I need cur, new and tmp directory?  :Question: 

Cheers

----------

## MikeP

http://cr.yp.to/proto/maildir.html

Edit: this link is even better: http://www.qmail.org/qmail-manual-html/man5/maildir.html

You can find details there, including what new, cur and tmp are for.

Anyway, i have a similar setup, i just chose dovecat as IMAP server (havent yet checked bincs page), its supposed to be very secure aswell.

Also i used maildrop instead of procmail, because its language seems to be nicer.

Finally, i use postfix as an MTA and let fetchmail relay mail to port 25 - with this setup, i can also use the server for sending mails (also to local accounts) and directly receiving mails adressed to the servers IP/dns name.

While this isn't that important, i primarily made it to learn about postfix/mail servers.

It also might be nice when you send larger mails often, because the mail will be sent to the server via a fast lan connection first, then the server forwards it via the rather slow DSL/cable/dialup connection, so even larger mails appear to be sent quite fast..  :Smile: 

----------

## bdemore

How do you send mail with this system?

----------

## jmk

 *bdemore wrote:*   

> How do you send mail with this system?

 

You don't. This setup is retreval only.

If you want to send, set your mua to the smtp address of your isp as you would do normally.

You could also use a package like esmtp, ssmtp or nbsmtp to get mail of your box to a relay host (your isp). But i's not nessasary to do so.

Oh and arrowood,

Thanks for a good guide. It has reduced the complexity of my email setup alot.  :Very Happy: 

----------

## RBJ1128

 *MikeP wrote:*   

> 
> 
> Finally, i use postfix as an MTA and let fetchmail relay mail to port 25 - with this setup, i can also use the server for sending mails (also to local accounts) and directly receiving mails adressed to the servers IP/dns name.
> 
> While this isn't that important, i primarily made it to learn about postfix/mail servers.
> ...

 

How did you set up postfix as the MTA?  Thanks.

----------

## adsmith

Nice write-up, though I saw no mention of dovecot which is (also) dead simple to set up and works beautifully.  I once looked into Binc, but I decided to stick with dovecot.  I don't remember why, though.

----------

## KernelFolla

great tutorial.

I've imap too

Exists one way to start fetchmail when someone calls imap?

----------

## jonnevers

 *adsmith wrote:*   

> Nice write-up, though I saw no mention of dovecot which is (also) dead simple to set up and works beautifully.  I once looked into Binc, but I decided to stick with dovecot.  I don't remember why, though.

 

Using courier-imap over the years has created a very foul taste in my system administrative mouth. (so to speak)

I finally had enough two weeks ago and found dovecot while looking for a replacement.

Dovecot is everything courier-imap is not. Small, FAST (courier-imap used to timeout on certain operations on my system, without any errors logged), designed to be secure, and very simple to configure.

anyone looking for a reliable IMAP daemon, take a look into dovecot. For a nice webmail system, postfix + dovecot + roundcube is very robust and very simple to administer.

- Jon

----------

## NotQuiteSane

 *arrowood wrote:*   

> 
> 
> ```
> # ebuild /var/db/pkg/net-mail/bincimap-1.2.11/bincimap-1.2.11.ebuild config
> ```
> ...

 

this command returns nothing on my server.  just an empty line.  (using, of course the version present on my box)

NQS

----------

## dnas

NotQuiteSane,

you should check if the file /etc/bincimap/bincimap.pem exists, if it does you will get an empty line every the command

grtz

----------

## capitanjackal

when I try to connect to imap-ssl I receive:

@4000000044390f511ccc8924 9244 0 [unknown@127.0.0.1:] error initializing Binc IMAP: SSL negotiation failed: Unexpected EOF

the ebuild config created regularly the bincimap.pem with two parts (rsa key and cert)

any idea ?

thanks

Giacomo

----------

## zeek

 *capitanjackal wrote:*   

> any idea ?

 

emerge dovecot

Seriously.  Because its IMAP, you don't need to move/convert any of your mail.  Just emerge dovecot, make a pass on the config file and start it.  I used to have nothing but problems with courier, binc was a config nightmare, cyrus and uw could not handle the massive maildirs I had.  Save yourself the hassle, don't bother with those IMAP servers because they all have problems.

Dovecot just works.

----------

## capitanjackal

I can't reach to make work dovecot with vpopmail.

the best remain courier-imap, about 20 mail server installed with qmail and courier-imap without problems.

Giaco

----------

## schutzaus

Hi,

I just went through this HowTo and it worked GREAT! So Thank you again   :Smile: 

BUT: In my opinion the imap account behaves kind of slow ?

First my setup:

My gentoo box is connected to my 54mbit Wlan Router via cable.

I access imap with: Apple Mail (over WLAN/PowerBook) and Thunderbird 1.5 (over WLAN/WinXP).

Both WLAN connections are fine ... so i rule them out for causing the delay.

The noticable delay is with Apple Mail ... it takes up to 30 seconds to refresh the folders ... Thunderbird seems to work fine ... but i get strange "folder echos" (I have missplaced a folder with apple mail, made it a subfolder of Trash .... then moved it back to where its supposed to be, now in Thunderbird i always get this subfolder in trash ... when I delete it ti's gone until the next restart of Thunderbird ? in Mail its fine)

The main question is ... how long does it usually take for information sent by one client to propagate through the system to then get synced to antoher client ? (I have like 600 mails in my "Sent" folder) Is it possible that those mails reduce the update speed of imap ?

Is there any way to speed it up but at the same time keep the mails available ?

For example: I told Thunderbird and Mail to keep an offline copy of the imap Mails so i can read them when I'm not connected.

Can i tell imap or Thunderbird/Mail to only sync the new/changed Mails ?

----------

## Atlas95

Hello, this is a very great howto!

It is the only howto which is working on my computer, I would like to install roundcube webmail with this...

how to do?

I have the webserver and roundcube on my computer but how to config roundcube please?

I'm yet a newbie please help me  :Smile: 

Best regards  :Smile: 

----------

## thecooptoo

I can pick up mail 

```
grenada ~ # fetchmail  -k -f /etc/fetchmailrc

fetchmail: WARNING: Running as root is discouraged.

1 message for user at isp.co.uk (1371 octets).

reading message user@isp.co.uk:1 of 1 (1371 octets) . not flushed

grenada ~ #
```

```
grenada ~ # cat /etc/bincimap/bincimap.conf |grep ^[A-Za-z0-9\ ]

Authentication {

    allow plain auth in non ssl = "yes",            /* allow login or

                                                      authenticate

                                                      when not in

                                                      SSL/TLS mode */

    auth penalty = 4,                              /* on auth failure,

    disable starttls = "no"

Security {

    jail path = "/usr/bin",

    jail user = "nobody",

    jail group = "nobody"

Log {

    type = "syslog",                             /* supports

    environment ip variable = "TCPREMOTEIP"

Mailbox {

depot="IMAPdir"

    type = "Maildir",                              /* only Maildir

    path = "Maildir",                              /* default path */

    auto create inbox = "yes",                     /* create INBOX in

    auto subscribe mailboxes = "INBOX",            /* list mailboxes

    umask = "077"                                  /* use this umask

Session {

    idle timeout = 1860,                           /* idle timeout in

    auth timeout = 60,                             /* timeout before

    transfer timeout = 1200,                       /* timeout per

    transfer buffer size = 1024                    /* number of bytes

                                                      to buffer before

                                                      passing on to

                                                      client. */

SSL {

    pem file = "/etc/bincimap/bincimap.pem",        /* private key and

                                                      certificate

                                                      chain PEM file

                                                      name */

    ca file = "",                                  /* file to use as

                                                      certificate

                                                      authority */

    ca path = "",                                  /* path to search

                                                      for more certificate

                                                      authorities */

    cipher list = "!ADH:RC4+RSA:HIGH:MEDIUM:LOW:EXP:+SSLv2:+EXP",

    verify peer = "no"

      /* $Id: bincimap.conf.in,v 1.2 2003/08/23 12:53:25 andreaha Exp $ */

grenada ~ #

```

but then , taken from the bincimap FAQ 

```
grenada# telnet localhost 143

Trying 127.0.0.1...

Connected to localhost.

Escape character is '^]'.

* OK Welcome to Binc IMAP Copyright (C) 2002-2005 Andreas Aardal Hanssen at 2006-06-17 10:30:33 BST

1 LOGIN "user" "pass"

1 OK LOGIN completed

2 LIST "" "%"

* LIST (\Noselect) "/" "INBOX"

2 OK LIST completed

3 CREATE mailbox

3 NO CREATE failed: With a Maildir++ depot, you must create all mailboxes under INBOX. Try creating INBOX/mailbox.

3 CREATE INBOX/mail

3 OK CREATE completed

4 RENAME INBOX/mail INBOX/renamed

4 OK RENAME completed

5 DELETE INBOX/renamed

5 OK DELETE completed

6 SELECT INBOX

6 NO SELECT failed: No such mailbox "INBOX"

Connection closed by foreign host.

grenada #

grenada # cat /etc/procmailrc

# Use maildir-style mailbox in user's home directory

#DEFAULT=$HOME/.maildir/

DEFAULT=$HOME/Maildir/INBOX/

LOGFILE=$HOME/.procmaillog

grenada maureva #      

```

so what have i done wrong ?

----------

## Cr0t

Does anyone know how to combine this with spamkiller?

----------

