Wednesday, February 3, 2010

Project: Building An All-Text Linux Workstation - Part 9

In our previous installment we saw how our Debian workstation supports email between users on the system.  This time we're going to add the ability to send and receive email over the Internet.

One of the reasons that email is such a difficult subject to cover is that there are so many different kinds of email tools and configurations.  For this lesson, we are going to create a really simple configuration designed to satisfy the basic needs of a residential user.  It is certainly possible to create a much more sophisticated configuration.  In fact, with Linux, almost any kind of email setup is possible, including huge enterprise-class solutions.

As we saw last time, our email client, mutt, reads mail messages that it finds in a mailbox file located in /var/mail.  To send mail, mutt passes a composed message to the exim mail transport agent (MTA) for delivery.  So how do we send mail to the outside world?

The Traditional Way

The traditional way is to configure the MTA to communicate with a smarthost, a remote server that can determine where the remote recipient's mailbox is located and pass the messages to it.  Such a configuration can be easily created in Debian by telling the package installer to reconfigure the exim4 package using a script built into the package.  This technique is good if your workstation is on a corporate network and you have a mail server through which mail from all users is sent and received.  You can find a complete description of this configuration process here.

Receiving mail is traditionally done by either configuring the MTA to receive incoming connections from other mail servers, or by running a mail delivery agent program (such as fetchhmail or getmail) that copies the contents of remote mailboxes to the local mailbox.

However, the traditional approach is not well suited to our residential workstation because each user may have different email providers, so we need a solution that is potentially different for each user.  Of course, with enough configuration, the traditional approach can be made to work, but it wouldn't be pretty.

The Client Centric Way

For those of you who have used GUI-based email clients like Evolution or Thunderbird, the traditional way probably seems very alien and complex.  You have likely used a single email client program that performs all the functions of the multi-program traditional method.  That's the approach that we'll try to take.  Too bad mutt makes it so hard.

The designers of mutt have taken the fairly stern view that a mail user agent (MUA) should be a mail user agent and nothing more.  In recent years however, they have softened their stance on this issue somewhat and now offer optional support for SMTP (Simple Mail Transport Protocol) to communicate with smarthosts and POP and IMAP support for reading mail on remote servers.  Fortunately, the version of mutt supplied with Debian has these optional features compiled in.

In the exercise that follows, we are going to configure mutt to use a POP3 server and an external smarthost, and an IMAP server and an external smarthost.  By leaving the configuration of exim unchanged, we will continue to send and receive local mail.  Note that you will need to adjust the configuration files listed below to fit your ISP's specific requirements.

POP3 Configuration

The Post Office Protocol (POP) is an older and less sophisticated mail delivery system.  It is common among residential ISPs.  To configure mutt to download messages from a remote POP3 (POP version 3, the version most often used today) and to send messages via a remote SMTP server acting as the smarthost, we will create a mutt configuration file and name it ~/.muttrc-pop3:


### POP3 setup for incoming mail

# File where incoming messages will be kept
set spoolfile=~/mailbox-pop3

# Your user name as understood by your ISP
set pop_user = "username"

# Your password as understood by your ISP
set pop_pass = "password"

# Host name of ISP's POP3 server
set pop_host = "mail.your_isp.com"

# Do not delete messages from POP3 server after downloading.
# Change to "yes" after testing.
set pop_delete = no


### SMTP setup for outgoing mail

# URL of ISP's SMTP server
set smtp_url = "smtp://username@mail.your_isp.com/"

# Password for SMTP server
set smtp_pass = "password"

# Your email address as understood by your ISP
set from = "username@your_isp.com"

# How you want your name to appear in email messages
set realname = "Your Name"

To execute this configuration, we invoke mutt this way:

me@linuxbox:~$ mutt -F ~/.muttrc-pop3

IMAP Configuration

If you have a good ISP, they will offer Internet Message Access Protocol (IMAP) on their mail server.  IMAP keeps your mail on the server and allows you to maintain multiple folders and has a host of other features lacking in the POP system.  In the configuration below, we will communicate with a remote IMAP server using SSL for encryption.  We will call this configuration file ~/.muttrc-imap.

Note: In order to use SSL authentication, make sure you have the libsasl2-modules package installed on your system.


### IMAP setup for incoming mail

# Your email address as understood by your ISP
set imap_user = "username@your_isp.com"

# Your account password
set imap_pass = "password"

# Name of your ISP's IMAP server and folder locations
set folder = "imaps://mail.your_isp.com:993"
set spoolfile = "+INBOX"
set postponed="+/Drafts"

### SMTP setup for outgoing mail (using SSL)

# URL of ISP's SMTP server including SSL (smtps://) and port
# number (:465) as needed.
set smtp_url = "smtps://username@mail.your_isp.com:465/"

# Password for SMTP server
set smtp_pass = "password"

# Your email address as understood by your ISP
set from = "username@your_isp.com"

# How you want your name to appear in email messages
set realname = "Your Name"

### Files needed to store IMAP cache and SSL certificates

set header_cache=~/.mutt/cache/headers
set message_cachedir=~/.mutt/cache/bodies
set certificate_file=~/.mutt/certificates


To use this configuration, we need to create the directories for the IMAP cache and for SSL certificate storage.  We can create them with the following command:

me@linuxbox:~$ mkdir -p ~/.mutt/cache/bodies

To execute this configuration, we invoke mutt like this:

me@linuxbox:~$ mutt -F ~/.muttrc-imap


Using Aliases To Support Multiple Configurations

We can simplify the invocation of mutt by adding these two lines to our ~/.bashrc file:

alias mutt-p='mutt -F ~/.muttrc-pop3'
alias mutt-i='mutt -F ~/.muttrc-imap'


There you have it.  We now have mutt commands for handling local mail (mutt), POP3 mail (mutt-p) and IMAP mail (mutt-i).

Further Reading

Background on mail protocols:
Using mutt with Gmail via IMAP:
Mutt configuration samples:
Other installments in this series: 1 2 3 4 5 6 7 8 9 10 11 12 13 14