I searched for ages to find a half decient example to send mime attachments from the command line that allows you to specify the from and to address. I found some applications however some either didnt let you specify the from address and others didnt let you do multi-part mime messages and the rest suggested to use uuencode. Uuencode is horrible, no just no!
So I wrote my own using sendmail and openssl and it works very well and is incredibly flexible and small.
Here it is:
#!/bin/bash
SENDMAIL="/usr/sbin/sendmail"
FILESPATH="/home/user/"
FILENAME="1.pdf"
FILETOSEND="${FILESPATH}${FILENAME}"
SUBJ="Subject"
SNDR="test@websurfshack.net"
RECP="user@gmail.com"
MIMEBOUNDRY="0016364d261349d0be047105fb55"
(
echo "Mime-version: 1.0"
echo "Subject: $SUBJ"
echo "From: $SNDR"
echo "To: $RECP"
#cc:
#bcc:
echo "Content-Type: multipart/mixed; boundary=${MIMEBOUNDRY}"
echo "--${MIMEBOUNDRY}"
echo "Content-Type: text/plain; charset=ISO-8859-1"
echo "Content-Transfer-Encoding: 7bit"
echo # This echo is important or else you get a mime error
echo INSERT MESSAGE HERE
echo "--${MIMEBOUNDRY}"
echo "Content-Type: application/pdf; name=\"${FILENAME}\""
echo "Content-Disposition: attachment; filename=\"${FILENAME}\""
echo "Content-Transfer-Encoding: base64"
echo # This echo is important or else you get a mime error
openssl enc -base64 -in ${FILETOSEND}
echo "--${MIMEBOUNDRY}--"
) | ${SENDMAIL} -t
There we have it, Simple isnt it. It is very basic but im sure you’ll be able to modify it to suit your needs.
UPDATE: ive since found out that you can specify -t as a sendmail switch to force sendmail to use header data as reciepient information. This then enabled you to specify a BCC field. This works with postfix sendmail… not sure about others.

