Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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
share|improve this question

2 Answers 2

up vote 2 down vote accepted

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
share|improve this answer
1  
Thanks. Echoing the string into osascript got rid of the error. –  foo Mar 1 '12 at 0:00

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.

share|improve this answer
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 '12 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 '12 at 13:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.