Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Automatic client or server mail filtering UNIX
If you have an account on a Unix system, you may be familiar with Procmail. This is a very advanced piece of mail processing software. One of its main uses is to sort incoming mail into different folders. The same can be done with Apple Mail's rules. However, there is a big difference (and I'm not talking about user friendliness): procmail generally runs on the server, while Mail runs on the computer you read your mail on (the client).

I'm sure this need isn't universal, but for me it's very convenient to be able to have mail put into the right folders on the server, both because it saves a lot of time downloading mail over slow connections, and because I sometimes actually read the mail on the server (that's the beauty if IMAP: you can read your mail from different computers without them getting in each other's way). So that's why I use Procmail to filter my mail.

However, there is a disadvantage to having the server filter mail: Apple Mail only checks the In folder for new mail. So when Procmail puts new messages into different folders, this isn't visible in Mail. The solution is filtering incoming mail in Mail rather than Procmail. But why not do both? In an ideal world, Mail would filter my mail when I'm online, and Procmail does it when I'm not. This was of course a challenge I couldn't resist, with the following result...

Whenever I receive mail, Procmail runs a shell script to determine whether I'm online and if I have Mail running. This line in my .procmail file runs the shell script:

FILTER=`/usr/home/iljitsch/mailfilter.sh`
The script itself looks like this:
#!/bin/sh
case `netstat -Wn | egrep -c "\.143 +10.0.0.20.*EST"` in
0)
  echo "server" ;;
*)
  echo "client" ;;
esac
The script executes the Unix netstat command that (among other things) lists all active TCP sessions. The -Wn option makes sure address are listed rather than host names, and the addresses aren't truncated. The egrep command matches anything with .143 (the TCP port for UDP) in it, followed by one or more spaces and my IP address (listed as 10.0.0.20 in the above script -- replace it with your real IP address!), and then EST (the session must be ESTABLISHED). Note that you could just as easily check the process table for instances of the imap daemon run under your name, so a static IP address isn't necessary. You don't want to check for your domain name, though, as the domain name lookups can get very slow.

If the result of the check is zero, the script replies server; in all other cases it says client. This result is put in the FILTER variable by Procmail. Then later in the .procmailrc, I can simply check the contents of this variable and only run the required filters when FILTER contains server:

:0
* FILTER ?? ^server$
{
  :0
  * ^from: .*webmaster@bgpexpert.com
  bgpexpert
}
For instance, this filter will put all messages from webmaster@bgpexpert.com into the folder 'bgpexpert' -- but only when I'm not online. When I am, Mail does this and I get a nice little beep to indicate that I have new mail.
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[5,398 views]  

Automatic client or server mail filtering | 6 comments | Create New Account
Click here to return to the 'Automatic client or server mail filtering' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Automatic client or server mail filtering
Authored by: Radha Venkat on Aug 20, '04 11:09:36AM
I went through some odd convolutions to address the same issue. I then found a simpler solution: MacBiff. It polls the imap server and alerts you to new mail. It is very customizable; it can be set to monitor multiple accounts, mbox/maildir formats, ignore certail folders (eg. spam), and show you the subject line. It can also use any alert sound you want... It has a very small footprint on the menu bar. It can also execute a shell command whenever new mail is received. And it's free (as in GPL).

http://www.forkit.org/macbiff/

[ Reply to This | # ]
Automatic client or server mail filtering
Authored by: MattHaffner on Aug 20, '04 11:52:42AM
So when Procmail puts new messages into different folders, this isn't visible in Mail.

I don't remember for sure if Mail actually beeps, but if you use IMAP, go to the Advanced pane of your account in Mail's preferences. There you'll see a option to "Automatically synchronize changed mailboxes". Check it.

Now, every time your mail is checked, all your folders will be checked and ones with new messages in them will be highlighted in bold with the number of new messages--just like your In box. If you mail folders organized in subfolders and the subfolder is closed, it will be bold to let you know that a folder below it has new mail.

This can be a little network intensive if you check mail frequently or have a lot of folders (like me). So, if you want, you can just do this on occasion manually with Mailbox->Syncronize "account". This is nice if you want to suck up all your filtered mail before you go get on the bus so you have plenty to read on your way home :) Normally, folders aren't synchronized until you open them.

There is also an Applescript synchronize command in Mail, so you could write a little script to synchronize less frequently than you check your In box, triggered by something (e.g. wake), etc.



[ Reply to This | # ]

Automatic client or server mail filtering
Authored by: jaskerr on Aug 20, '04 01:43:15PM

I believe that Apple Mail will update the folder (and containing group folder) without requiring the synchronization as described.

I don't keep offline-copies, which means that 'Automatically synchronize changed mailboxes' is not available (greyed out). But, I have 'Include when automatically checking for new mail' checked, and this seems sufficient.

When Mail scans my two IMAP accounts, the mail files contained in various subfolders are also scanned for new mail and the folder names are updated accordingly.



[ Reply to This | # ]
Automatic client or server mail filtering
Authored by: MattHaffner on Aug 20, '04 10:40:39PM

Yes, you're right. The counts and bolded (sub-)folders are updated without the synchronize. Synchronize actually retrieves all the messages so they exist locally.



[ Reply to This | # ]
Automatic client or server mail filtering
Authored by: iljitsch on Aug 21, '04 06:47:21AM

When exactly does Mail update the folder counts?

For me this only happens when I take my mail account online, when I open a folder, or when I reply to a message (but only for that one folder).

Or when Mail moves the messages to the folders itself, of course.

But maybe I'm in some way limited by my IMAP server. The one I have isn't exactly the most advanced in the world.



[ Reply to This | # ]
Automatic client or server mail filtering
Authored by: William McCallum on Aug 21, '04 05:22:04PM

I have the same problem, and I'm using the mac.com IMAP server. I use popfile to sort mail on the server. I use cron and applescript to get around the problem.



[ Reply to This | # ]