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

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!

.fetchmailrc  << Page 3 of 3  

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!
fetchmail

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





.fetchmailrc  << Page 3 of 3