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

Send Mail.app email from the command line UNIX
Here's a perl script that will create a new email in Mail.app. I wrote it so I can type "mail email@what.ever" in Terminal, and have it open up a new message in Mail.app (I've named it "mail", and put it into my own ~/bin directory). This will be of interest mainly to unix heads inured to typing "mail" into the command line.

Read the rest of the article for the script...

#! /usr/bin/perl

$address = $ARGV[0];

if (! $address) {
print "usage - mail email\@mac.com\n";
exit;
}

$SCRIPT =<<EOS;
tell application "Mail"
activate
set mailversion to version as string

set composeMessage to (a reference to (make new compose message at beginning of compose messages))
tell composeMessage
make new to recipient at beginning of to recipients with properties {address:"$address"}
end tell
set messageEditor to make new message editor at beginning of message editors
(* the following is a work around for a bug fixed in later
versions of the Mail application that was present in versions
1.0 and 1.1. *)
if mailversion is "1.0" or mailversion is "1.1" then
set compose message of last message editor to composeMessage
else
set compose message of first message editor to composeMessage
end if

end tell

EOS

open (FH, "|/usr/bin/osascript");
print FH $SCRIPT;
close FH;
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[8,435 views]  

Send Mail.app email from the command line | 5 comments | Create New Account
Click here to return to the 'Send Mail.app email from the command line' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Name it Mail instead
Authored by: osxpez on May 04, '02 08:18:47AM

Great idea! I might try to adapt it so I can use it with Entourage. But I suggest you name your script Mail instead of mail. This will bring less surprise to *nix heads and you don't risk breaking any scripts relying on the mail command (/usr/bin/mail).



[ Reply to This | # ]
oops!
Authored by: osxpez on May 04, '02 08:42:35AM

I just learned something important about OS X!! It seems it's case insensitive also when it comes to command names. This is truly strange. But it leads to that even if you name the script Mail it risks be confused with /usr/bin/mail. Try "which Mail" without having a script of your own called Mail or mail before /usr/bin/mail in the path...

Anyhow, this means you should name the script something else. Try myMail or some such.



[ Reply to This | # ]
Entourage
Authored by: osxpez on May 04, '02 06:55:19PM

Here's a script that works with Entourage:

#!/bin/sh
osascript << EOF > /dev/null
tell application "Microsoft Entourage"
activate
make new draft window with properties {to recipients:"$1"}
end tell
EOF



[ Reply to This | # ]
create a .url document of 2 lines
Authored by: spaceMan on May 06, '02 09:32:15PM

alternate method:
create a text file(BBEdit Lite works nicely!) of naming style:
yourfile.url
use the sample following text <enter> indicates end of line
starts on next line - total 2 lines
[Internet Shortcut]<enter>
mailto:someone@mac.com?subject=Site%20feedback&body=Dear%20Webmaster:<enter>

use the Finder Show Info(or BBedit) to set open with application to your preferred Internet Browser. Now when you double click (or use the open path to file in terminal) you will see your browser interpret and then launch your preferred email-filling in the recipient, subject and start of text body.
try this out, it is a handy time saver for frequent mail templates.



[ Reply to This | # ]
Even easier in Jaguar
Authored by: Donatello on Feb 01, '03 10:56:07PM

With 10.2, you can just do this:

alias mail 'open mailto:!*'

This starts a new message in your default mail program, just like clicking an email link in your browser.

Pre-Jaguar, you have to stick with the other solutions posted here.



[ Reply to This | # ]