ALINK="#FF0000">

"Linux Gazette...making Linux just a little more fun!"


Mail for the Home Network

Fetcahmail

By JC Pollman and Bill Mote


Fetchmail Setup

Fetchmail

Fetchmail will get mail from your email on the net (your ISP account and wherever else you have an email account) and pass the mail off to procmail running on your mail server to put it in your mail account on your server. It can handle a wide variety of mail servers including POP3 and IMAP, but it can not handle an Exchange Server.

Although fetchmail can be configured to handle quite a variety of chores, we will keep it simple here. For each user that has an email account on the internet create a file: ~/.fetchmailrc   For user jpollman on our server, the file /home/jpollman/.fetchmailrc looks like this:

poll www.deniz.com with proto POP3
       user "jpollman" there with password "mypassword" is jpollman here

Explaining each part:
    poll www.deniz.com: contact www.deniz.com which is my ISP's pop3 server.
    with proto POP3: use the pop3 protocol to get the mail.
    user "jpollman" there: my login name for email on my ISP.
    with password "mypassword": this is my password to my ISP.
    is jpollman here: jpollman is the user on our home mail server.

Put a .fetchmailrc file in the home directory of each user that needs to get mail from the internet.  Note: each .fetchmail will be a little different because everyone has a different email address on the internet. Also, fetchmail is very particular about permissions and ownership - which is a good thing as email passwords need to be kept private.  To make sure .fetchmailrc is correct, using the example above for jpollman:

chown jpollman /home/jpollman/.fetchmailrc [Enter]
chmod 700 /home/jpollman/.fetchmailrc [Enter]
If a user has more than one account on the internet, just add additional lines starting with: poll.

We could have set fetchmail to run as a daemon and poll the ISP every so often, but fetchmail will cause diald to call out if we are not connected. Instead we have a simple script, I call it: getmail, that is called both by cron and by ip-up.local - see below for examples.  Here it is:

#!/bin/sh

if [ -f /var/lock/LCK..ttyS3 ]; then
        su jpollman -c fetchmail
        su bmote -c fetchmail
fi

When it runs, it first checks to see if we are connected (diald puts a file called LCK..ttyS3 in my /var/lock directory when it is connected, and removes it when it disconnects.)  Note: you may have set diald up differently than we did, or may be using pppd daemon, or using a different com port, so the lock file may have a different name. This script exits if the lock file is not there.  If we are connected, the script will use su to become jpollman and executes the fetchmail program. The -c means; run the following command. After fetchmail is has gotten jpollman's mail, the script becomes bmote via su and runs fetchmail to get his mail. Note: jpollman and bmote are user names on our server - not the names for their ISP email accounts.

Cron: here is a really quick, down-n-dirty, tutorial on cron. Crond is the daemon that is started when your machine boots, and so, is always running in the background. It reads the crontab files every minute to see if anything needs to run. You need to create a crontab for user "root". To do this, as root, type:

crontab crontab [Enter]
Root now has his own crontab file - which is identical to the system's crontab file. To edit root's crontab, type
crontab -e [Enter]
Leave all the header line alone and delete the program lines. When you are done, it should look something like this:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
HOME=/
MAILTO="root"
To make cron run a program, add a line with 6 fields: the first five are the time fields and the six field is the program. As an example:
0 1 * * * getmail
The time fields are broken down this way:

       The time and date fields are:

              field          allowed values
              -----          --------------
              minute         0-59
              hour           0-23
              day of month   0-31
              month          0-12 (or names, see below)
              day of week    0-7 (0 or 7 is Sun, or use names)

       A  field  may  be an asterisk (*), which always stands for
       ``first-last''.

       Ranges of numbers are allowed.   Ranges  are  two  numbers
       separated  with  a  hyphen.  The specified range is inclu-
       sive.  For example, 8-11 for an ``hours'' entry  specifies
       execution at hours 8, 9, 10 and 11.

So, in the above example (0 1 * * * getmail) getmail will run at 1 o'clock in the morning everyday. I would prefer to have it run every 5 minutes, so my crontab entry looks like this:

0-59/12 * * * * /usr/local/bin/getmail
To find out more about the crontab file, type:
man 5 crontab [Enter]
ip-up.local: to have the script run every time you connect, just add it to your /etc/ppp/ip-up.local (or ip-up file if that is all you have). To add it, just type it in as a single line with full path. Mine looks like this:
/usr/local/bin/getmail


Copyright © 1999, JC Pollman and Bill Mote
Published in Issue 45 of Linux Gazette, September 1999