Yahoo Mail/Gmail 2024 Easy Sender Compliance Guide: Click here

Quick and Easy Email Attachments in Linux


Need to export a file from 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. Because I'm old.

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 (all on one line):
% 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. I hope you find it useful, too.

1 Comments

Comments policy: Al is always right. Kidding, mostly. Be polite, please and thank you.

  1. I suggest using mutt:
    https://www.thegeekdiary.com/linux-unix-send-mail-with-attachment-using-mutt/

    ReplyDelete
Previous Post Next Post