Fetching email with Mutt

8452

Author: Shashank Sharma

What do you look for in an email program? You may find it in Mutt, an easy-to-use text-based messaging client. Here’s all you need to know to get started with Mutt.

To read mail from a POP account using Mutt, you need to have a mail transfer agent such as Sendmail and a mail delivery agent such as Procmail installed, as well as a program that can connect to a POP server and download your emails, such as fetchmail. Most standard *nix systems, have all these programs installed.

If everything is in place, let’s configure Mutt and the other programs to read Google’s Gmail messages, as Gmail allows POP forwarding.

Configuring fetchmail

You can use fetchmail to connect to Gmail’s POP server, log into your account, and retrieve messages. To configure fetchmail, you’ll need to know details about the POP mail server, such as its address and port number. Instructions on enabling POP forwarding in Gmail is available at Google’s help center page.

Next, you need to create a .fetchmailrc file in your home directory that contains these lines:

poll SERVER with proto PROTOCOL
user REMOTE_USER there with password REMOTE_PASSWORD is LOCAL_USER here

The words in capital letters need to be replaced with actual values. SERVER is your POP server. PROTOCOL refers to the protocol you want fetchmail to use to connect to the remote servers. REMOTE_USER is your email ID. LOCAL_USER is the user account on your machine. Below is my .fetchmailrc, configured to retrieve mails from my gmail account:

poll pop.gmail.com with proto POP3 and options no dns
user 'linuxlala@gmail.com' is 'linuxlala' here options ssl

You can see I used some additional options. no dns means that a dynamic IP address is allocated to your machine. The second line asks fetchmail to connect to the Gmail server using SSL.

To test whether fetchmail works, open a terminal and enter fetchmail -vk . The -v switch enables verbose mode, so you can see what fetchmail is doing. The -k switch ensures that messages are not deleted from the server when they are downloaded.

if all goes well, you’re now ready to use Mutt to read email.

Starting Mutt

Start by typing mutt at the command line. The first time you run Mutt, it will complain that the home directory does not contain a directory called Mail. Mutt will create it for you, along with a file called “mbox” that stores all your messages.

When you run Mutt, you’ll see an index list of messages. To move up and down through the list of messages, use the “k” and “j” keys respectively.

Pressing Enter on the selected message, drops you to the pager, which is akin to a text viewer, much like the *nix program less. You can use the backspace and Enter keys to scroll through your messages. Most of the commands that work in the index work in the pager as well; for instance, pressing “j” or “k” in the pager shows the content of the next-in-line message.

If the received message includes an attachment, you can use “v” to launch the attachment menu. If the received message contains more than one attachment, use the “k” and “j” keys to move up and down through them. Pressing Enter displays an attachment in the pager only if it can be viewed as plain text. If not, Mutt tries to launch a suitable external viewer for the file.

To quit the attachment viewer use “q,” which drops you back to the pager. Press “q” again to return to the index.

Mutt displays a list of all the essential keyboard shortcuts at the top of the screen, and the list changes according to where you are in the program.

From the index, you can reply to a message or create a new one. Pressing “m” creates a new message. Mutt prompts you for the recipient’s email address and the message subject. When you hit Enter after writing the subject, Mutt opens the nano text editor so you can write your message. Once you’ve composed your message, press Ctrl-o to save the message. You will be prompted to provide a name. You can press Enter to use the default, which is in the format mutt-yourhostname-xxxx-x, where the “x”s are numbers. The message is saved in the /tmp directory. After saving the message, press Ctrl-x to quit nano and move to a new screen, where you can review the subject and the recipient’s email address, and under a heading called Attachment you can see the message you just saved, along with “I” written at the extreme left, which indicates that this attachment is being sent in-line in the body of the message.

If you want to attach another file, press “a.” You can then type the location of the file or press “?” for a selection list. You can press “c” to change to a different directory. After selecting the file, you are again dropped to the review screen. Now, under Attachment, two files are listed. Instead of an “I” at the extreme left, the second file appears with “A,” with represents an external attachment.

To send the message, press “y.” Once the email is sent, Mutt deletes the text you saved in the /tmp directory.

The procedure for replying to a message is similar to the one used for creating a new mail. You press “r” to reply from either the pager or the index. Mutt picks up the sender’s ID to fill in the To: field.

Try sending a message to yourself. You should see in the sender field username@localhost.localdomain, because you have not yet configured Mutt to use your POP account to send email.

Configuring Mutt

To configure Mutt, you need to create a .muttrc file in your home directory. Read the sample .muttrc file from /usr/share/doc/mutt/example/muttrc to get an idea of what to put in the file, and take a look at the dotfiles site, where people share their .muttrc files. Here’s an excellent .muttrc, which is very well commented.

To send mail using Mutt, your .muttrc needs to contain a set_envelope field. A basic .muttrc looks like this:

# My email address
set pop_user="username@gmail.com"

# My email account password
set pop_pass="password"

# Too many email headers make reading a message difficult
ignore *
unignore From: To: Cc: Subject: Date: #Only these are shown in the header

#To ensure that mutt does not put
#'username@localhost.localdomain in From
set from="username@gmail.com"
set use_from=yes
set envelope_from="yes"

#The text editor I want to use to write emails
#The default is nano
set editor=vi

Filtering mail

In addition to sending and receiving messages, you may be filtering email with Procmail. If your filters send messages to mailboxes other than the inbox, you need to tell Mutt about which mailboxes you have. To do so, add lines similar to the following to your .muttrc:

set folder = /home/linuxlala/Mail
mailboxes /home/linuxlala/Main/default
mailboxes /home/linuxlala/Mail/buddies
mailboxes /home/linuxlala/Mail/cc-license
mailboxes /home/linuxlala/Mail/indlinux
mailboxes /home/linuxlala/Mail/spam

You’ll also have to tell fetchmail to run the emails through Procmail to be sorted. Instead of entering only fetchmail -vk , run fetchmail -vk -m "/usr/bin/procmail -d %T". The -m switch informs fetchmail that all email now needs to be forwarded to a mail delivery agent, and the string within quotes provides the path to the MDA (Procmail).

I enjoyed learning how to use Mutt with fetchmail and Procmail. I found the setup process to be a bit lengthy, but I considered it a good learning opportunity. Today, this Mutt is, indeed, a man’s best friend.

Shashank Sharma is studying for a degree in computer science. He specializes in writing about free and open source software for new users.