Filed under: Terminal Tips
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!
Get a WordPress.com Blog
![TUAW [Cafepress]](http://www.blogsmithmedia.com/www.tuaw.com/media/tuaw-cafepress-promo.png)


Reader Comments (Page 1 of 1)
Adam said 1:02PM on 4-19-2008
I used to use this frequently while I was ssh'd into remote unix boxes. For what it's worth, I've had trouble with gmail users receiving the attachments correctly.... sometimes it fails to parse the contents out and just shows them the raw encoded data.
Of course gmail is only a beta, so that's to be expected! ;)
Reply
Edward Patel said 1:04PM on 4-19-2008
I use MSMTP to send email by the Terminal and it supports SSL and other neat features.
http://msmtp.sourceforge.net/
Reply
LenPict said 2:14PM on 4-19-2008
Wow, manual uuencoding...
It feels like it's 1996 all over again. :)
Reply
brian said 2:38PM on 4-20-2008
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?
Chris said 2:45PM on 4-19-2008
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
Reply
Jacob Johnson said 3:11PM on 4-19-2008
Or....
You could just install quicksilver
:-p
... but you already knew that.
Reply
Juan Manuel Palacios said 3:12PM on 4-19-2008
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
Reply
Juan Manuel Palacios said 3:13PM on 4-19-2008
Sorry, make that:
{ command1; command2; } | command3
But point remains.
-jmpp
Murphy Mac said 4:05PM on 4-19-2008
For anyone who wants a little extra help...
http://murphymac.com/new-and-improved-finder-emailing/
Reply
Rafe H. said 4:24PM on 4-19-2008
What account does this use to send the email? What is the return address of the email?
Reply
Kent said 5:54AM on 4-20-2008
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).
Reply
brian said 8:34PM on 4-19-2008
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
Reply
Brett Terpstra said 9:09PM on 4-19-2008
@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?
TranceMist said 2:22PM on 4-22-2008
Now if I can just find my Telebit TrailBlazer and get that uucp 'g' spoofing going again...
Reply