简体   繁体   中英

MacOSX: new mail with attachment

I want to create a new mail with the default mail client and attach a file automatically to it.

To create a new mail to dummy@somewhere.com with subject foo and body bar , I can do the following:

open "mailto:dummy@somewhere.com?subject=foo&body=bar"

How can I attach a file now?

If this is not possible this way (with open ), what are my alternatives?

I would prefer a solution which works in Java as well as in native languages (C++, ObjC). So if there is a way via shell to do this, this would make it easy as I can just spawn such a progress.

Otherwise I would have to fall back to some SMTP engine and just write an own small mail sender.

You can do this via AppleScript, eg

tell application "Mail"
    set msg to make new outgoing message with properties {subject:"Test", visible:true}
    tell msg to make new to recipient with properties {address:"someone@somewhere.com"}
    tell msg to make new attachment with properties {file name:"Macintosh HD:Users:me:my_file.txt" as alias}
end tell

If you don't have any way to run AppleScript directly then you can use osascript via the command line, eg

osascript <<EOF
tell application "Mail"
    set msg to make new outgoing message with properties {subject:"Test", visible:true}
    tell msg to make new to recipient with properties {address:"someone@somewhere.com"}
    tell msg to make new attachment with properties {file name:"Macintosh HD:Users:me:my_file.txt" as alias}
end tell
EOF

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM