Originally Published: Thursday, 27 September 2001 Author: Michael & Melinda Petruzziello
Published to: learn_articles_firststep/General Page: 1/1 - [Std View]

Beginner's Week: Notes from the Command-Line Commando: Fetchmail

In this, the first of a series of articles dedicated authors Michael and Melinda Petruzziello try a unique writing style to convey everything you need to know to start being a command line commando. It's fun writing and useful stuff on setting up fetchmail. Read on!

The Linux command line: The true power of the Linux server! (But is it for the home user?)

The Linux command line is one of the most useful and versatile features of your computer. It is powerful in the freedom it provides the user or administrator. You can setup, run, revise, and tweak just about any program that you can get for Linux. Add a secure telnet session and presto! You have a simple, secure way to administer your network from a remote location in ways that most operating systems wouldn't allow without additional software or hardware. For example, as those of you familiar with Linux probably already know, you can setup, configure, and run Web, FTP, POP, and many other services right from the command prompt.

Since we know that Linux has all this functionality for servers, where does this leave the home user? Can the home user benefit from the Linux command line just as much without having to rely on "gooey" X-windows interfaces? I'm here to answer this question with a resounding: "Yes!"

In this series of articles, I want to focus on how the home user can benefit from Linux command line utilities for email, web browsing, FTP, telnet, and even word processing and multimedia!

Since this is the first article in the series, let me give you a brief overview of my computer experience, and my own personal Linux box.

Back In The Day

My humble computer experience began in Derry, New Hampshire, when my uncle ordered the first do-it-yourself home ENIAC system. We had to set it up in my uncle's barn. It wasn't much of a machine. Though quite impressive in size, it had no memory and no data storage space. My dreams of playing Wolfenstein 3D were less than what I'd hoped for. It took us nearly four hours just to install the pre-alpha version, and I was always running back and forth between switches Alpha-459 and Zeta-006, and that was just to fire. Movement was nearly impossible. I'd just have to hope that I'd materialize in a convenient-to-defend corner every time I got fragged.

All right, all right! I didn't actually start out on an ENIAC (but it sure did sound good, didn't it?) My first computer was the Atari 400. Although not as impressive looking as the ENIAC, it did come in a convenient size that would actually fit in your house. It had a whopping 4 KB of RAM and a tape device for storing data. Not too bad, since any old cassette would do in a pinch. Once you plugged in the BASIC cartridge, it looked like it had a command prompt, but it was actually just a BASIC interpreter. I was mildly disappointed by this, but the fact that I had a 27-inch monitor was some compensation. In addition to having no command line, the Atari 400 had other problems as well. The keyboard was as flat as a tabletop, which made typing more like drumming your fingers. This presented issues, since most programs available for the Atari had to be typed in by hand and then stored on tape, because the Atari 400 could only have one program loaded into memory at any given time (multi-tasking, where are you?).

The most memorable system meltdown I ever had with my Atari was when I was typing in the "Ghengis Khan" adventure game out of a magazine, and about half-way through, my parents decided they wanted to watch "Starsky and Hutch." Flipping the game switch on the back of the TV, they disabled my 27-inch monitor and accidentally knocked out the Atari's power cord, erasing all traces of the program from memory. Blisters notwithstanding, I re-typed the whole program in the next day, and it promptly did not work.

I went through several different Atari systems as time went on, some of which even had real keyboards (no more blisters!), gooey interfaces, and multi-tasking, but still no REAL command line (waaaah!). Then one day, I saw an IBM 386, and there it was: a command line! Multi-tasking was hopelessly beyond it, and it often said rude things like "Invalid command: <A>bort, <R>etry, <I>gnore", and then made funny little beeping sounds that I am convinced to this day sounded like laughter. In spite of all this, I was still pleased. I was finally experiencing a computing environment I enjoyed.

Finally, in 1996, I discovered a true multi-tasking, command-line environment: Linux. I nearly fainted. Not only could I log in, I could log in several times. I could FTP, Telnet, read email, and browse the Web from different screens, all on the same computer. I was in command-line heaven.

My Current Setup

In 1998, I started my own computer consulting firm, http://www.JMPTechnologies.com. I work on Linux, SCO, and all versions of Windows. While each OS has its place in the computing hierarchy, when I come home, I like to sit down in front of my very own Linux box. It's a Pentium 233 with 32 MB of RAM and a 4 GB hard drive running RedHat 6.2. (I know, the distribution argument goes on and on, but RedHat is the first one I ever used, and I've just stuck with it.) I have my Linux box attached to my network with an NE2000 compatible card, which is plugged into a Linksys Broadband/DSL router for Internet access. Everyone else in the house is running that Windows gooey thing (yuk!).

Finally: Fetchmail

Now that you've read about my computer experience and my Linux box, it's time to get to the meat of this article: fetchmail, for personal use.

One of the first things I wanted to do when I first set up my Linux box was send and receive email. To read my email, I use Pine v4.21 (which I think is the latest version at this time). Sending and receiving email to users on the system or in a network environment is easy with Pine, but what if you want to receive email from a source outside your network? Fetchmail is the answer.

First Things First

Make sure you have your Linux box set up for email support. You will need to have at least three email utilities installed:
  • sendmail, the MDA (mail delivery agent)
  • fetchmail, for retrieving email from sources outside your network
  • pine, for reading and composing your email

    When I installed RedHat 6.2, I selected these utilities as part of my install. If you did not install these utilities during setup and you use RedHat, install them from the RPMs included on your installation CD. If you don't have a CD or if you are using a different version of Linux, then you may have to get these files from the web. If you are not sure if these packages are installed, try the following:

    login: <username>
    Password: *******

    $ rpm -q sendmail <Enter>

    You should see something like the following, if sendmail is installed:


    or you might see:

    package sendmail is not installed

    If the reply comes back "package not installed," then obviously the package needs to be installed. Follow these steps to make sure fetchmail and pine are installed also, and then take the appropriate action necessary. Again, I am a RedHat guy, so this may vary on your system.

    All right! I got everything; let's get on with it!

    Sendmail Setup

    This section is very brief. If sendmail is installed properly on your system, then its default settings should work just fine. If not (ahhhhh!), check the various HOW-TO sites on the web. This I know, however; RedHat's default sendmail installation works peachy (gratuitous RedHat plug -- thank me later, Mr. Young).

    All right, now that sendmail is setup, let's go on to pine.

    Pine Setup

    Here, we come to the easy part. Login and start pine. On the initial screen, arrow down to Setup and press <Enter>. You will see a menu at the bottom of the screen; press <C>. You will then see another menu. The top three options are the ones we need to check:

    - The top one is "personal-name." Highlight it by using the arrow keys and press <C>. This will allow you to change the information there. Type in your name, if it's not there already, and press <Enter>.

    - The next one on the list is "user-domain". Highlight it and press <C>. Type in the last part of your email address after the @ symbol and then press <Enter>. For example, if your email address is "frank@mail.server.com," you put "mail.server.com."

    - The next one is "smtp-server". Highlight this, press <C>, type in your SMTP (or outgoing mail) server name, and press <Enter>.

    Press <E> to exit Setup. Pine will ask if you want to save changes; type <Y>for yes, and you will find yourself back at the main menu. Press <Q> to quit pine.

    Fetchmail Setup

    Fetchmail is a very powerful program and can handle various email-related tasks. For example, I use fetchmail to handle all the email of a major client of mine. Fetchmail goes out, grabs the mail, and then separates it (this involves a script) into individual mailboxes for each user. Employees login individually with a POP3 email client and get their mail. However, since this article is focused on how to use fetchmail for your personal email, I will be leaving most of the beefy stuff out for now.

    Before you configure fetchmail, I recommend that you have a username that is the same as your email login name. For example, if your email address is "frank@mail.server.com," setup a username of "frank." I highly recommend doing this because it keeps the confusion down. (As we go through this example, you will see what I mean.)

    OK, time to set things up. Let's start by logging in with the username you will be using to receive the email (NOT as root). Now, there is more than one way to invoke fetchmail. If you type:

    fetchmail mail.server.com <Enter>

    at the command prompt, fetchmail will prompt you for a password, and then check for email (IMAP and POP3) at the server mail.server.com with whatever username you happen to be logged in with. For example, if you are logged in as "frank," it will check frank@mail.server.com. If your email name is not the same as your username, don't give up hope. If you are logged in as "frank," but your email address is "franklin@mail.server.com," you can type this instead:

    fetchmail -u franklin mail.server.com <Enter>

    Then, fetchmail will contact mail.server.com using the IMAP and POP3 protocols, and try to login as "franklin." You will then be prompted for your password. Type it in, press <Enter>, and presto! You've Got Mail! (Ahhhh! Did I say that? Sorry!)

    Let's say you're tired of fetchmail checking both the IMAP and POP3 protocols, because you only have a POP3 account. To get rid of the IMAP error message you keep seeing, type:

    fetchmail -p pop3 mail.server.com <Enter>

    And, if your username is different than your login name, type:

    fetchmail -p pop3 -u username mail.server.com <Enter>

    You will then see a prompt for your password. Again, type it in, etc., etc., ad nauseam.

    Now, if you're like me, you're not going to want to type all this information in every single time you want to check your mail. And so, we come to the subject of the .fetchmailrc file. Once you have a .fetchmailrc file setup properly, you can just type:

    fetchmail <Enter>

    at the prompt.

    Let me show you how to set one up.

    At the command prompt, use your favorite text editor (pico or vi or whatever you like) to create a file in your HOME directory called .fetchmailrc (don't forget the period!). Type in the following:

    poll mail.server.com # your mail server name
    protocol: pop3 # could be pop3, imap, or both, depending on your ISP
    username: username # hopefully, the same as your login name
    password: password # be aware, this can be seen by anyone with access to your login

    You may be concerned about the fact that your password is sitting in clear text in this file. If you want to be prompted for your password each time fetchmail checks your email, you can leave out the "password" line in your .fetchmailrc file. Also, if your email username is the same as your login name, you can omit the "username" line from the .fetchmailrc file.

    Now, save the file. Next, we need to use chmod to change the file permissions. At the prompt, type:

    chmod 710 .fetchmailrc <Enter>

    Now type in:

    fetchmail <Enter>

    Fetchmail will check for email using all the information you specified in the .fetchmailrc file, and download whatever email it finds into your mailbox.

    If you have new email (you will know by the status messages fetchmail will display), then bring up pine, and press <I> to take a look at your Inbox.

    Another bonus of having a .fetchmailrc file is that if you want to check more than one email account, on the same or even different servers, all you have to do is add another section to the .fetchmailrc file. (Snazzy, ain't it?)

    Having said all that, I now say this: use the .fetchmailrc file. It takes a little more time to setup initially, but you can't beat it for ease of use or flexibility.

    OK, now you've got your .fetchmailrc file setup, and you're checking six of your high-volume email accounts just by typing in "fetchmail" at the command prompt. Still, if you're like me, you're thinking about how inconvenient it is to have to remember to type "fetchmail" every time you want to check your email. "Isn't there some way we can automate this?" you ask. The answer is "yes," and there are a couple of ways to do it.

    What I like to do is edit my login profile, .bash_profile. Some login profiles differ in filename or other stuff based on what shell you're using (I use bash), but work the same in general. Bring up your login profile in your favorite editor, and add the following lines to the end of the file:

    echo Please wait checking mail!

    Save the file, logout, and log back in again. This will run the fetchmail program and go get your mail. (Neat-o, huh?)

    "Hey, listen," you say, "are you trying to tell me I've gotta logout and login every time I wanna check my mail? Come on, can't you do better than THAT??" Again, I say, "Why, yes!" Instead of just adding "fetchmail" to your login profile, you can add this instead:

    fetchmail -d 120

    Save the file, logout, and log back in. Every two minutes (120 seconds), fetchmail will check your email for you, without you having to lift a finger. (If you don't feel like logging out to test this, just run the command at the prompt.)

    Now for a brief explanation of the change we just made. The -d starts fetchmail as a daemon that runs in the background. The number after it is the number of seconds it waits before it checks again. For example, "fetchmail -d 900" would check mail every 15 minutes. If you type in "fetchmail" while the daemon is running, you will wake the daemon. It will check your mail immediately, and then wait for however many seconds you specified the last time you ran "fetchmail -d." If you decide you are sick of your fetchmail daemon running, type in:

    fetchmail -quit <Enter>

    at the command prompt. That will stop the rascal!

    Well, I hope that was useful and informative; I aim to please. Wait! Harken! Never mind; that was it. If you enjoyed this article and/or found it useful, tune in next time to find out how to stream audio over the Internet from your command line. No "gooey" needed!

    Dear Reader: Since we penned this article, we've since upgraded to RedHat 7.1 with the latest kernel. All of the instructions above still work just fine.

    October 2000, Michael & Melinda Petruzziello michael@jmptechnologies.com; melinda@jmptechnologies.com