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

Create Mail messages with attachments from Terminal UNIX
Here is a Unix script that I use to create an email in Mail containing a number of attachments that I specify via the command line. If you save the script as, say, attachments, then you create the email by entering something like attachments foo.jpg bar.txt. Remember to chmod +x the script and move it into someplace in your path, like /usr/local/bin.
#!/bin/sh

FILE_LIST=""
while [ "$*" != "" ]; do
  FILE_LIST="$FILE_LIST , POSIX file \"$1\""
  shift
done

osascript \
-e "      set file_list to { $FILE_LIST }        " \
-e "      tell application \"Mail\"              " \
-e "        open file_list                       " \
-e "        activate                             " \
-e "      end tell                               "
Note that the obvious way to do this with a droplet and on open fails to work because using the open(1) command causes the on open to receive the first file passed in, rather than the list of files. I imagine this is a bug.
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[5,627 views]  

Create Mail messages with attachments from Terminal | 8 comments | Create New Account
Click here to return to the 'Create Mail messages with attachments from Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Just Applescript should work too
Authored by: brandonium on Oct 12, '04 12:11:09PM
I'm not sure why the 'on open' droplet doesn't work for the poster. This Applescript seems to work to do the same thing:

on open FILE_LIST
	tell application "Mail"
		open every item in FILE_LIST
		activate
	end tell
end open


[ Reply to This | # ]
Create Mail messages with attachments from Terminal
Authored by: hypert on Oct 12, '04 01:20:16PM

This is a little off-topic from this script example, but doesn't anybody know an EASY way to enable command-line (terminal) mail? I'd love to be enable to compose a short email (not necessarily with attachment) and fire it off right from the terminal. I do this all the time in UNIX (Solaris), even though I don't receive mail there.

I'm afraid that I might need to install sendmail or something equally complex (and prone to attacks). Any other suggestions? I might take this combination shell-script/AppleScript, and use that as a base...

Hmmm.....



[ Reply to This | # ]
Create Mail messages with attachments from Terminal
Authored by: keith_veleba on Oct 12, '04 05:01:12PM

If you're running Panther, get Postfix Enabler http://www.roadstead.com/weblog/Tutorials/PostfixEnabler.html and start Postfix locally(it comes with Panther!)

Then, use the sendmail wrapper that comes with postfix:

Powerbook:~ velebak$ sendmail
From: <who@ami.com>
To: <who@areyou.com>
Subject: A quick message
A really quick message
.

Entering a '.' alone on a line tells sendmail/postfix to do it's thing.

Works for me all the time.

Enjoy!
Keith

keith dot veleba at nospam dot gmail dot com


---
-- If you hadn't stayed to read this sig, you'd be home by now.



[ Reply to This | # ]
Create Mail messages with attachments from Terminal
Authored by: wgscott on Oct 12, '04 01:55:38PM
open -a Mail foo.bar opens a mail send to window with the file foo.bar embedded. Also, here is a
    drop-in replacement
for /usr/bin/mail that I have used.

[ Reply to This | # ]
Create Mail messages with attachments from Terminal
Authored by: hypert on Oct 13, '04 09:17:53PM
This is a great tip! Thanks, "WG" (and thanks for emailing me directly, too). At first, I had problems with the AppleScript created out of that Perl script. Essentially:

tell application "Mail"
	set newMessage to make new outgoing message
	tell newMessage
		set subject to "invisible"
		set content to "hello"
		make new to recipient at end of to recipients with properties {address:"foo@bar.com"}
		set visible to false
	end tell
	send newMessage
end tell
Turns out that I had an old "Classic" app laying around in a half dozen places called "Mail", and AppleScript insisted on trying to send these commands to that app (needless to say, that failed miserably).

So, if anyone else is having problems with WG's Perl script, make sure "Mail" is really Apple's Mail.app.

Now, I'm going to work on getting this talk to Entourage! If it works, I'll post it....

[ Reply to This | # ]
Create Mail messages with attachments from Terminal
Authored by: hypert on Oct 13, '04 11:21:27PM
I figured out (i.e., Googled and hacked my way toward) a simple Applescript for sending mail through Entourage.

tell application "Microsoft Entourage"
	--set newMsg to make new outgoing message with properties {recipient:{recipient type:to recipient, address:"foo@bar.com"}}
	set newMsg to make new outgoing message with properties {to recipients:"foo1@bar.com, foo2@bar.com"}
	set the subject of newMsg to "send mail through Entourage using AppleScript"
	set the content of newMsg to "line 1\nline 2"
	send newMsg
end tell

This could easily be inserted into the original Perl script (I'll hack it in later), but doesn't have all the options of Apple's Mail. I can't find a way to specify CC addresses. In fact, it's kind of complex to specify the TO address (there are two examples above) and apparently has to be done while creating the newMsg object. I've seen online example of being able to set the recipients with a "set" command (like the subject and content), but it simply wouldn't work for me.

I'd welcome any comments to this Entourage AppleScript.

[ Reply to This | # ]

Check out this hint
Authored by: googoo on Oct 13, '04 10:22:12AM
This hint shows how to do something similar with a shell script. (Read the comments to see how to modify the script so you do not have to install a Perl package on Mac OS X 10.2.)

-Mark

[ Reply to This | # ]

Check out this hint
Authored by: gshenaut on Oct 13, '04 11:42:04AM

If Mail.app is running, it is possible to send mail from the command-line simply by adding a correctly-formatted message to the outgoing queue in ~/Library/Mail/Mailboxes/Outbox.mbox/mbox. It appears that if you have more than one smtp server, Mail.app will use the one in the From: header field.

The biggest issue I can see would be how to lock .../mbox while you are writing to it.

Greg Shenaut



[ Reply to This | # ]