Linux.com

Feature

Fetching email with Mutt

By Shashank Sharma on October 25, 2005 (8:00:00 AM)

Share    Print    Comments   

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.

Shashank Sharma specializes in writing about free and open source software for new users and moderates the Linux.com forum boards. He is the coauthor of Beginning Fedora, published by Apress.

Share    Print    Comments   

Comments

on Fetching email with Mutt

Note: Comments are owned by the poster. We are not responsible for their content.

Mutt Fan

Posted by: Anonymous Coward on October 25, 2005 05:40 PM
I love mutt and use it all the time when I want to check my mail while on travel or in an unsafe location. Because it can be run from a SSH session your mail isn't downloaded or saved to the computer you're accessing your mail through and is encrypted making it harder for someone sniffing packets to peek.

#

Re:Mutt Fan

Posted by: blindcoder on October 25, 2005 07:39 PM
For unsafe locations I also use IMAP+SSL for fetching and a ssh tunnel to my mailserver on port 25 to localhost port 25 for sending.
Just as an alternative when you need your mail local (for attachments, for example).

#

Re:Mutt Fan

Posted by: Anonymous Coward on October 26, 2005 12:22 AM
Unless the e-mail you received is encrypted, it is already "unsafe". The easiest way that I have found to send and receive encrypted e-mail is by creating an account at www.cryptomail.org

#

Another way to have secure mail when you travel

Posted by: Anonymous Coward on October 26, 2005 12:04 PM
Another easy way to have encrypted mail when you travel is to use <a href="http://mobility.shaneland.co.uk/" title="shaneland.co.uk">Mobility Email</a shaneland.co.uk>. This uses GnuPG, so the level of security is very high.

#

Deja vu all over again.....

Posted by: Anonymous Coward on October 28, 2005 11:01 PM
Isnt this a dupe from the past - I swear I have seen this article here before.

Maybe its time to quit taking those time-travel pills.... Woah...

#

Retrieve/Send e-mail messages

Posted by: Anonymous Coward on October 26, 2005 12:56 AM
As a casual Mutt user, I always remember two useful programs to retrieve and send messages. Both are written with Python. The retrieving one is "getmail" and is similar to "fetchmail". The sending one is "putmail". Disclaimer: I'm the author of this last program. "getmail" is probably packaged for your distribution of choice. "putmail" probably is not.

<a href="http://pyropus.ca/software/getmail/" title="pyropus.ca">http://pyropus.ca/software/getmail/</a pyropus.ca>
<a href="http://putmail.sourceforge.net/" title="sourceforge.net">http://putmail.sourceforge.net/</a sourceforge.net>

#

Arrow keys?

Posted by: Anonymous Coward on October 26, 2005 01:03 AM
Have not tried mutt, but in the article I see the 'j' and 'k' keys are used to traverse mail.
I think that it would be much easier if you could use the arrow keys on the keyboard. It would make more sense and be more logical.

#

Re:Arrow keys?

Posted by: Anonymous Coward on October 26, 2005 01:31 AM
You can use the arrows too. The `j' and `k' keys are borrowed from vi.

#

Re:Arrow keys?

Posted by: Anonymous Coward on February 24, 2007 05:33 AM
The origination of the j and k keys comes from the old-style AT&T keyboards used with Unix systems back in the day. The arrow keys were on h,j,k and l. Wierd huh?

#

Errata

Posted by: Anonymous Coward on October 26, 2005 03:07 AM
Being precise:
1. You don't need procmail to read messages with mutt.
2. nano is not the default mutt editor - it all depends on your system settings.
3. You don't need set envelope_from to send mail with mutt - of course set_envelope is a typo there.
4. You should mentione Maildir as the default format of mailboxes - mbox is<nobr> <wbr></nobr>... a bad idea.
5. Good idea is to run fetchmail with -d option - you don't have to run fetchmail every time you want to retrieve your mail.

Regards,
Rafal

#

Re:Errata

Posted by: Anonymous Coward on October 26, 2005 07:51 PM
To be accurate, item 4 is one of those opinion things, along the same lines as which editor is best.

However, if someone's just starting to set things up, I'd have to recommend mbox, simply because it's much easier to find other programs which work on it. They can always convert to maildir later if they wish.

#

Re:Errata

Posted by: Anonymous Coward on October 26, 2005 09:14 PM
Nowadays most of the good email clients do. When it comes to new users I disagree, because:
1. It's easier for a new user to handle with ones mail after a system crash.
2. It only needs very few setting in config files:

    - in<nobr> <wbr></nobr>.muttrc
set mbox_type=Maildir

    - in<nobr> <wbr></nobr>.procmailrc just add "/" at the end of the folder name.
3. It's always a good idea to get new users used to better standards, etc.

Regards,
Rafal

#

Re:Errata

Posted by: Anonymous Coward on October 26, 2005 10:19 PM
I'm not talking particularly about clients, but there you go.

As for points 1 and 3, we'll just have to disagree, I think.<nobr> <wbr></nobr>:)

#

Good for debugging mail

Posted by: Anonymous Coward on October 26, 2005 06:51 AM
Mutt is good for debugging mail problems and it is the only program I know of that can be used to send mail with attachments from a script.

However, using it daily to read mail is only for masochists...

#

Re:Good for debugging mail

Posted by: Anonymous Coward on October 26, 2005 08:25 AM
nail can also send attachments from the command line, so you can use it in a script. Probably others have this capability too.

#

Re:Good for debugging mail

Posted by: Anonymous Coward on October 26, 2005 07:54 PM
.. or for people who have a lot of email and need to deal with it quickly and easily.<nobr> <wbr></nobr>;-)

#

Re:Good for debugging mail

Posted by: Anonymous Coward on May 20, 2006 09:28 AM
or people who can remember five or six shortcut keys and don't like half their screen taken with useless buttons and a quarter of their resources hogged by bloat like GTK+

#

Re:Good for debugging mail

Posted by: Anonymous Coward on February 23, 2007 08:44 PM
and then by what you're opening the image/docs etc?
guess what? still using gtk/kde!!

saving memory by using mutt is so stupid.

#

Re(1):Good for debugging mail

Posted by: Anonymous [ip: 66.248.192.1] on August 21, 2007 06:23 PM
If you spend a majority of your time reading and writing mostly text e-mails, Mutt can be a real time-saver. On the other hand, if you use e-mail for sending and receiving non-text documents, images, etc, Mutt may not be the best choice. Don't try to justify *why* or *how many* people in the business world need to work with office documents and images throughout their day. If you use e-mail simply for mostly text-based communication (golly), there's Mutt.

Oh, and running Mutt while still opening the occasional binary attachment is stupid because...?

#

Re(1):Good for debugging mail

Posted by: Anonymous [ip: 84.39.74.80] on November 14, 2007 10:11 PM
You can do all that in the framebuffer console.

#

Console based mail reader is geek

Posted by: Anonymous Coward on November 07, 2006 02:17 AM
Nowadays, there are good graphical mail clients such as Mozilla Thunderbird, Evolution, etc.

Gmail is nice, very nice with free email that has 2,7 gb space. It has nice features like stars, archive, labels, etc.

Though, I don't like the data retention on Gmail. I wish my privacy was valued more...

#

How should sendmail be configured

Posted by: Anonymous Coward on November 07, 2006 03:22 AM
It makes a huge difference as to how Sendmail is configured. What should it be doing in order to replicate the setup suggested in the article?

#

mutt has POP support built-in

Posted by: Anonymous Coward on November 07, 2006 03:56 AM
you don't need anything other than mutt fool. this article should be voted for most clueless article of the decade!

#

Doesn't work with gmail

Posted by: Anonymous Coward on November 07, 2006 09:27 AM
Mutt's POP support doesn't work with gmail fool. Specifically the there's no support for "TOP"

#

The title is confusing...

Posted by: Anonymous Coward on November 08, 2006 12:10 PM
The title of the article is very confusing, as one does not "fetch" mail with mutt. A better one would have been "Reading email with mutt", or "Getting started with mutt", or "mutt and gmail".

Cheers,
Amarendra

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya