Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Attach a file to a new Mail message from the Terminal UNIX
I found the following command useful to attach a file to an outgoing mail message:
 % open -a Mail 
This allows you to quickly send a file via email from your command line.
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[8,896 views]  

Attach a file to a new Mail message from the Terminal | 11 comments | Create New Account
Click here to return to the 'Attach a file to a new Mail message from the Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
open -a Mail
Authored by: Enkerli on Nov 21, '03 12:00:46PM
I guess you meant to write:
open -a mail [filename]
Come to think of it, it's rather obvious, but I never thought about this.
Unfortunately, it seems to attach multiple files to multiple messages, as in GUI:
open -a mail [filename1] [filename2]
(Creates two new messages)

[ Reply to This | # ]
open -a Mail
Authored by: bfr00 on Nov 23, '03 05:02:06PM

Any way to pass other parameters such as to, subject, etc.



[ Reply to This | # ]
open -a Mail
Authored by: vectors_to_final on Nov 23, '03 06:17:57PM
Not with "open". You're better off using AppleScript for this sort of thing. You can use the "osascript" command to run arbitrary scripts from the command line. Apple has a good script for this in a Q&A document, but if you want something quick, you might try something like this:

tell application "Mail"
	set newMessage to (a reference to (make new outgoing message))
	
	tell newMessage
		make new recipient at beginning of to recipients 
			with properties {address:"someone@somewhere.com"}
		
		set the subject to "Testing"
		set the content to "Hello World!"
		
		tell content
			make new attachment 
				with properties {file name:"/path/to/file"} 
				at after the last word of the last paragraph
		end tell
		
		set visible to true
	end tell
end tell

This will create a new message, but it won't send it. If you want to send the message automatically, change "set visible to true" to "send".

[ Reply to This | # ]

mailto:
Authored by: Enkerli on Nov 24, '03 10:26:26AM
Well, if Mail.app's your default mailreader, it accepts mailto: links so maybe there's a way to do this using a browser...
Yup, it works:
open -a Safari mailto:[address]

[ Reply to This | # ]
mailto:
Authored by: Enkerli on Nov 24, '03 10:26:34AM
Well, if Mail.app's your default mailreader, it accepts mailto: links so maybe there's a way to do this using a browser...
Yup, it works:
open -a Safari mailto:[address]

[ Reply to This | # ]
Attach a file to a new Mail message from the Terminal
Authored by: benlotto on Nov 21, '03 02:37:17PM

This inspired me to try

open -a Safari <URL>

which works perfectly!



[ Reply to This | # ]
Attach a file to a new Mail message from the Terminal
Authored by: merlyn on Nov 21, '03 02:55:38PM
You don't need to "tell safari" to do it.
open http://some.url.here/
works just fine as well.

[ Reply to This | # ]
Attach a file to a new Mail message from the Terminal
Authored by: bcs on Nov 21, '03 03:27:11PM
Do a man open to get a description for open. The description is short and helpful.

[ Reply to This | # ]
Attach a file to a new Mail message from the Terminal
Authored by: mat1 on Nov 22, '03 04:58:58AM

it doesn't open necessarily safari, it takes your default browser, for me these commands open OmniWeb.
even with -a Safari

to be sure it opens with safari :
osascript -e 'tell application "Safari" to activate' -e 'tell application "Safari" to make new document at beginning of documents' -e 'tell application "Safari" to set URL of document 1 to "http://...."'



[ Reply to This | # ]
Attach a file to a new Mail message from the Terminal
Authored by: Graff on Nov 24, '03 02:50:24PM

This is a little quicker: (all on one line)

osascript -e 'tell application "Safari" to make new document with properties {URL:"http://www.apple.com/"}'


[ Reply to This | # ]
Attach a file to a new Mail message from the Terminal
Authored by: Graff on Nov 24, '03 02:52:22PM

Hmm. I meant that to be in reply to mat1's osascript example. Dunno how it got tacked to the wrong thread, oh well...



[ Reply to This | # ]