Friday, September 8, 2017

On Quick & Dirty Email Attachments in Unix

Need to export a file from Unix/Linux via email? Got uuencode? Do you even remember uuencode? It's how we used to encode files for file sharing, back before you were born.

Here's a handy one-liner that will wrap your file up as a UUEncoded attachment and mail it to the address you specify. The email should come through with a properly formatted attachment that you can then download.

Just do this:
% cat file.zip | uuencode file.zip | mail recipient@example.com

Or you can get a bit fancier and add a subject line and a proper from address (if your system doesn't add one already):
% cat file.zip | uuencode file.zip | mail -s "Export of file.zip" -a "From: Me <sender@example.com>" recipient@example.com

Or you can do it as part of a shell script, with bits that look something like this:

FROM="Just Me <sender@example.com>"
TO="recipient@example.com"
FILE="file.zip"
cat $FILE | \
uuencode $FILE \
mail -s "Export of $FILE attached" \
-a "From: $FROM" \
$TO

I know there's probably some better way to do this, but this simple example has saved me endless amounts of time lately. Hope you find it useful, too.

No comments: