Dovecot is a really cool imap server. If you, like me, have a lot of mail accounts around the internet, a bit of gmail there, some hotmail over here, and want to collect everything on your nice local home server, this is the howto to follow.
Fetchmail
First of all we need to configure Fetchmail to fetch our mail. Fetchmail can't handle multiple IMAP accounts with IDLE with a single Fetchmail running, so we need to run multiple Fetchmail instances, one for each IMAP account we want to monitor. To do this we use this script:
# searches for subdirectories of $HOME/.fetchmail, and considers each as a # directory to run fetcmail from. The config should be stored as "fetchmailrc" # within this directory; without the leading ".". Networks can be started and # stopped independently, or all together. See the --help option for detail on # invoking the script.
FETCHMAILROOT=~/.fetchmail
cd "$FETCHMAILROOT"
ACTION="start"
while [ "$1" ]; do ARG="$1" case "$ARG" in -k|--kill) ACTION="kill" ;; -l|--list) ACTION="list" ;; -h|--help) ACTION="help" ;; --) shift; break ;; -*) echo "Unknown argument $ARG"; exit 1 ;; *) break ;; esac shift done
WHICH="$1"
function find_fetchmail_homes() { # Find directories immediately off FETCHMAILROOT for ENT in *; do if [ -d "$ENT" ]; then echo "$ENT" fi done }
case "$ACTION" in help) echo "$0: [options..] [network]" echo "Options are:" echo " -h, --help: This text" echo " -k, --kill: Stop network" echo " -l, --list: List networks, running or not" echo "If a network is named, the action applies only to that network" exit 0 ;; start) for ENT in `find_fetchmail_homes`; do if [ "$WHICH" -a "$ENT" != "$WHICH" ]; then continue; fi export FETCHMAILHOME=`pwd`/$ENT
fetchmail $EXTRAOPTS #$ENT done ;; list) for ENT in `find_fetchmail_homes`; do if [ "$WHICH" -a "$ENT" != "$WHICH" ]; then continue; fi export FETCHMAILHOME=`pwd`/$ENT FETCHMAILPID=$FETCHMAILHOME/fetchmail.pid
echo "Config $ENT in $FETCHMAILHOME" if [ -r $FETCHMAILPID ]; then echo " running as PID `cat $FETCHMAILPID`" else echo " not running" fi done ;; kill) for ENT in `find_fetchmail_homes`; do if [ "$WHICH" -a "$ENT" != "$WHICH" ]; then continue; fi export FETCHMAILHOME=`pwd`/$ENT FETCHMAILPID=$FETCHMAILHOME/fetchmail.pid
echo "Config $ENT in $FETCHMAILHOME" if [ -r $FETCHMAILPID ]; then fetchmail -q #$ENT fi done ;; *) echo "Ahh; unknown action $ACTION" exit 1 ;; esac
We call it fetchmultimail.sh and save it somewhere, maybe in our ~/bin/fetchmultimail.sh.
The script is configured with fetchmailrc files, one for every account:
#spam if header "X-Spam-Flag" "YES" { fileinto "Junk"; stop; }
#email1@gmail.com if header "X-Mailbox" "email1@gmail.com" { fileinto "email1@gmail_com"; stop; }
#email2@outlook.com if header "X-Mailbox" "email2@outlook.com" { fileinto "email2@outlook.com"; stop; }
#email3@outlook.com if header "X-Mailbox" "email3@outlook.com" { fileinto "email3@outlook.com"; stop; }
fileinto "INBOX";
As you can see, we use the X-Mailbox header we set with Fetchmail to sort our mail. We could use the To header too, but if someone sends you an email where you are in the Bcc or the Cc filed, the To header field would be empty, so we use the custom X-Mailbox header we set by ourselves, so we are sure the mail arrived to that account, and gets put in the correct folder.
The end
So that was all. I skipped uninteresting stuff like how to install SpamAssassin and stuff like that, because it's Linux distribution dependent, and is very easy.