Sunday, 25 August 2013

How do I email with an attachment in Unix?

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.

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
[email protected]  # 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 uses.                                                                                                                                    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.
    Older versions of elm without ME+ won't even deal with attachments at all, detaching or attaching.
  • 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.
  • 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 run.
    For a single file:
cat $TXTFILE | zmail.small -subject "$SUBJECT" -attach /application/octet-stream:${ATTFILE} $MAILTO

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.

0 blogger-disqus:

Post a Comment