home > notes > mmencode [ printer-friendly version ]
Animal Crossing
AOL Images
baked beans
Bass Guitar
AOL main.idx
Crash IE
Disk Hog
Dual Heads
FireFox prefetch
Highway Hacking
ISO images
Chip's Challenge
sims cheats
Sim Tower hack
Solaris USB
spray cans
SSI Banners
HTML Symbols
ToolTalk error
CDE Wallpaper
Windows 7 Password


The mmencode utility (sometimes called mimencode) can be used to encode binary files for transmission through email systems. It is useful in the context of writing shell scripts, but not so much for direct use by people typing on the command line. If you have a bunch of images and would like to create MIME-encoded email messages from them and send those messages, then the combination of mmencode and some sendmail chicanery in a shell script will work nicely.

I had a bit of trouble finding mmencode on my systems (at least, a version I trusted that wasn't wrapped by a gigantic Big Brother script - long story), so I ran around the web for a few minutes and found a copy of 'metamail'. Buried in the guts of the metamail code is a nice little implementation of mmencode.

So, the first thing you need to do is download the mmencode.zip file (7950 bytes) and unzip it in a work directory on your system. This little zip file contains five files: Makefile, compile-it, config.h, codes.c, and mmencode.c

Edit the compile-it script to suit your environment; it's a tiny wrapper script that sets your $PATH so that you pick up the correct C compiler. If your environment is set up properly, you may not even need the compile-it script. Then, just execute the compile-it script. It will build codes.o and mmencode.o and then build the mmencode binary.

Now, you're ready to send an image file through the mail via sendmail. Here's a nasty, ugly script for that:

#!/bin/ksh # # Try to dummy up a mime-encoded mail message # containing an image IMAGE=${HOME}/images/biketroop.jpg MMENCODE=/ora01/home/devel/sandbox/mmencode MAILBODY=mailbody.txt # command-line options for mmencode: # # b - base64 # q - quoted-printable # u - decode stdin # p - newline convention (used only with -b) ${MMENCODE} -b ${IMAGE} -o mmencode.out # Construct the mail message cat <<EOF > ${MAILBODY} From: Colonel Flagg <colonel.flagg@cid.com> Subject: No One Sees The Wind Sender: colonel.flagg@cid.com MIME-version: 1.0 Content-type: multipart/mixed; boundary="Boundary_(ID_wxHPRiM1ebsu1CJ5f3/XqA)" This is a multi-part message in MIME format. --Boundary_(ID_wxHPRiM1ebsu1CJ5f3/XqA) Content-type: text/plain; charset=us-ascii Content-transfer-encoding: 7BIT Let's see how this looks. --Boundary_(ID_wxHPRiM1ebsu1CJ5f3/XqA) Content-type: image/jpeg; name=biketroop.jpg Content-transfer-encoding: BASE64 Content-disposition: inline; filename=biketroop.jpg EOF cat mmencode.out >> ${MAILBODY} cat <<EOF >> ${MAILBODY} --Boundary_(ID_wxHPRiM1ebsu1CJ5f3/XqA)-- EOF cat ${MAILBODY} | /usr/lib/sendmail target.address@anywhere.com

I warned you, didn't I? That script is the most mud-ugly, brute-force way imaginable to create a mail message that has both a normal text portion and a MIME-encoded image attachment. The script first runs mmencode to encode the image file into a text file ('mmencode.out') and then uses 'cat' to cram a bunch of text and email headers around it.

By the way, you may have noticed that this approach lets you fake some key bits of data - like the sender's name, that sort of thing. Don't get too carried away, though, because sendmail and the networking software is probably smarter than you think. Use your powers for good, Grasshopper.


home May you live in interesting times! privacy