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

Create a simple command-line mailchecker UNIX
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:
  • [optional] change the ip-adress (17.254.0.91 is apple.com's) to an adress of a 'close' server.
  • [if you're not using Mail.app] change every occurence of "Mail.app" to the mail-client you're using.
  • [if your mail-client doesn't reside in /Applications/] adapt the path of the mail-client
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.
    •    
  • Currently 4.00 / 5
  You rated: 4 / 5 (3 votes cast)
 
[4,993 views]  

Create a simple command-line mailchecker | 5 comments | Create New Account
Click here to return to the 'Create a simple command-line mailchecker' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
The problem with Mail.app
Authored by: el bid on Apr 22, '02 05:00:00PM

I'm not entirely sure why this fix in necessary -- why not just leave Mail.app running with the timed fetch option switched on?

The problem I have, which touches on all this, is that the collection of email from my three or four ISPs will fairly often throw up an error -- usually just a fetch that has timed out. This in itself isn't a problem -- a UNIX-type system should normally just log the error and do a retry next time around. But Mail.app, alas, insists on turning it (almost literally) into a song and dance, with the Dock Icon doing a jitterbug until I bring Mail.app into focus and dismiss the error dialogue.

So I don't set up Mail.app to collect the mail automatically, and this neat scripted fetcher isn't going to help either.

A better solution (which I've implemented but not yet polished) would be to use fetchmail not just to check the existence of unread mail, but to download it too, to a UNIX mail directory. Then Mail.app can be used to collect automatically from that directory. As this should never throw up any errors, those irritating "Uncontrollable Fits of Interactivity" will be circumvented.

--
el bid




[ Reply to This | # ]
The problem with Mail.app
Authored by: adriaant on Apr 22, '02 05:58:07PM
Of course you can use fetchmail to retrieve mail and drop it in a Unix box! You don't even need to run a cron job using a script, because fetchmail can retrieve mail periodically already. For example, the following .fetchmailrc will retrieve POP mail every 300 secs:
set postmaster "user"
set daemon 300 #poll every 300 secs
poll your.mail.server with proto POP3
        user "user" there with password "passw" is "login" here options stripcr
This will drop mail in your "user" UNIX mailbox, which you can then read in with Mail.app.

[ Reply to This | # ]
With Entourage
Authored by: xchanyazy on Apr 22, '02 10:36:23PM
Instead of
ok=`/bin/ps -aux | /usr/bin/grep Mail.app | /usr/bin/grep -vc grep` ; ko=0
write
ok=`/bin/ps -auxww | /usr/bin/grep Entourage | /usr/bin/grep -vc grep` ; ko=0
also, instead of
/Applications/Mail.app/Contents/MacOS/Mail &
write
open -a "Microsoft Entourage"

Now, if only I could figure out a way to get to only have entourage check when there's new mail, instead of having a set schedule, and without quitting it every time...

[ Reply to This | # ]
Plain-text password? Hmm.
Authored by: Phoenix1701 on Apr 24, '02 08:53:04PM

Is there any way around having the mail password stored in plain text? I realize the file's owned by root and not readable by anyone else, but that's only good as long as no one's looking over my shoulder. I find it hard to believe that fetchmail would have users just dump their passwords into a text file...



[ Reply to This | # ]
Plain-text password? Hmm.
Authored by: Gutta on Jun 21, '02 11:52:16AM

I think there's a reference to this in the man pages for fetchmail.



[ Reply to This | # ]