Sunday, July 19, 2009

Sending email with attachments on UNIX systems

Sending email with attachments on UNIX systems
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
MAILTO=user@where.ever  # 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
     
  •  

    No comments:

    Post a Comment