<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE artikkel PUBLIC "-//infoweb.ntnu.no//DTD Infoweb 1.0//NO" "http://infoweb.ntnu.no/infoweb.dtd">

<artikkel spraak="en">
  <tittel>Basic email sorting and filtering with procmail</tittel>
  <forfatter>janpetar</forfatter>
  <sammendrag> Sorting and filtering of email is very useful when you're on email lists or want to forward email from specific senders.</sammendrag>
  <lenker>
    <ekstern tittel="procmail" uri="http://www.procmail.org/"/>
  </lenker>

  <hoveddel>
    <os2>Why sort and filter your email?</os2>
      <avsnitt>Sorting and filtering of email is useful to:</avsnitt>
      <ul>
        <li><infoweb tittel="Spam filtering with procmail" aid="250">get more control of over your spam filtering</infoweb></li>
        <li>Collect all mail from one source, like an email list in their own mailbox</li>
        <li>Forward important mail from some recipients to another email account</li>
      </ul>

    <os2>General info</os2>
      <avsnitt> On the stud servers you can filter your email with the program <kommando>procmail</kommando>. 
      This program is activated automatically every time you receive an email if you follow the instructions below.</avsnitt>
    

    <os2>Instructions</os2>
      <avsnitt> Create a file in the <uthev>root folder of your home directory</uthev> with the name <uthev>.procmailrc</uthev>
       with any editor. This will be the configuration file for procmail and needs to have the following in the start of the file.:</avsnitt>
      <kode>MAILDIR=$HOME/mail
LOGFILE=$MAILDIR/.procmail-log</kode>
      <avsnitt>Many procmail users have already defined variables like HOME, DEFAULT and SHELL. 
      You should <trykk>not</trykk> define these yourself, as procmail defines them automatically.</avsnitt>

      <os3>Example</os3>
        <avsnitt>In this example you want to filer out email that comes to the mail list maillist@list.stud.ntnu.no to its own mailbox <uthev>maillist</uthev>
         Also, you want to filter out email that comes from a friend "friend@stud.ntnu.no" to the mailbox "Friends". That means you need two filter rules:</avsnitt>
        <kode>:0 H:
* ^TO.*maillist@list.stud.ntnu.no
maillist

:0 H:
* ^From:.*friend@stud.ntnu.no
friends</kode>
        <avsnitt> All email that is marked with To:/Cc: maillist on list.stud.ntnu.no will now be put in the mailbox maillist. 
        The mailbox is created when you first receive a mail that triggers the rule. The mailbox will be in the directory mail together with the main mailbox <uthev>INBOX</uthev>.</avsnitt>
        <avsnitt>If you use ^To not ^TO it will not include carbon copy (Cc:) mail. You can also filter on e.g. ^Subject: </avsnitt>
        <avsnitt><uthev>NB!</uthev> The symbol in the first line is the number <uthev>zero</uthev>, not capital 'o'.</avsnitt>

    <os2>Filtering out spam</os2>
      <avsnitt>To do additional filtering of spam other than the already default spamassainsin based filtering 
      that is run on stud you should read the following article: "<infoweb tittel="Spam filtering with procmail" aid="250">Spam filtering with procmail</infoweb>".</avsnitt>

    <os2>Finished example file</os2>
      <avsnitt>The following file you can download and save on the <uthev>root of your home directory</uthev>.  
      Lines that begin with #, is discarded by procmail as comments(they are ignored).</avsnitt>
      <avsnitt><fil tittel="Finished example of a .procmailrc" navn="procmailrc" /> (has to be renamed as .procmailrc to work).</avsnitt>

    <os2>What can go wrong?</os2>
      <avsnitt>If you write this file in windows (e.g. with notepad) it may not work. This is because a good deal of the 
      windows based text editors add a control symbol at the end of each line to show that there shall be a line break
       here. You can edit the files with a UNIX editor and remove the symbols. They look like this: <kommando>^M</kommando>.
         If you use Textpad you can choose to save it with "File Format" set to UNIX and it will work.</avsnitt>

    <avsnitt>When you forward mail with procmail it is very important to know that you are set as sender when it forwards. 
    If the email does not reach the recipient it will be returned to you. If this is not caught by a rule in procmail is may try to resend 
    the returned mail to the already failed recipient. This will result in an <uthev>never-ending loop</uthev>.</avsnitt>

    <avsnitt>The easiest way to prevent this form happening is to include the following rule as the <uthev>first</uthev> rule in .procmailrc:
      <kode>:0:
* ^FROM_MAILER
$DEFAULT</kode></avsnitt>

    <avsnitt>This rule will catch all <uthev>error messages</uthev> (which is sent from a special user called
     <uthev>MAILER-DAEMON</uthev> or other senders as procmail recognizes as this special user) and put 
     those messages directly in the inbox. This prevents never-ending loops which may cause instability on the mail servers.</avsnitt>
    
    <os2>Learn more?</os2>
      <avsnitt>For more information about  procmail you can log in to login.stud.ntnu.no and type <kommando>man procmail</kommando>, <kommando>man procmailrc</kommando> and <kommando>man procmailex</kommando>.
      Also this <ekstern uri="http://www.zer0.org/procmail/mini-faq.html" tittel="Procmail FAQ"/> is very useful. </avsnitt>

  </hoveddel>
</artikkel>

