Using Fetchmail to Retrieve Mail


SMTP is designed to work with systems that have a full-time connection to the Internet. What if you are on a dial-up account? What if you have another system store your email for you and then you log in to pick it up once in awhile? (Most users who are not setting up servers are in this situation.) In this case, you cannot easily receive email using SMTP, and you need to use a protocol, such as POP3 or IMAP, instead.

Note

Remember when we said that some mail clients can include some MTA functionality? Microsoft Outlook and Outlook Express can be configured to use SMTP and, if you use a dial-up connection, offer to start the connection and then use SMTP to send your mail. Therefore, a type of MTA functionality is included in those mail clients.


Unfortunately, many MUAs do not know anything about POP3 or IMAP. To eliminate that problem, you can use a program called Fetchmail to contact mail servers using POP3 or IMAP, download mail off the servers, and then inject those messages into the local MTA just as if they had come from a standard SMTP server. The following sections explain how to install, configure, and use the Fetchmail program.

Installing Fetchmail

Similar to other .rpm files, Fetchmail can be installed with the rpm -i command. This command installs all files to their default locations. If, for whatever reason, you need to perform a custom installation, see Chapter 7, "Managing Software," for more information on changing the default options for rpm.

You can get the latest version of Fetchmail at http://tuxedo.org/~esr/fetchmail. It is available in both source and RPM binary formats. The version of Fedora on the CDs and DVD accompanying this book provides a reasonably current version of Fetchmail and installs useful Fetchmail documentation in the /usr/share/doc/fetchmail directory. That directory includes an FAQ, features list, and Install documentation.

Configuring Fetchmail

After you have installed Fetchmail, you must create the file .fetchmailrc in your home directory, which provides the configuration for the Fetchmail program.

You can create and subsequently edit the .fetchmailrc file by using any text editor. The configuration file is straightforward and quite easy to create; the following sections explain the manual method for creating and editing the file. The information presented in the following sections does not discuss all the options available in the .fetchmailrc file, but covers the most common ones needed to get a basic Fetchmail installation up and running. You have to use a text editor to create the file to include entries like the ones shown as examplesmodified for your personal information, of course. For advanced configuration, see the man page for Fetchmail. The man page is well written and documents all the configuration options in detail.

Caution

The .fetchmailrc file is divided into three different sections: global options, mail server options, and user options. It is very important that these sections appear in the order listed. Do not add options to the wrong section. Putting options in the wrong place is one of the most common problems that new users make with Fetchmail configuration files.


Configuring Global Options

The first section of .fetchmailrc contains the global options. These options affect all the mail servers and user accounts that you list later in the configuration file. Some of these global options can be overridden with local configuration options, as you learn later in this section. Here is an example of the options that might appear in the global section of the .fetchmailrc file:

set daemon 600 set postmaster foobar set logfile ./.fetchmail.log


The first line in this example tells Fetchmail that it should start in daemon mode and check the mail servers for new mail every 600 seconds, or 10 minutes. Daemon mode means that after Fetchmail starts, it moves itself into the background and continues running. Without this line, Fetchmail would check for mail once when it started and would then terminate and never check again.

The second option tells Fetchmail to use the local account foobar as a last resort address. In other words, any email that it receives and cannot deliver to a specified account should be sent to foobar.

The third line tells Fetchmail to log its activity to the file ./.fetchmail.log. Alternatively, you can use the line set syslogin which case, Fetchmail logs through the syslog facility.

Configuring Mail Server Options

The second section of the .fetchmailrc file contains information on each of the mail servers that should be checked for new mail. Here is an example of what the mail section might look like:

poll mail.samplenet.org proto pop3 no dns


The first line tells Fetchmail that it should check the mail server mail.samplenet.org at each poll interval that was set in the global options section (which was 600 seconds in our example). Alternatively, the first line can begin with skip. If a mail server line begins with skip, it is not polled as the poll interval, but is polled only when it is specifically specified on the Fetchmail command line.

The second line specifies the protocol that should be used when contacting the mail server. In this case, we are using the POP3 protocol. Other legal options are IMAP, APOP, and KPOP. You can also use AUTO herein which case, Fetchmail attempts to automatically determine the correct protocol to use with the mail server.

The third line tells Fetchmail that it should not attempt to do a DNS lookup. You probably want to include this option if you are running over a dial-up connection.

Configuring User Accounts

The third and final section of .fetchmailrc contains information about the user account on the server specified in the previous section. Here is an example:

user foobar pass secretword fetchall flush


The first line, of course, simply specifies the username that is used to log in to the email server, and the second line specifies the password for that user. Many security conscious people cringe at the thought of putting clear-text passwords in a configuration file, and they should if it is group or world-readable. The only protection for this information is to make certain that the file is readable only by the owner; that is, with file permissions of 600.

The third line tells Fetchmail that it should fetch all messages from the server, even if they have already been read.

The fourth line tells Fetchmail that it should delete the messages from the mail server after it has completed downloading them. This is the default, so we would not really have to specify this option. If you wanted to leave the messages on the server after downloading them, use the option no flush.

The configuration options you just inserted configured the entire .fetchmailrc file to look like this:

set daemon 600 set postmaster foobar set logfile ./.fetchmail.log poll mail.samplenet.org proto pop3 no dns user foobar pass secretword fetchall flush


What this file tells Fetchmail to do is

  • Check the POP3 server mail.samplenet.org for new mail every 600 seconds

  • Log in using the username foobar and the password secretword

  • Download all messages off the server

  • Delete the messages from the server after it has finished downloading them

  • Send any mail it receives that cannot be delivered to a local user to the account foobar

As mentioned earlier, many more options can be included in the .fetchmailrc file than are listed here. However, these options get you up and running with a basic configuration.

For additional flexibility, you can define multiple .fetchmailrc files in order to retrieve mail from different remote mail servers while using the same Linux user account. For example, you can define settings for your most often used account and save them in the default .fetchmailrc file. Mail can then quickly be retrieved like so:

$ fetchmail -a 1 message for bball at mail.myserver.com (1108 octets). reading message 1 of 1 (1108 octets) . flushed


By using Fetchmail's -f option, you can specify an alternative resource file and then easily retrieve mail from another server like this:

$ fetchmail -f .myothermailrc 2 messages for bball at othermail.otherserver.org (5407 octets). reading message 1 of 2 (3440 octets) ... flushed reading message 2 of 2 (1967 octets) . flushed You have new mail in /var/spool/mail/bball


By using the -d option, along with a time interval (in seconds), you can use Fetchmail in its daemon, or background mode. The command launches as a background process and retrieves mail from a designated remote server at a specified interval. For more advanced options, see the Fetchmail man page, which is very well written and documents all options in detail.

Caution

Because the .fetchmailrc file contains your mail server password, it should be readable only by you. This means that it should be owned by you and should have permissions no greater than 600. Fetchmail complains and refuses to start if the .fetchmailrc file has permissions greater than this.




Red Hat Fedora 5 Unleashed
Red Hat Fedora 5 Unleashed
ISBN: 067232847X
EAN: 2147483647
Year: 2004
Pages: 362

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net