Complete.Org: Mailing Lists: Archives: discussion: September 1998:
[aclug-L] HEre it is, the best procmail example ... IMHO
Home

[aclug-L] HEre it is, the best procmail example ... IMHO

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: ACLUG Mailing List <aclug-L@xxxxxxxxxxxx>
Subject: [aclug-L] HEre it is, the best procmail example ... IMHO
From: JOHNSTONE JEREMY S <jsjohnst@xxxxxxxxxxx>
Date: Tue, 15 Sep 1998 13:43:12 -0500 (CDT)
Reply-to: aclug-L@xxxxxxxxxxxx

Here is the procmail article straight from Linux Journal 

<OPINION>
BTW, to all who don't subscribe, its cheap (relative to the info you learn) 
and YOUR SUPPORTING A VALUABLE LINUX RESOURCE
<a href="http://www.linuxjournal.com";>Linux Journal online</a>
</OPINION>

Hope this info works for you
Jeremy

Linux Journal

Linux Apprentice

Setting Up E-mail

This article gives step-by-step instructions to set up your e-mail configuration properly and an overview of various pieces of e-mail software.

by Jonathan Walther

In my time on IRC (Internet Relay Chat), I found that the number one problem newbies have, after setting up PPP, is e-mail setup. Many people get their e-mail half-working and leave it at that, for fear of breaking it and not knowing how to fix it.

Mail handling is split up between several types of programs, instead of having everything handled by a single program (such as Eudora). Trust me, it is conceptually easy, and once you understand it, very quick and easy to set up. It also gives you a lot of flexibility.

I've included three sidebars which you may wish to read first; in particular, the Glossary for definitions of terms I will be using. Although there are alternative programs, I'll discuss the ones that I use.

Glossary

Prerequisites

First, you need to get an e-mail account set up by your ISP with mail delivered to their machine. Then, you need to know your ISP's domain and your password on the mail server (usually the same as the password you use when you start up PPP). For the examples in this article, we will call the ISP domain foo.com, the user name barney and the password f00bar.

To begin, you need the following programs or their alternatives:

  • pine [or elm]
  • either smail or sendmail
  • fetchpop [or fetchmail]
  • procmail

To find out if these programs are installed on your system, use the command which. If you type which program at the prompt, the pathname of that program, if it exists in your path, will be returned to the screen. For example, which might return the path /usr/bin/program, letting you know that program is in the /usr/bin/ directory. All of the basic distributions (Slackware, Debian and Red Hat) usually include all of these programs except fetchpop, so you should have no problem finding and installing them.

fetchpop sources can be downloaded from http://snakepit.wasteland.org/fetchpop.tgz (special thanks to TheAsp for hosting this prepatched version on his server so downloading it from Sunsite is unnecessary).

Pine

For this example, Pine is your MUA (mail user agent). You use it to read and write mail. Start up pine by typing pine at the prompt. When you see the opening screen, type s to call up the setup menu, then type c to go to the configuration screen. Now do the following steps.

  1. Press enter and type in the personal name you wish to be associated with your actual e-mail address. Mixed case and spaces are allowed (e.g., Barney Fallon). In this case, other people will see your address as Barney Fallon (barney@xxxxxxx).
  2. Set smtp-server to localhost to ensure that mail gets handed to your local MTA (mail transport agent), where you don't have to worry about it. The MTA then sends it off the next time you connect. (See Glossary and Generic Mail Flow sidebars.)
  3. Leave the other options set to the defaults for now.

To send mail from the prompt or in a script, type:

pine person@xxxxxxxxxxx < message.file

Smail/Sendmail

Use smail or sendmail as the MTA (mail transport agent). You use it to transport mail between machines. People have written 600 page books on setting up these programs. The horror stories are numerous. For this simple application, in most cases, you don't have to do any set up. If you use Slackware, Debian or Red Hat, the default setups for smail and sendmail work fine right out of the box.

Some installation scripts may ask you whether to use a smarthost, which means it will hand mail to your ISP's mail server so it can do the delivery. That decision is up to you. There's not much difference. If you plan to receive mail on your machine, you might want to look at the /etc/aliases file some day.

If your user name at your ISP is barney@xxxxxxx and you wish this name to show up in the From: header, use sendmail to handle it by taking the following steps:

  1. Create a user named barney on your system.
  2. Edit the /etc/sendmail.cf file line that begins with the characters #DM by changing it to masquerade the Domain (DM--Domain Masquerade), e.g., DMfoo.com. If your machine has a hostname, but not a domain name (i.e., it is not networked except via your ISP), you may also need to edit the line #Dj$w.Foo.COM to remove the # and change Foo.COM to your ISP's domain name. Finally, edit the DS line to name the Domain Smart mail host, if you wish your ISP to actually forward all your mail.
  3. Once the changes are made and saved, just execute SIGHUP sendmail, so sendmail will re-read the sendmail.cf file.

Sometimes smail or sendmail will die. You will then get a pine error message saying the SMTP connection is not available . If this occurs, log in as root and type smail -bd or sendmail -bd. If this type of failure happens consistently, the first thing to check is your /etc/rc* directories to be sure that your SMTP server is started at boot up.

Checking the Mail

Sendmail and smail include two very handy programs for mail checking:

  • mailq lets you see any mail that is in the queue (i.e., not sent). If you aren't connected, you can use mailq to check the queue and to remove a mail, if you decide not to send it.
  • runq gives smail and sendmail a ``kick in the pants'' to start sending out the mail that's in the outgoing queue. This command is useful if your sendmail or smail is set up to send the mail at some specified interval, and you want to send it earlier. Many installations have the default interval set to 60 minutes. Not all distributions include runq. Another way to accomplish the same result is to type sendmail -q at the prompt.

Fetchpop

I chose fetchpop because I think it's the easiest program to set up. Download the sources (since it comes with none of the major distributions), compile it and install it as root. If you are brave, you can download it from Sunsite and apply the patch yourself, but the source at the URL given earlier has the patch already applied, so it will work for everyone. Without the patch, it will fail to connect to certain POP (Post Office Protocol) servers, with no damage.

Now, run fetchpop without parameters. (Substitute your personal information in the place of the example names used below.) Fetchpop will prompt you for your POP server. Enter foo.com. Then, it will ask for your user name and password. Enter barney, then f00bar. Fetchpop will then write the information to the ~/.fetchhost file. Then, to get your mail, type:

fetchpop -arbp
In the testing stages, to reassure yourself that it works, use only the -a option. If you use the -r option, any messages you fetch are removed. The -p option causes fetchpop to filter the mail through procmail. Without the -p option, the mail is dumped in your default mailbox (see Mailboxes sidebar), /var/spool/mail/barney.

For more information on fetchpop, read the man page--it explains a lot of other details.

Procmail

Procmail is an MDA (mail delivery agent) and is used for ``sorting'' e-mail. The standard method is for the MTA to pass mail to procmail one message at a time, where it then checks its configuration file and decides what to do based on what's in the mail. Normally, it puts it in an appropriate mailbox. If you are a concerned parent, you could use procmail to direct all mail containing swear words to /dev/null. If you do a lot of hacking, you can set procmail up so a special ``trigger'' e-mail would encrypt your hard drive--a quick e-mail containing the code word will lock the box up tight. Or suppose you receive an e-mail from someone important; mail from a particular address could invoke a script to beep you on your pager.

To ensure that your incoming mail is filtered through procmail, create a file called ~/.forward containing the following line:

|exec /usr/bin/procmail
Then, change permissions on this file using the command:

chmod 644 ~/.forward
Check the sendmail man page for the particulars on other uses of the ~/.forward file.

Edit ~/.procmailrc by adding the following lines at the beginning:

MAILDIR=$HOME/mail
DEFAULT=misc
The default misc folder is the mailbox that receives all mail not put in another box. Generally, this will be personal mail.

Let's put our first ``filter'' in the file. Here is your basic ``recipe'' in all its glory:

:0
* ^.*linux-kernel@xxxxxxxxxxxxxxxx
linux-kernel
Let's break it down line by line.

:0 indicates the start of a new recipe. You have to use it whenever you put in a new rule for sorting mail.

Each rule line must begin with an * (asterisk) followed by a space. What does ^.*linux-kernel@xxxxxxxxxxxxxxxx mean? The ^ (caret) means ``at the beginning of a line''. The . (period) means ``match any character''. The * means match any number of the preceding character(s). So, .* means match any number of characters that don't match the following characters. linux-kernel@xxxxxxxxxxxxxxxx is the name of the Linux Kernel mailing list. Altogether, that rule matches any line in the e-mail header that contains linux-kernel@xxxxxxxxxxxxxxxx--if a match is found, it's a safe bet the e-mail came from that mailing list. That rule has worked fine for me for several months now.

Now, the final line, linux-kernel, tells procmail that if the rule above was matched, to store that piece of mail in the mailbox named linux-kernel.

That was easy, eh? Let's do another example.

:0
* ^Reply-To:.*mindanao-l@xxxxxxxxxxxx
mindanao-list
Much like before, except to pick out mail from this mailing list, I only need to look for lines that have the mindanao address in the Reply-To: field of the header. If I put * ^.*mindanao-l@xxxxxxxxxxxx, I might find that personal e-mail that were carbon copied to that mailing list would also get sent to the mailing list mailbox instead of my private one.

Here's one final example. Sometimes, two separate rules are needed to sort all the mail from one mailing list. If I were more knowledgeable about regular expressions, I could easily condense these two rules into one. But I'm not, so here they are:

:0
* ^To:.*wmaker@xxxxxxxxx
WindowMaker

:0
* ^Cc:.*wmaker@xxxxxxxxx
WindowMaker
Can you see what these rules do? They ensure that all mail, having lines that begin with To: or Cc: and contain wmaker@xxxxxxxxx elsewhere in the line, is put into the WindowMaker mailbox. Look at the man pages for procmailrc and procmailex to get more information.

If you already have mail and want to filter it through procmail for resorting, use the commands cat and formail as follows:

cat mailbox | formail -s procmail
This command is handy if you discover mail has been sorted into the wrong folder. Tighten up the ``rules'' in your ~/.procmailrc file (in the same manner as shown above) and run it through again. For instance, if I find mail in my mailbox which should have gone into the debian-list folder, I type:

mv misc temp; cat temp|formail -s procmail
I can then check the misc file to see if I tightened the rules enough to result in the proper sorting.

To do thorough filtering, I recommend that you learn to use regular expressions effectively. An excellent resource is the book from O'Reilly & Associates, Mastering Regular Expressions, Jeffrey E. F. Friedl, 1997. The man page for egrep can provide a quick reference for regular expressions.

Conclusion

You should now be able to send, receive, sort, read and reply to e-mail on your very own Linux machine. From here, I would recommend you read the EMAIL-HOWTO, the various man pages and documentation for each program.

Once your setup is working, try experimenting with different programs. There are several alternatives to procmail, e.g., deliver and mailagent. Instead of fetchpop, you could use the popular programs fetchmail or popclient. Many other MUAs, such as mail and elm, work quite well. As a substitute for sendmail or smail, install qmail, which is quite popular due to its speed, reliability and simplicity compared to sendmail.

Generic Mail Flow

Mailboxes

Credits

Jonathan Walther enjoys hanging out on MOOs and coding various little programs. Currently, he's looking for any Unix sysadmin/coding related work at an entry level, hopefully somewhere in western Canada where he lives. He just LOOOOOOVES getting e-mail at krooger@xxxxxxxxxxxx. Look for SirDibos on IRC and the various MOOs.


[Prev in Thread] Current Thread [Next in Thread]
  • [aclug-L] HEre it is, the best procmail example ... IMHO, JOHNSTONE JEREMY S <=