Email PDFs From the Command Line With Open Source Software uuencode

Monday May 17th 2010 by Juliet Kemp

Tip of the Trade: Uuencode is a command-line oneliner that saves you time by piping attachments directly into the mail command.

I often find myself wanting to email someone an attachment of some sort – PDF, doc, jpg, or whatever. As a rule I'll do this by scping it to the machine I use for mail (or uploading to gmail), but I'd far rather save time with a command-line oneliner.

Enter uuencode, which allows you to pipe attachments directly into the mail command. Here's a basic example:

uuencode article.pdf article.pdf | mail -s 
"Here's that article" juliet@earth.li

The -s option to mail sets the subject line.

Note that you must give the filename twice. If you give it only once, the image will not actually be sent, and mail will wait until you hit Ctrl-D to return to the command line. It will then send the rest of the email, but without the attachment. This is because the first filename is read as the label for the attachment (i.e., it assumes -o), and the default is for uuencode to read from stdin. Without a second filename, it will try to send, as an attachment, whatever you type before you hit Ctrl-D. This means you could do this:

uuencode text.txt | mail -s "Testing text" juliet@earth.li
Then, type a bunch of text in before hitting Ctrl-D, and it would be sent as a text file.

However, a better way to do this is to save your text file, then use:

uuencode text.txt < text.txt | mail -s "Testing text" juliet@earth.li
If you want a (short) message body as well, try:
(echo 'Hope you find it interesting -- Juliet'; uuencode article.pdf 
article.pdf) | mail -s "Here's that article" juliet@earth.li

Finally, bear in mind that if the machine you're sending from isn't set up with proper reverse DNS, some mail servers will reject the incoming mail. If it's not working, this might be why.

Juliet Kemp has been messing around with Linux systems, for financial reward and otherwise, for about a decade. She is also the author of "Linux System Administration Recipes: A Problem-Solution Approach" (Apress, 2009).

Follow ServerWatch on Twitter

Mobile Site | Full Site