Encoding Files with uuencode
You'll use encoding whenever you're sending a binary file (a nontext file) through email or posting one to a newsgroup. Although many email programs and news readers will take care of encoding for you (and, therefore, you won't need to mess with the information here), you may occasionally need to do it yourself.
Files must be encoded so that they can pass through Internet email and news gateways unscathed. If you don't encode a file and your program doesn't do it for you, the file will arrive as a bunch of unusable gibberish (because the gateways assume that all text passing through uses 7-bit words (bytes), while binary files use 8-bit words, thus binary files are garbled). To prevent gibberish, just uuencode your files before you send them along, as shown in Code Listing 13.1.
Code Listing 13.1. Use uuencode to encode files and, optionally, to redirect the output to disk.
[ejr@hobbes compression]$ ls Folder bigfile.uuefolderzip.zip home.gz.uue Zipadeedoodah file1.htm fortunes1.txt newzip.zip bigfile.gz file2.html fortunes1.zip ournewhouse.jpg bigfile.new.gz folder.tar gzip temp [ejr@hobbes compression]$ uuencode ournewhouse.jpg ourhouse.jpg > house.uue [ejr@hobbes compression]$ head house.uue begin 664 ourhouse.jpg M"<@>H@("'@("'@("'@("'@("'@("'@("'@("'@("'@("'@("'@("'@("'@(" M("'@4F%N9&]M(%5.25@@1F]R='5N97,A"@H*"@I)9B!Y;W4G<F4@;F]T('!A M<G0@;V8@=&AE('-O;'5T:6]N+"!Y;W4G<F4@<&%R="!O9B!T:&4@<')E8VEP M:71A=&4N"@H*"@I4:&4@;VYL>2!R96%L;'D@9V]O9"!P;&%C92!T;R!B=7D@ M;'5M8F5R(&ES(&%T(&$@<W1O<F4@=VAE<F4@=&AE(&QU;6)E<@IH87,@86QR M96%D>2!B965N(&-U="!A;F0@871T86-H960@=&]G971H97(@:6X@=&AE(&9O M<FT@;V8@9G5R;FET=7)E+'IF:6YI<VAE9"P@86YD('!U="!I;G-I9&4@8F]X M97,N"B'@("'@("'@("'@("'@("'M+2!$879E($)A<G)Y+"'B5&AE(%1A;6EN M9R!O9B!T:&4@4V-R97<B"@H*"@HB1&ES8V\@:7,@=&\@;75S:6,@=VAA="!% [ejr@hobbes compression]$
To encode a file using uuencode:
uuencode ournewhouse.jpg ourhouse.jpg house.uue
At the shell prompt, type uuencode followed by
The name of the unencoded file (ournewhouse.jpg, in this case).
The name you want the (eventually) unencoded file to have (ourhouse.jpg).
A command to redirect the output to a new filename (> house.uue). You add this bit so the file will be saved on disk and not displayed on the screen instead. We've used the .uue extension so we'll more easily remember that the file is uuencoded.
Code Listing 13.1 lists the files in a directory (to verify the name) and then uuencodes the file. Also, notice that it shows what the top of a uuencoded file looks like.
To encode with uuencode and email at once:
uuencode ournewhouse.jpg house.jpg | mail -s "Heres the new picture" email@example.com
At the shell prompt, use uuencode followed by
The name of the unencoded file(ournewhouse.jpg in this case).
The name you want the (eventually) unencoded file to have (house.jpg).
A command to pipe the output (| 'mail -s "Here's the new picture" firstname.lastname@example.org). This mails the file to a specific email address with specific text in the subject line, which the -s flag sets. See Chapter 11 for more about mailing files and mailing from the shell prompt.
Code Listing 13.2 shows this command and gives a glimpse into a uuencoded file.
Code Listing 13.2. You can uuencode and mail all in one step to work more efficiently.
[ejr@hobbes compression]$ uuencode ournewhouse.jpg house.jpg | mail -s "Heres the new picture" email@example.com
If you're sending a file to someone with a MIME-compliant email program, you might try base64 encoding, using uuencode -m. See Chapter 11 for more about email.
A relatively new development on Usenet is to use yEnc (yencoding/ydecoding). Look for yencode and ydecode on your system, or consider installing them if needed. See Chapter 14 for more information about installing software.
Also check out Chapter 11 for information about email programs that will automatically handle attachments, including encoding files for you.
You must (either manually or automatically) encode all binary files (graphics, programs, compressed files, etc.) before emailing them. Plain text (text files, scripts, or HTML documents) don't need to be encoded.