Creating wrapper script for Mail.app to send mail from Terminal I'm trying to send mail from terminal with the help of bash and applescript. I use bash for taking care of arguments and the email message body. Then I have tried using Applescript to send the mail with Mail.app. I have run into some problems though, when I try the following I get: 4:4: syntax error: Expected expression but found end of script. (-2741)

#!/bin/bash

if [ $# -ne 2 ]
then
    echo "Arguments: &lt;subject&gt; &lt;recipient&gt;" &gt;&amp;2 #stderr
    exit 1
fi

read message

applescript="
tell application \"Mail\"
    set theMessage to make new outgoing message with properties {visible:true, subject:${1}, content:${message}, address:${2}}
    send theMessage
end tell
"

# send the message
osascript -e ${applescript}


Updated version:

#!/bin/bash

if [ $# -ne 2 ]
then
    echo "Arguments: &lt;subject&gt; &lt;recipient&gt;" &gt;&amp;2 #stderr
    exit 1
fi

read message

echo "tell application \"Mail\"
    set theEmail to make new outgoing message with properties {visible:true, subject:\"${1}\", content:\"${message}\"}
    tell theEmail
        make new recipient at end of to recipients with properties {address:\"${2}\"}
        send theEmail
    end tell
end tell" | osascript
Do you need to use Mail.app, or is your goal just to send an email message? If it's just to send an email message from the Terminal, you can use the sendemail command-line utility.

/usr/local/bin/sendemail 
 -f from@my.email.com 
 -t my.recipient@their.email.com 
 -s your.smtp.server:port 
 -xu smtp.username.here 
 -xp smtp.password.here 
 -m message.body.goes.here


Obviously in Terminal all of that will need to go on a single line, with appropriate escapes and string quoting. You can look at the man page for sendemail for more options.