Skip to Content

Terminal tip: easy email attachments

If you're looking to automate the sending of emails with attachments quickly and easily (and aren't too concerned with having some glamorous stationery), Terminal is once again your friend. It's possible with Mail.app and AppleScript, but there are a few pitfalls and, for most purposes, a simple shell command will do the trick:

(echo "This is the message body";uuencode Desktop/yourDoc.doc yourDoc.doc)|mail -s "Test attachment" someone@adomain.com

The magical command in this one is uuencode, which is used to encode and decode binary files and can be used on just about any file type. The two arguments in the command above define the name and location of the source file and the name the file should have when it's received. The parenthetical statement at the beginning combines the results of the echo and uuencode commands which are then piped (|) to the mail command. The mail command, having received the body text and attachment, is told to append a subject (-s "Subject") and send it to the address specified. If you wanted to send a longer text file – with line breaks, perhaps – as the body, you could save the text in an external file and replace the echo statement with cat myfile.txt.

By adding a little complexity you could make a shell script that takes arguments, making the automation a little more flexible. But TUAW reader Adam was wondering how to send a photo he'd taken automatically using AppleScript (triggered by a Mail Rule). So here's an AppleScript implementation that doesn't require opening Terminal or dealing with Mail.app scripting:

set msgBody to "This is the body of the message"
set msgSubj to "Message subject"
set mailDest to "someone@adomain.com"
do shell script "(echo '" & msgBody & "'; uuencode /Users/you/Desktop/pictosend.jpg pictosend.jpg) | mail -s '" & msgSubj & "' " & mailDest

Make sure you remove any line breaks from that last line. This obviously requires a predetermined image name, but that could be made a variable as well and used as part of a larger script. We hope this helps, Adam!



Categories

Terminal Tips

If you're looking to automate the sending of emails with attachments quickly and easily (and aren't too concerned with having some...
 

Add a Comment

*0 / 3000 Character Maximum

14 Comments

Filter by:
TranceMist

Now if I can just find my Telebit TrailBlazer and get that uucp 'g' spoofing going again...

April 22 2008 at 11:50 AM Report abuse rate up rate down Reply
brian

Um, unless I didn't read this correctly, I think there's a pretty big step missing... how does the Mac know where to send the mail? AFAIK, for this to work, the mail server/MTA has to be set to communicate to someone, somehow... at the very least, this will result in a return address like 'yourname@yourmachine.local' which will get thrown in the spam bin by almost any ISP that receives it.

I just tried it on my 10.4 machine and was immediately told
postdrop: warning: unable to look up public/pickup: No such file or directory

April 19 2008 at 8:34 PM Report abuse rate up rate down Reply
1 reply to brian's comment
Brett Terpstra

@brian and everybody:

I'll take the flak for syntax errors gladly. I'll also gladly concede that uuencode is not a perfect solution. However, in all testing on this particular use case (sending images to yourself from your iSight), it worked perfectly.

As far as the MTA goes, I made an assumption. I'm running a Leopard (not Server) install on several machines, have never done any extraordinary setting up of any mail protocols outside of Mail.app, and have never had any trouble using the mail command to shoot messages to my phone or email.

Granted, the return address is .local, but for the "mail-to-self" scenario spam-blockers are an easy fix. So, shamelessly admitting to ignorance here, how would I have unknowingly set this up to work?

April 19 2008 at 9:09 PM Report abuse rate up rate down Reply
Kent

Oooh... what a nasty hint....

As mentioned above, uuencoding is not a nice way to do things. If you are looking for a nice tool to do the job - http://caspian.dotconf.net/menu/Software/SendEmail/ - available through darwin ports and will mime things up nicely for you (as well as allow you to do a bunch of other things with varying degrees of usefulness).

April 19 2008 at 8:30 PM Report abuse rate up rate down Reply
Rafe H.

What account does this use to send the email? What is the return address of the email?

April 19 2008 at 4:23 PM Report abuse rate up rate down Reply
Murphy Mac

For anyone who wants a little extra help...

http://murphymac.com/new-and-improved-finder-emailing/

April 19 2008 at 3:55 PM Report abuse rate up rate down Reply
Juan Manuel Palacios

No need to use parenthesis in the original command, as those span a an entirely new subshell but all you need in this case is grouping of commands. The following will do:

{ command1; command2 } | command3

There's a lot more to subshells than grouping, including a lot of unnecessary overhead for this case.

-jmpp

April 19 2008 at 3:11 PM Report abuse rate up rate down Reply
1 reply to Juan Manuel Palacios's comment
Juan Manuel Palacios

Sorry, make that:

{ command1; command2; } | command3

But point remains.

-jmpp

April 19 2008 at 3:13 PM Report abuse rate up rate down Reply
Jacob Johnson

Or....
You could just install quicksilver
:-p

... but you already knew that.

April 19 2008 at 3:10 PM Report abuse rate up rate down Reply
Christopher Masto

This will of course fail if you use a single quote in your message ("How's it going?"), and uuencode is not the right way to do a file attachment; it may be picked up by a few mail applications, but to do a proper attachment you need to set the right MIME headers and encode it properly. Generally speaking, it's a bad idea to be building shell commands out of strings and executing them to do stuff like this when Mail has a perfectly serviceable AppleScript interface. I don't know how much of this will survive the comment formatting, but you could use something like the following:

tell application "Mail"
set msg to make new outgoing message with properties ¬
{subject:"hello there", content:"here's some picture I took"}
tell msg to make new to recipient with properties {address:"yourfriend@example.com"}
tell content of msg to make new attachment with properties {file name:"myimage.jpg"}
send msg
end tell

April 19 2008 at 2:43 PM Report abuse rate up rate down Reply
Len

Wow, manual uuencoding...

It feels like it's 1996 all over again. :)

April 19 2008 at 2:14 PM Report abuse rate up rate down Reply
1 reply to Len's comment
brian

Reminds me of my favorite old-timey joke:
User: How do I view this attachment?
Admin: You uudecode it.
User: I I I decode it?

April 20 2008 at 2:38 PM Report abuse rate up rate down Reply
Edward Patel

I use MSMTP to send email by the Terminal and it supports SSL and other neat features.

http://msmtp.sourceforge.net/

April 19 2008 at 1:04 PM Report abuse rate up rate down Reply
Buy an ad here

Hot Apps on TUAW

Tweets

© 2012 AOL Inc. All Rights Reserved.