#!/bin/sh

mmencode () { perl -0777pne '
    $_=unpack("B*",$_); s/$/0000/; s/.{6}/00$&/g;
    s/((.{8})*).*/pack("B*",$1)/e; tr[\0-\77][A-Za-z0-9+/];
    s/$/===/; s/((....)*).*/$1/; s/.{76}/$&\n/g;' $*
}

t=/tmp/mailjpeg$$
trap "rm $t" 0 1 2 3 15

echo "To: $2
Bcc: $USER
Subject: $3
MIME-Version: 1.0
Content-Type: image/jpeg
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename=$1
Content-Description: file:$1
Content-Location: file:$1

`mmencode $1`" > $t

vi $t && /usr/lib/sendmail -t < $t
