Create a simple command-line mailchecker

Apr 22, '02 08:26:04AM

Contributed by: Anonymous

What I really was missing on OS X so far was a small mailchecker. There are several programs for mail checking, but each one I tried takes almost as much RAM as Mail.app, which I use. So I wrote a little script, run by cron, which checks whether there are new mails (using fetchmail) and fires up Mail.app if there are.

Read the rest of the article for the script and installation instructions.

I'm really not a scripting king, but here it is. For installation you don't need to know much about the command-shell and I'll describe all editing-tasks.

First, create the following script in your preferred UNIX text editor:

#!/bin/sh
# check if there's a network connection; mailchecking doesn't make
# much sense without network connection
ok=`/sbin/ping -c 1 -q 17.254.0.91 | /usr/bin/grep -c 100%` ; ko=0
if test $ok = $ko; then
# check if Mail.app is already running; we needn't check mail
# nor do we need to start Mail.app in that case
ok=`/bin/ps -aux | /usr/bin/grep Mail.app | /usr/bin/grep -vc grep` ; ko=0
if test $ok = $ko; then
# check if there's new mail; if there is: start Mail.app
ok=`/usr/bin/fetchmail -c | /usr/bin/grep -c message` ; ko=0
if test $ok = $ko; then
exit
else # !!adapt the path and the mail-client in the following line!!
/Applications/Mail.app/Contents/MacOS/Mail &
fi
fi
fi
Edit the file with the following steps:Save the text to a file named "MailCheck.sh" somewhere in your directory structure. In a Terminal, change rights to the script to make it executable: "chmod 700 MailChecker"

Next, configure the fetchmail program so it knows about your mail servers. Save the following text to a file named ".fetchmailrc" in your home directory:
server  your.popserver.name           # Change to your mail servername
protocol POP3 # Change to your mail protocol
username MAILUSER # Change MAILUSER to your user's mailserver name
password MAILPWORD # Set MAILPWORD to the corresponding password
fetchall
keep
noflush
mda "/usr/libexec/mail.local USER" # Change USER to your OS X username
NOTE: The space at the beginning of all lines after the first is important! Change the file permissions on .fetchmailrc in the Terminal with chmod 0710 .fetchmailrc, and set the owner of the script to root with sudo chown root .fetchmailrc.

[Editor's insert: I couldn't get the script to work when it was owned by root, but I was running it in my user shell, not as a cron job. I believe root ownership is required if you take the next step and add the script to cron.]

Add the following crontab-entry:
*/1     *       *       *       *       USER    sh /Users/USER/bin/MailCheck.sh
Here you have to change "USER" to your (tada!) OS X user name, and adapt the path to reflect the location where you saved MailCheck.sh. If want to have mails checked just every five minutes, change "*/1" to "*/5". If you're not familiar with cron, try CronniX, an OS X gui for cron.

That's it. After restarting cron or rebooting, the mailchecker should work (at leat it works for me). The script takes almost no system-load or memory. I guess that the script can be improved a lot. Hints are welcome.

Comments (5)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20020422082604587