Tuesday, May 29, 2007

Email for the single user in Debian


This is a simple tutorial aimed at those wishing to setup their Debian box to work with their ISP email account. Please send any corrections or comments to nlativy at gmail dot com. Right now on with the tutorial...

The first thing to do is get the needed packages which can be accomplished by running:

# apt-get install exim4 fetchmail mutt procmail cron vim

Of these packages you probably already have cron and exim4 also you may have noticed I slipped in vim which is unessential as you can use any other editor if you want, although why would you ;-).

Exim4

Hopefully you should have already set this up when you installed Debian, if not you can simply run:

# dpkg-reconfigure --priority=medium exim4-config

and choose "mail sent by smarthost; recieved via SMTP or Fetchmail" and set the smarthost to whatever the SMTP server of your ISP is. Note here that we are telling dpkg-reconfigure to only ask us medium priority questions and deal with the rest automatically.

Next, for exim to put the correct From: line on outgoing mail we need to add the following line to the /etc/email-addresses file:

user: someone@isp.com

substituting in your local username and your email address of course.

For myisp you need to edit
1
) /etc/exim4/email-addresses
(a symlink to /etc/email-addresses
which had a single line
loginname: loginuser@myisp.net

2) /etc/exim4/passwd.client
which had a line
outgoing.myisp.net:myisplogin:myisppasswd

3) /etc/exim4/exim4.conf.template
where we set
AUTH_CLIENT_ALLOW_NOTLS_PASSWORDS='true'



Procmail

Procmail is a program for filtering mail, especially useful if you are subscribed to mailing lists. Setting up Procmail is pretty simple, just put the following in the ~/.procmailrc file:

MAILDIR=$HOME/mail/
LOGFILE=$HOME/.procmaillog
VERBOSE=no

# Mailing lists

# debian-user
:0
* ^TO_debian-user
debian-user/

# All other mail goes to inbox
:0
inbox/

Note the trailing "/" on the mail boxes, this tells Procmail to use the maildir format. Also now would be a good time to create the maildirs we are going to use. Simply run the following commands:

$ mkdir -p ~/mail/inbox/{cur,new,tmp}
$ mkdir -p ~/mail/sent/{cur,new,tmp}
$ mkdir -p ~/mail/debian-user/{cur,new,tmp}

You can obviously skip the last command if you aren't subscribed to debian-user.

Fetchmail

Since we are only talking about a single user the simplest way to configure fetchmail will be to run it as the user who you are collecting mail for, so in the ~/.fetchmailrc file put the following:

poll pop.isp.com with protocol pop3,
user isp_username there is local_username here,
with password isp_password;

mda '/usr/bin/procmail -f fetchmail'

replacing pop.isp.com, isp_username, local_username and isp_password with the appropriate values. Notice we are passing the mail on to procmail. Also if you use a protocol other that pop3 (which I recommend you do since POP3 sends your password over the network unencrypted) or your ISP supports some form of authentication on top of POP3 then you need to simply replace pop3 with the desired protocol -- see the fetchmail man page for a complete list of supported protocols.

Now you can test if it is all working by running:

$ fetchmail -vk

I would recommend the -v and -k flags first time in case there is a problem v makes it run verbosely and k tells it to leave the messaged on the server. Of course running fetchmail manually is a pain so we can add a cron job for it: run crontab -e, which will launch your default editor, and enter the text:

*/10 * * * * fetchmail -s

this will run fetchmail every 10 minutes and check for any new mail. The -s flag tells fetchmail to run silently (unless something goes wrong) and if you miss it out you will be emailed fetchmail's output every 10 minutes which is obviously undesirable.

What to do if your connection isn't always on

Unfortunately on my laptop the '-s' flag isn't sufficient as when I'm not on the network I get a mail every 10 minutes about fetchmail not being able to connect. To combat this I rustled up the following bash script:

#!/bin/bash

DEVICES='wlan0:eth0'
FETCHMAIL='/usr/bin/fetchmail'
OPTIONS='-s'

devices=`echo $DEVICES | sed 's/:/\\\|/g'`
ifstate=`grep '\('$devices'\)' /etc/network/ifstate`
if [[ $ifstate != "" ]] ; then
$FETCHMAIL $OPTIONS
fi

Now I run this bash script in my cron job instead of fetchmail itself and no longer get error messages just because the network is down. To save you copying and pasting this you can download the script then simply put it somewhere convenient (I use ~/bin for all my scripts) make it executable (chmod 700 whatever-you-named-the-script) and replace fetchmail -s in the above cron job with the path to wherever you put the script on your machine, for example I have:

*/10 * * * * /home/nrl/bin/fetchmail

Mutt

Finally we can setup mutt, this step should be pretty simple as most of the work is done by the aforementioned programs. All we really need is the following in the ~/.muttrc file (the first five lines are the only really essential ones):

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

# Add an item for each mailbox
mailboxes ~/mail/debian-user

# unessential niceties:

# Show only important stuff in the header
ignore *
unignore from resent-from reply-to x-mailer user-agent date to cc subject

# Order to display the headers in
hdr_order From: Resent-From: Reply-To: X-Mailer: User-Agent: Date: To: Cc: Subject:

# sort messages by thread
set sort=threads

# Automatically quote message in reply
set include=yes

# Set quotemark to 1 byte
set indent_str="> "

# Only show the body when I edit a message
unset edit_headers

# for when you just can't wait for the cron job :)
# set up mutt so i can run fetchmail at any time by pressing G
macro index G "!fetchmail\n" "Invoke fetchmail"
macro pager G "!fetchmail\n" "Invoke fetchmail"

# tell mutt about my mailing lists
subscribe debian-user

I also noticed that in mutt (or at least the Debian package at time of writing) the default display of the list of messages will show the list name rather than the author when the message came from a mailing list. This is perhaps useful if all your mail is together but since we have filtered it into different maildirs it would be more useful to see the author of the message. So if you wish you can add the following line to your ~/.muttrc

set index_format="%4C %Z %{%b %d} %-15.15F (%?l?%4l&%4c?) %s"

Another thing worth noting is that we did not set your real name in the ~/.muttrc file, this is so that mutt will pull it from the /etc/passwd file. For this to work you must have set your real name when prompted during the Debian install process, if you did not do this you should now run the chfn command and set your real name as you would like it to appear on all outgoing email.

Vim

You can set vim up to run a few commands when it recognises that it is editing a mail message by adding the following to your ~/.vimrc:

augroup mail
autocmd!
autocmd FileType mail set textwidth=70 wrap nonumber
autocmd FileType mail :nmap :w:!aspell -e -c %:e
augroup END

This turns on line wrapping and sets the width of lines to 70 characters, it also turns off line numbering if you have it enabled elsewhere. The fourth line sets up vim so that you can spell check a message by simply pressing F8. This, of course, requires that aspell is installed. Note: you may prefer vimspell (see below) which offers "MS Word like" highlighting of spelling errors as you type.

muttprint, abook and urlview

What follows is unessential but thoroughly recommended for a nice mutt setup. First let's install some more programs:

# apt-get install muttprint urlview abook

Now add the following to your ~/.muttrc file:

set query_command="abook --mutt-query '%s'"
set print_command="muttprint"
macro index \cb "|urlview\n"
macro pager \cb "|urlview\n"
macro index a "|abook --add-email\n" 'add sender to abook'
macro pager a "|abook --add-email\n" 'add sender to abook'

Now mutt will run muttprint when you press "p" which will print the message nicely formatted, run abook when you press "Q" to query you address book, add the sender of the current message to your address book by pressing "a" and run urlview so you can easily view URLs in a message when you press C-b (control and b). You can also invoke abook from the command line (with the abook command surprisingly enough) to edit your address book in more detail.

Attachments and HTML mail

Another thing you need to deal with these days is the inevitable arrival of an MS Word document, pdf file or worst of all an HTML email in your inbox. Don't panic though to deal with these we can simply add the following lines to the ~/.mailcap file:

application/msword; /usr/bin/antiword '%s'; copiousoutput; description="Microsoft Word Text"; nametemplate=%s.doc
application/pdf; /usr/bin/pdftotext '%s' -; copiousoutput; description="PDF File"; nametemplate=%s.pdf
text/html; /usr/bin/lynx -force_html '%s'; needsterminal; description=HTML Text; nametemplate=%s.html

This will open up MS Word documents with the handy Antiword program so that you can have a quick look at them without having to wait for a bulky office suite to load up. The remaining two lines will open up pdf files with pdftotext (although you may prefer xpdf for this if you run mutt in X Windows) and use lynx to view HTML email. If you don't have these programs installed then you will need to run:

# apt-get install antiword lynx xpdf

Note that the pdftotext program is provided by the xpdf package.

Further reading

Now that you have mutt set up you are no doubt itching for more information so let me recommend the following:
Fighting spam on Debian with SpamAssassin - this should be the very next page you visit!
Mutt homepage - a good place to find lots of mutt related info
My first Mutt - a very nice page for new Mutt users
Mutt and GnuPG howto - a great guide to getting mutt to work with GnuPG
Exim homepage - home of all things exim :-)
Fetchmail homepage - The home of fetchmail
vimspell - a useful script for spell checking in vim
Antiword - quickly read word documents rather than waiting for an office suite to load.

Also if you are stuck don't forget to look at the man pages and you can also look at my ~/.muttrc file and my ~/.procmailrc file.


Copyright (C) 2004 Nicholas Lativy
Contributors: Florian Schlichting
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the licence can be found here.

No comments: