Bash Email Script

Email Script with Attachments

script found at http://grimthing.com/archives/2004/09/18/bash-email-script-with-attachment/

!/bin/bash

###################################################
#  The following is all you should have to modify #
#  in order to get the script working for you.    #
#  This script presupposes that you have a        #
#  working sendmail on your system.  An alias to  #
#  postfix or msmtp should work though.           #
###################################################

from=your_email@ddress.org
subject="Cron-Bash attachment email script"
msgdate=`date +\"%a, %e %Y %T %z\"`  # Leave alone
boundary=GvXjxJ+pjyke8COw            # Leave alone
archdate=`date +%F`                  # Leave alone
attachment=/path/to/attachment.ext
archattachment="/path/to/attachment-${archdate}.ext
emailtarget=recipient@ddress.org

###################################################
#  The text below Content-Disposition: inline can #
#  be modified to suit your needs.  It's          #
#  important that you escape all quotes in the    #
#  body of your message or the script will fail.  #
###################################################

daemail=$(cat <<!
Date: $msgdate
From: $from
To: $emailtarget
Subject: $subject
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary=\"$boundary\"
Content-Disposition: inline

--$boundary
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

This is the text that makes up the body of the email.
Anything you want to appear in the body of the message
should be put here.  Try to keep it generic since it's a
form email.  Something along the lines of,

\"The results of xyz script has been attached to this
email.\"

--$boundary
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=\"attachment.ext\"
!)

echo "$daemail" > msg.tmp
echo  >> msg.tmp
cat "$attachment" >> msg.tmp
echo  >> msg.tmp
echo "--$boundary--" >> msg.tmp
email=`cat msg.tmp`
echo "$email" | /path/to/sendmail -t
rm msg.tmp
mv $attachment $archattachment
touch $attachment

You can also use

cat text_file |/bin/mail -s "this is a test" username@email.com