Are you prepared for Wrath of the Lich King? WoW Insider has you covered!

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!

Related Headlines

Reader Comments (Page 1 of 1)

Add your comments

Please keep your comments relevant to this blog entry. Email addresses are never displayed, but they are required to confirm your comments.

When you enter your name and email address, you'll be sent a link to confirm your comment, and a password. To leave another comment, just use that password.

To create a live link, simply type the URL (including http://) or email address and we will make it a live link for you. You can put up to 3 URLs in your comments. Line breaks and paragraphs are automatically converted — no need to use <p> or <br> tags.

New Users

Current Users

TUAW Features


Mac 101 ask-tuaw
Mac News
WWDC (250)
.Mac (57)
Accessories (635)
Airport (74)
Analysis / Opinion (1329)
Apple (1637)
Apple Corporate (555)
Apple Financial (187)
Apple History (45)
Apple Professional (54)
Apple TV (160)
Audio (446)
Bad Apple (118)
Beta Beat (149)
Blogging (84)
Bluetooth (16)
Bugs/Recalls (56)
Cult of Mac (869)
Deals (214)
Desktops (115)
Developer (252)
Education (98)
eMac (10)
Enterprise (138)
Features (396)
Freeware (381)
Gaming (361)
Graphic Design (31)
Hardware (1281)
Holidays (37)
Humor (576)
iBook (65)
iLife (234)
iMac (184)
Internet (325)
Internet Tools (1309)
iTS (967)
iTunes (796)
iWork (21)
Leopard (367)
Mac mini (112)
Mac Pro (53)
MacBook (202)
MacBook Air (79)
Macbook Pro (220)
MobileMe (16)
Multimedia (439)
Odds and ends (1437)
Open Source (277)
OS (910)
Peripherals (208)
Podcasting (182)
Podcasts (89)
Portables (197)
PowerBook (135)
PowerMac G5 (50)
Retail (577)
Retro Mac (48)
Rig of the Week (42)
Rumors (629)
Software (4304)
Software Update (406)
Steve Jobs (252)
Stocking Stuffers (50)
Surveys and Polls (97)
Switchers (112)
The Woz (34)
TUAW Business (235)
Universal Binary (281)
UNIX / BSD (61)
Video (902)
Weekend Review (81)
WIN Business (47)
Wireless (83)
Xserve (39)
iPhone/iPod News
iPhone (1487)
iPod Family (2024)
App Store (17)
SDK (14)
Mac Events
One More Thing (27)
Liveblog (1)
Other Events (226)
Macworld (489)
Mac Learning
AppleScript (2)
Ask TUAW (101)
Blogs (85)
Books (26)
Books and Blogs (62)
Cool tools (443)
Hacks (459)
How-tos (482)
Interviews (43)
Mods (185)
Productivity (586)
Reviews (107)
Security (153)
Terminal Tips (58)
Tips and tricks (563)
Troubleshooting (166)
TUAW Features
iPhone 101 (26)
TUAW Labs (3)
Blast From the Past (17)
TUAW Tips (141)
Flickr Find (34)
Found Footage (80)
Mac 101 (88)
TUAW Interview (31)
Widget Watch (198)
The Daily Best (1)
TUAW Faceoff (4)

RESOURCES

RSS NEWSFEEDS

Powered by Blogsmith

Sponsored Links

The Unofficial Apple Weblog (TUAW) bloggers (30 days)

#BloggerPostsCmts
1Cory Bohon862
2Robert Palmer6046
3Steven Sande5723
4Dave Caolo370
5Scott McNulty370
6Mat Lu358
7Erica Sadun282
8Mike Schramm222
9Brett Terpstra210
10Michael Rose1334
11Christina Warren1245
12Joshua Ellis54
13Lisa Hoover26
14Chris Ullrich12
15Jason Clarke11

Featured Galleries

Macworld 2008 Keynote
Macworld 2008 Build-up
Apple Vanity Plates
DiscPainter
Macworld Expo 2007 show floor
The Macworld Faithful in Line
iPhone First Look
AT&T 3G Coverage Map
MobileMe Guided Tour UI Changes

 

    Most Commented On (7 days)

    Recent Comments

    More Apple Analysis

    More from AOL Money and Finance

    Weblogs, Inc. Network

    Other Weblogs Inc. Network blogs you might be interested in: