2

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: <subject> <recipient>" >&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: <subject> <recipient>" >&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

2 Answers 2

2

You might also want to check out this SuperUser post. The accepted answer provides a bash script that utilizes AppleScript to send an email with an attachment from a Terminal command line. This script may be more functionality than you need, but it's a great starting point. The code from the answer:

#!/bin/bash
echo "tell application \"Mail\"
    activate

    set MyEmail to make new outgoing message with properties {visible:true, subject:\"$2\", content:\"Some Message Here\"}
    tell MyEmail
        make new to recipient at end of to recipients with properties {address:\"$1\"}
        make new attachment with properties {file name:((\"$3\" as POSIX file) as alias)}
    end tell
end tell
" | osascript
1
  • 1
    Thanks. Echoing the string into osascript got rid of the error.
    – foo
    Mar 1, 2012 at 0:00
1

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 [email protected] 
 -t [email protected] 
 -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.

2
  • 1
    Thanks Ash. I have thought of other options like sendemail, but don't like the idea of storing login details in plain text. After some Googling I found out that there are some options available to use the keychain to authenticate, but there is always some chance of misconfiguration and so on. That is when I thought, hey I already have an authenticated email client: Mail.app, if I could just script that.
    – foo
    Feb 29, 2012 at 22:17
  • Good point, @foo, I should have mentioned the cleartext login details in my post. That gave me pause too, but I'm using my script to send ToDo emails to Wunderlist using a disposable Gmail account. I don't mind if that account is compromised. I'm glad you found the solution to the problem.
    – Ash
    Mar 2, 2012 at 13:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .