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.
Tip of the Trade: Uuencode is a command-line oneliner that saves you time by piping attachments directly into the mail command.
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" [email protected]
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" [email protected]
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 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" [email protected]
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).