I would like to start writing an email with Mail.app from Terminal, and add an attachment. Something like this:

macbook:~ me$ /Applications/Mail.app/Contents/MacOS/Mail -s the_subject -to email@domain.com < ~/Downloads/file.zip

You can do this using AppleScript. Here's a quick (quickly tested) bash script which is close to what you want.

echo "tell application \"Mail\"

    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

Usage: compose_email 'email@domain.com' 'Some Subject' /path/to/attachment.zip

  • Script needs a few tweaks... stay tuned! – Josh Mar 31 '10 at 2:34
  • Script tested and working – Josh Mar 31 '10 at 2:49

I know this is an old thread but I found it extremely useful. The code Josh wrote was exactly what I was looking to do. The only problem is that it doesn't work on my Mac until I add a delay. Without this it sends without an attachment. Changed:

    delay 1
    send MyEmail
    end tell
end tell
" | osascript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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