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

Unix mail notifications via Growl UNIX
This tip lets you get a notification through Growl whenever new Unix mail arrives, with the subject of the mail. I get Unix mail as a result of run cron-jobs. I use cron because whether you have lingon or not; it is far easier to schedule a job with cron.

When there is something that I really want to watch from a process run by cron I do:

echo "the very interesting stuff" |mail -s "IMPORTANT" myusername

or some other Unix command to send the same mail message.

The problem is that I have to manually go into Unix mail and manually check if there is any new mail or not, and sometimes I just forget to do that.

Therefor I have made a notification in Growl via a launchd job and an AppleScript.

Prerequisites: You need to have installed Growl and Growl helper.

The lanchd process checks if your /var/mail/You mail file has changed, and if it has, the launchd process will run the script below and die until next time a new mail arrives and changes the mailfile. The AppleScript makes Growl show that there is a new mail with subject line of the last arrived mail in the bezel window.

Installation:

This script creates a Growl notification and must be placed somewhere on your disk. I have folder named .UserAgents for that purpose; to keep them collected. And a folder named .Processess for the jobs I run through cron via Cronnix.

The script must be installed via a property list file looking like the one given after the shell script below, except that you have to adjust it to insert the correct path for both the shell script and your mailfile.

You must also set its executable bit with the command chmod u+x from a Terminal window.
--------UnixMailNotifier.sh
#!/bin/bash
export mvar=`mail -H |tail -1 |sed -n 's/(..*")([^"][^"].*)(["].*)/2/p'`
/usr/bin/osascript <<-EOF 2>/dev/null 1>&2
  tell application "GrowlHelperApp"
    try
      set the allNotificationsList to ¬
        {"Unix Mail Notification"}
      set the enabledNotificationsList to ¬
        {"Unix Mail Notification"}
      register as application ¬
        "Growls Unix Mail Notification" all notifications allNotificationsList ¬
        default notifications enabledNotificationsList ¬
        icon of application "Terminal.app"
      
      --  Send a Notification...
      notify with name ¬
        "Unix Mail Notification" title ¬
        "Unix Mail Notification" description ¬
        "Subject: $mvar" application name "Growls Unix Mail Notification" icon of application "Terminal.app"
      
    on error the error_message number the error_number
      display dialog "Error: " & the error_number & ". " & the error_message buttons {"Cancel"} default button 1
    end try
    
  end tell
EOF
The property list file which must be installed in ~/Library/LaunchAgents looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>com.You.UnixMailNotifier</string>
  <key>Program</key>
  <string>/Users/You/.UserAgents/UnixMailNotifier.sh</string>
  <key>ProgramArguments</key>
  <array>
    <string>/Users/You/.UserAgents/UnixMailNotifier.sh</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>KeepAlive</key>
  <dict>
    <key>SuccessfulExit</key>
    <false/>
  </dict>
  <key>WatchPaths</key>
  <array>
    <string>/var/mail/You</string>
  </array>
</dict>
</plist>
After you have saved the property list file as (for instance) com.You.UnixMailNotification.plist you must start it with:

launchctl load com.you.UnixMailNotification.plist.

From there on you should get a notification whenever you get a Unix mail.

[crarko adds: I haven't tested this one. If there are any errors in either the script or the plist please let me know via the comments and I will correct them.]
    •    
  • Currently 3.50 / 5
  You rated: 4 / 5 (4 votes cast)
 
[4,784 views]  

Unix mail notifications via Growl | 7 comments | Create New Account
Click here to return to the 'Unix mail notifications via Growl ' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
“Growl helper”
Authored by: boredzo on Jun 14, '11 09:29:25AM

“Growl and Growl helper” is redundant. GrowlHelperApp is part of Growl; if you have Growl installed, then you have GrowlHelperApp, and if you don't have GrowlHelperApp, then Growl is not installed.

I mention this in case anyone is wondering “well, where do I get ‘Growl helper’ from?”. It's part of Growl. If you have Growl, then you have GrowlHelperApp.



[ Reply to This | # ]
Unix mail notifications via Growl
Authored by: Michael_S on Jun 14, '11 11:18:06AM
Hmm, if you're sufficiently geeky (technically inclined) to run cron and mail, wouldn't you normally work with a shell in a terminal? The shell will happily tell you:
You have mail in /var/mail/username
$ 
Then read it with mailx like on any other BSD Unix. It appears that previous versions of Mail.app could read the local mail spool (presumably showing badges), but one now has to set up kluges.

[ Reply to This | # ]
Unix mail notifications via Growl
Authored by: kiltbear on Jun 14, '11 12:35:09PM

Cool, but why not use a .forward file or aliases on the unix systems to forward your email to an account you do in fact watch?



[ Reply to This | # ]
Unix mail notifications via Growl
Authored by: Akel on Jun 15, '11 01:11:10AM

Simply define MAILTO='yourrealmailaddress' at the top of crontab and all output will be sent to that address.



[ Reply to This | # ]
Unix mail notifications via Growl
Authored by: stottm on Jun 15, '11 02:05:20AM

It's been a really long time since i used Unix email on the console. However, I seem to remember you could create a hidden file named ".forward" and inside the file place multiple email addresses to be used as forwarding addresses. So doing that and forwarding to your primary Internet email will deliver your alerts to you that way.

I believe you have to configure sendmail on OS X to use your ISP's SMTP server for it to work...



[ Reply to This | # ]
Unix mail notifications via Growl
Authored by: stottm on Jun 15, '11 02:09:47AM

Bingo, here's how to configure sendmail's relay host to send outgoing mail to your ISP's SMTP server.

http://hints.macworld.com/article.php?story=20081217161612647



[ Reply to This | # ]
Unix mail notifications via Growl
Authored by: PhilHudson on Apr 13, '12 02:37:47PM
I think there's a backslash missing before the 2 in the sed command; in other words I think it should read \2. It's supposed to be a back-capture reference, right?

[ Reply to This | # ]