Sending email with attachments on UNIX systems
This page outlines several methods of generating email messages from
command line and shell scripts using both popular email processing tools such as
metamail and mpack, and home grown scripts. Most of these methods use the MIME specification,
as outlined in RFC 2045, RFC 2046, RFC 2047, RFC 2048 and RFC 2049.
Please send mail to me with any comments, suggestions, criticism or additions.
All of the below examples use the following shell variables. I use
MIME type application/octet-stream just as an example. Actual type
used will vary depending upon attachment file type. Remember, these are
simple examples of the different tools available.
TXTFILE=/tmp/textfile # A text message with a simple preface message
ATTFILE=/tmp/binary_file # File to be attached and generally requiring encoding
SUBJECT="Your attachment" # Change as needed
MAILTOfirstname.lastname@example.org # Ditto
uuencode - This is the original method to send encoded text within a message. It
is not an attachment as we think of them today but is still used enough to warrant putting it here.
uuencode $ATTFILE $ATTFILE | mail -s "$SUBJECT" $MAILTO
(uuencode $FILE1 $FILE1; uuencode $FILE2 $FILE2) | mail -s "$SUBJECT" $MAILTO
simple shell commands - For a very simple text (plain or html) attachment with
just one file:
echo "From: $LOGNAME\nTo: $MAILTO\nSubject: $SUBJECT\n\
Mime-Version: 1.0\nContent-Type: text/plain\n" > /tmp/file
cat $TXTFILE >> /tmp/file
/usr/lib/sendmail -t -oi < /tmp/file
metamail - This is the original set of MIME tools written by Nathaniel Borenstein.
It works as a stand-alone set of tools for sending messages and encoding/decoding
of MIME messages parts or can be set up as a plug-in for other tools; like Elm and
Z-Mail use. metasend is one of the binaries in the package. You can use the program
mailto for interactive use.
metasend -b -s "$SUBJECT" -f $TXTFILE -m text/plain -e none -n \
-f $ATTFILE -m application/octet-stream -e base64 -t $MAILTO
mpack - A nice simple utility for encoding and decoding (munpack) MIME message parts.
mpack -s "$SUBJECT" -c application/octet-stream $ATTFILE $MAILTO
mutt - A popular MUA that can do nice simple command line sending.
mutt -a $ATTFILE -s "$SUBJECT" $MAILTO < $TXTFILE
Elm - The following requires release Elm2.4ME+ PL54, when it appears
the "-A" option was changed to work in batch mode. Some previous ME+ releases could only deal with attachments in interactive mode.
elm -s"$SUBJECT" -A $ATTFILE $MAILTO < $TXTFILE
Older versions of elm without ME+ won't even deal with attachments at all, detaching
Pine - (to be investigated but it doesn't look good; maybe the c-client???)
uuenview - This is from the UUDeview package, another decoding/encoding package along the lines of mpack.
uuenview -m $MAILTO -b -a $ATTFILE < $TXTFILE
nail - This is a newer MUA recently brought to my attention. You can define outgoing MIME types beyond text/plain and application/octet-stream by using extensions and defining those in ~/.mime.types or a system-wide file.
nail -s "$SUBJECT" -a $ATTFILE $MAILTO < $TXTFILE
Z-Mail - The grand daddy of all email user agents. :-) This is using the
zmail.small binary that has none of the GUI code so it uses less resources to
For a single file:
cat $TXTFILE | zmail.small -subject "$SUBJECT" -attach \
Bart Schaefer offers the following for multiple attachments. (untested by myself)
zmail.small -rf /dev/null -e 'mail -z -s "$SUBJECT" $MAILTO' \
-e 'compcmd attach-file $ATTACHFILE application/msword base64 \
"$DESCRIPTION"' \ -e! 'compcmd send'
You can repeat the -e 'compcmd attach-file ...' as often as necessary to
attach more than one file. The -z in the mail command tells it not to
go interactive, but rather to wait for compcmds to tell it what to do.
Here is some Bourne shell code (taken out of a deliver enabled autoresponder) for creating email messages with multiple attachments of varying types. This script uses metamail's mmencode program for encoding and sendmail for sending.
Check out this far more advanced shell script written by Dave Wotton that does much more than my simple script. It uses a encoder called encdec that I am not
familiar with but will soon address on this page. In the meantime, you could use mmencode mentioned previously for the encoding.
And here is some basic, fairly static Perl code using the
MIME::Lite module to send an attachment along with a text note to a sender. This is
almost verbatim from the modules man page and could use some tweaking to get it more
useful but it certainly works as a "plugin" piece of code.
Here is a useful page by Heiner Steven with several examples of shell coding using metamail and mpack in particular.
For an excellent guide to creating proper text files and for the formatting of your message
if you write your own script, please see Keith Moore's style guide.