Send Mail.app email from the command line

May 04, '02 02:02:05AM

Contributed by: Anonymous

Here's a perl script that will create a new email in Mail.app. I wrote it so I can type "mail email@what.ever" in Terminal, and have it open up a new message in Mail.app (I've named it "mail", and put it into my own ~/bin directory). This will be of interest mainly to unix heads inured to typing "mail" into the command line.

Read the rest of the article for the script...

#! /usr/bin/perl

$address = $ARGV[0];

if (! $address) {
print "usage - mail email\@mac.com\n";
exit;
}

$SCRIPT =<<EOS;
tell application "Mail"
activate
set mailversion to version as string

set composeMessage to (a reference to (make new compose message at beginning of compose messages))
tell composeMessage
make new to recipient at beginning of to recipients with properties {address:"$address"}
end tell
set messageEditor to make new message editor at beginning of message editors
(* the following is a work around for a bug fixed in later
versions of the Mail application that was present in versions
1.0 and 1.1. *)
if mailversion is "1.0" or mailversion is "1.1" then
set compose message of last message editor to composeMessage
else
set compose message of first message editor to composeMessage
end if

end tell

EOS

open (FH, "|/usr/bin/osascript");
print FH $SCRIPT;
close FH;

Comments (5)


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