Collecting Mails from Different Accounts

Maybe like me you have various mail-accounts with different providers. wouldn't it be convenient to have all of them collected in one place and accessible through one account from all your devices (Home, Work, Laptop, Mobile phone…)? Here's how I achived this using a combination of fetchmail/procmail for fetching and sorting mail and dovecot to make them accessible via imaps (Note: I don't run my own smtp-server but use the providers servers for this):

Prequisites

You will need a linux server with root access and a permanent internet connection to use this setup. Also you need to install the following programs:

dovecot
fetchmail
procmail

How to install these depends on your distribution, for gentoo its as simple as

emerge -av dovecot fetchmail procmail

Configuring Dovecot

Since I will use the normal system users as mail users the dovecot setup is quite simple. This config also disallows plaintext login, so you will have to use ssl. I won't show how to generate the (self signed) certificates here, since there are more than enough Howtos on the net for this. This setup will use a directory Maildir in the users home directory. For a setup like this here is a working /etc/dovecot/dovecot.conf (for dovecot 1.1.x):

listen = *
disable_plaintext_auth = yes
syslog_facility = mail
ssl_cert_file = /etc/ssl/dovecot/server.crt
ssl_key_file = /etc/ssl/dovecot/server.pem
mail_location = maildir:~/Maildir
protocol imap {
  imap_client_workarounds = outlook-idle tb-extra-mailbox-sep
}

protocol pop3 {
}
protocol lda {
  postmaster_address = postmaster@example.com
}
auth default {
  mechanisms = plain
  passdb pam {
    args = "*"
  }
  userdb passwd {
  }
  user = root
}
dict {
}
plugin {
}

With this you should be able to access your mailserver with configured system users through imaps. Now on to fetchmail/procmail config to get the mails from the providers servers

Fetchmail Configuration

Fetchmail configuration is quite simple. For every user you want to fetch mail for put a .fetchmailrc with content like this in the users home directory:

poll mail.example1.com
protocol pop3
username username1
password supersecretpassword
mimedecode
mda "/usr/bin/procmail -d %s"

poll mail.example2.com
protocol pop3
username username2
password secretpassword2
mimedecode
mda "/usr/bin/procmail -d %s"

I think this is quite self explanatory, and there is enough documentation out there if you have further questions. One thing to note: permissions for the .fetchmailrc have to be 600 and it has to be owned by the according user.

Procmail Configuration

Now that we get the mail from our accounts we certainly want to sort them in some way. Thats what we will use procmail for. In fetchmails configuration above you can already see the last line of each block which passes the fetched mail to procmail for proccessing. Like for fetchmail we need a configuration file for procmail. Its name is .procmailrc and it has to be in the users home-dir like the fetchmail configfile. Here's an example file:

# Some default Variables
MAILDIR=$HOME/Maildir
DEFAULT=$HOME/Maildir/
VERBOSE=no
LOGFILE=$HOME/procmail.log

# Mails with "[dokuwiki]" in the Subject go into Lists/[dokuwiki]
# Note that with dovecot the seperator for directories is a single dot
:0
* ^Subject:.*\[dokuwiki\].*
.Lists.[dokuwiki]/


# Anything from amazon goes into Online-Shops/amazon
:0
* ^From:.*@amazon.de.*
.Online-Shops.amazon/

# Anything to the second account goes into username2
:0
* ^TO username2@example2.com
.username2/

# Anything to the first account goes into the default Inbox
:0
* ^TO username1@example1.com
$DEFAULT

# Everything that isn't sorted by now goes into Junk
:0
.Junk/

You can add more rules if you like. Also procmail can do a lot more complex tasks like spam-filtering and so on. The web has a lot of howtos and documentation on this topic.

Last Step: Fetching Mail Automatically

All that is left to do now is to run fetchmail on a regular basis for the user(s) you want't to fetch the mail for. An easy way to achive this is by using cron. Here is an example line for crontab to fetch mail every 10 minutes for the user “seiichiro”:

*/10  *  * * *  root    /home/seiichiro/getmails.sh

The script called by cron looks like this:

#!/bin/bash
su seiichiro -c fetchmail >/dev/null 2>/dev/null

Thats it. If you did everything right your server will fetch and sort your mail automatically and it will be avaiable to you with any imaps capable device/program. I use my server from different Windows and Linux systems at home and work, a N810 Internet Tablet, a Nokia E51 Mobile phone and a webinterface (v-webmail). If you have questions or suggestions feel free to leave a comment below.

Comments




S M L​ F L
blog/collecting_mails_from_different_accounts.txt · Last modified: 28.09.2010 13:39 by Seiichiro
CC Attribution-Share Alike 3.0 Unported
Driven by DokuWiki Hosted by Linode Recent changes RSS feed Valid XHTML 1.0