#! /usr/bin/perl

$address = $ARGV[0];
$subject = $ARGV[1];
$body = $ARGV[2]. "\n\n";
$attachment = $ARGV[3];

if (!$address) {
    print "usage - sendmail.pl email\@mac.com subject body attachment\n";
    exit;
}

$SCRIPT =<<EOS;
tell application "Mail"
   activate
   set theMessage to make new outgoing message with properties {visible:true, subject:"$subject"}
   make new paragraph at beginning of content of theMessage with data "$body"
   tell content of theMessage
      make new attachment with properties {file name:"$attachment"} at after last paragraph
   end tell
   tell theMessage
      make new to recipient at end of to recipients with properties {address:"$address"}
   end tell
end tell

EOS

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

