|[Home] [Credit Search] [Category Browser] [Staff Roll Call]||The LINUX.COM Article Archive|
|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 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.
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.
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.
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:
$ rpm -q sendmail <Enter>
$ 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!
All right, now that sendmail is setup, let's go on to pine.
- 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 "firstname.lastname@example.org," 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.
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 "email@example.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 firstname.lastname@example.org. 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 "email@example.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:
at the prompt.
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 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 firstname.lastname@example.org; email@example.com