Mail Handling with getmail, procmail, Mutt and Exim

I recently needed to reinstall my mailing tools and so I am going to write it down for further reference. I have used getmail to fetch the mails from the POP3 Server, procmail to filter them, mutt to display them and finally exim4 to forward new mails to my providers server for delivering.

Installing the necessary packages

apt-get update && apt-get install getmail procmail mutt exim4

Getting the mails from the server

Depending on your needs there are different solutions, the simplest is to just grab the mails from your providers POP3-server without further processing such as filtering them into different mail boxes etc. This can be achieved by adding 2 lines to your ~/.muttrc file:

set pop_host="pop.gmail.com"
set pop_user="yourgmailaccount.gmail.com"

You can test your settings by starting mutt and then pressing G (Shift

  • g) mutt should now try to download your mails.

However, this is not so comfortable if you want to categorize your mails into different mailboxes. For example a mailbox for each subscribed mailing list. In this case I would recommend a separate tool such as fetchmail or getmail for downloading the mails. I personally use the latter, because it is quite simple and still provides all the features that I currently need. Now we have to configure it so that it can download the mails and forward them to procmail which will categorize them. This configuration is only valid for version 4.x of getmail.

mkdir ~/.getmail/ && cd ~/.getmail/
cat > getmailrc << "EOF"
[options]
# print out some information
verbose = 1
# just read new mails
read_all = false
# don't delete the mails on the server
delete = false
message_log = /home/username/.getmail/log

[retriever]
type = SimplePOP3Retriever
server = pop.gmail.com
username = yourgmailaccount@gmx.com
password = yourgmailpassword

[destination]
type = MDA_external
path = /usr/bin/procmail
# procmail requires either that the message starts with an mboxrd-style
# "From " line (which getmail can generate by setting "unixfrom" to True), or
# that the -f option is provided as below.
arguments = ("-f", "%(sender)", "-m", "/home/username/.procmailrc")
EOF

Sorting them with procmail

Next we have to set up procmail, note this is just a minimal configuration refer to the procmail manual for further information.

cat > ~/.procmailrc << "EOF"
MAILDIR=$HOME/mail/
LOGFILE=$HOME/.procmaillog
VERBOSE=yes

# example of filtering for mailing lists
# all mails to debian-user will be send to
# the debian-user directory.
:0
* ^TO_debian-user
debian-user/

# all other mails will be moved to the inbox
:0
inbox/
EOF

Create the mailboxes.

mkdir -p ~/mail/inbox/{cur,new,tmp}
mkdir -p ~/mail/sent/{cur,new,tmp}
mkdir -p ~/mail/yourmailbox/{cur,new,tmp}

You can test the settings by running getmail, if it succeeds then you should have some mails in ~/mail/inbox.

Setting up exim4 for SMTP Authentication with the ISP

So far we are able to fetch and read our mails but not yet to send new ones. To change this we have to configure exim4 in “mail sent by smarthost; received via SMTP or Fetchmail” mode. This and some other options can be configured with dpkg-reconfigure.

dpkg-reconfigure -phigh exim4-config

Additionally we have to provide our account name and password which exim will use for SMTP authentication with the mail server of our ISP.

echo "smtp.gmail.com:$accountname:$accountpassword" >> /etc/exim4/passwd.client

Try to send a test mail it should now deliver correctly.

Mutt configuration

As a final step we can tweak our mutt configuration a bit, here are the important settings from my file. For further options see the mutt documentation.

set mbox_type=maildir
set mbox="~/mail/inbox/"
set spoolfile="~/mail/inbox/"
set folder="~/mail/"
set record="~/mail/sent/"

mailboxes ~/mail/inbox ~/mail/debian-user
set sort=threads
set include=yes
set indent_str="> "

subscribe debian-user

Now you can (hopefully) read your mails and enjoy.

mutt                         # opens standard mailbox ~/mail/inbox
mutt -f ~/mail/custommailbox # opens custommailbox

Setting up a crontab

Ok one last thing, if your to lazy to run getmail by hand then set up a crontab. In the following example getmail gets executed every 10 minutes.

crontab -e
*/10 * * * * getmail