February 16, 2006

Mail with Nail

Author: Jonas Kramer

I do almost everything in text mode when working with Linux. Some tasks -- such as browsing the Web and reading email -- are harder to do satisfactorily at the console. I use Firefox to browse the Web, but finally I found a good tool to quickly access my mail in text mode.

Nail is a lightweight console-based mail client, made to be a modern replacement for the ancient mailx command. It's fully compatible with mailx, but has a lot more features, which makes it appropriate for everyday use. Nail supports POP3, IMAP, and SMTP out of the box, so there's no need for further programs like Fetchmail or Postfix. It also has a built-in Bayesian spam filter, so you don't need external tools like SpamAssassin.

Getting Nail

Nail is simple to install. Most Linux distributions provide a package for Nail, but if yours doesn't, or if you're unhappy with it (some distributions provide Nail without SSL support), go to the Nail SourceForge page and download the source tarball. Unpack it and change into the new directory. For most people just typing make should install it. I like to optimize binaries for size, so I installed Nail with the command:

make install PREFIX=/usr CFLAGS=-Os UCBINSTALL=/usr/bin/install

Set the UCBINSTALL variable to the correct path, since the default Makefile assumes that install is located in /usr/ucb/.

Basic configuration

Most of the configuration is done in your ~/.mailrc file, so other mail clients you use that comply to the POSIX.2 and Single Unix Specification version 3 (SUSv3) mailx specifications can use it, too. Some options for Nail only should be placed in a ~/.nailrc file if you want the ~/.mailrc to be read by other mail clients.

Here is how I set up my mail account at web.de, a popular German free mail provider. This configuration should be usable with almost all common mail providers -- just change the host names and login information:

set folder=imaps://shell-monkey@imap.web.de
set smtp=smtp://smtp.web.de
set from="shell-monkey@web.de (Jonas Kramer)"
set smtp-auth=login
set smtp-auth-user=shell-monkey
set smtp-auth-password=MyPassword

The folder option tells Nail where to get the mail from. The string starts with the protocol used to fetch your mail. Possible values beside imaps are imap, pop3, and pop3s. The rest of the folder string consists of your login name and server address.

The smtp option specifies the SMTP host, which Nail will use to send the mail.

The from option should be your mail address, optionally followed by a nick or real name. This is the sender address shown to the recipient of your mail.

The smtp-auth option tells Nail what authentication method to use. Nail knows the methods login and cram-md5. Most mail providers support both, so just choose the one you like more.

The last two lines specify the login and the password to use for SMTP authentication.

Start Nailing

If you start Nail without command-line arguments, it tries to fetch new mail from your mail account and shows you a list of the subjects with a number for each one. Nail uses these numbers to specify the target message for an action. For example, to reply to message 12, type reply 12 and press Enter.

Note that Nail shows new or unread mails only. When exiting, it saves all read mails to your local mbox file and won't display them again when it's started next time. To read old mail, you can start it with nail -f ~/mbox or just use the folder command in the Nail prompt to switch to your mbox or any other account.

To read messages, enter the number shown alongside the mail subject. If the mail fits onto the screen, it's displayed there, otherwise it's displayed with your $PAGER. After reading the message, enter z or h to get the subject list again.

To send mail to one or more addresses, type at the Nail prompt mail address1 address2 and so on for each of the addresses or, at the command line, type nail address1 address2. Attach files to your mail by typing ~@ file1 file2 in the message body or by appending any number of -a file arguments to the Nail command. After you've finished composing a mail, press Ctrl-D to send it.

To save attachments of received mails, use the w command, followed by the number of the mail and the filename in which to save the message. Nail will ask for a filename for each attachment. If you want to save the attachment only, you can "save" the message in /dev/null: w 14 /dev/null.

Filtering junk

As mentioned above, Nail provides a trainable Bayesian spam filter. You need to specify a junk database in your configuration to use it:

set junkdb=~/.mail/junkdb

You can then train your filter by marking mail as good or bad with the good or bad command. The commands ungood and unbad remove marks. Once the filter is set and trained, Nail tries to detect junk and, if it thinks a message looks like junk, prints a trailing "J" in the subject listing. You can mark false positives as non-spam with the command unjunk.

It might take some time before Nail reliably detects spam. Once you think the filter works reliably enough, you can set up the filter to automatically move incoming spam to a specified folder. For example, the following configuration tells Nail to move incoming spam from my inbox to ~/.mail/junk automatically:

define junkfilter {
  classify (smaller 20000) :n
  move :j ~/mail/junk
set folder-hook-imap://user@host/INBOX=junkfilter

External tools

While Nail has many common email features built-in, there are some jobs it can't do on its own, such as interpreting HTML mail. Therefore, it provides an easy way to utilize other programs. My favorite text browser is elinks, so I added following line to my ~/.mailrc:

set pipe-text/html="/usr/bin/elinks -dump"

I then specified the utility to which to pipe the mail data by the MIME type:

set pipe-[MIME type]="command"

Here are a few more suggestions for further configuration to make life even easier:

To specify the editor for composing email, set the EDITOR variable in your .mailrc file. For instance, this command will set it to use Vim:

set EDITOR=/usr/bin/vim

Once you've set the EDITOR variable, type ~e while in the message body to invoke the editor.

If you don't want to have your mbox file in your home directory, set the MBOX variable to the path where you'd like the mbox file to live:

set MBOX=~/.mail/mbox

To create a shortcut for your mailbox, use the shortcut directive. The following shortcut creates a shortcut called "inbox" that points to a Secure IMAP server:

shortcut inbox imaps://shell-monkey@imap.web.de/INBOX

Nail supports multiple email accounts. To configure a second account, use the following syntax in the .mailrc:

account account-name {
  # configuration of another account

I know, the world has more than enough mail clients already. It's just the same as with any other kind of software -- some people like choice, some do not. I prefer Nail because it provides quick and easy access to my mail without having to know everything about it, while also providing the opportunity to create advanced configurations.

Click Here!