home   articles   tags   browse code   

BASH - Send mail with an Attachment


 

This requires uuencode, which is available in the package sharutils.

It uses the command line utility sendmail, and formats the smtp message as necessary to send an email and an attachment. We also have an article with just a basic bash sendmail script.

#!/bin/bash
#requires: basename,date,md5sum,sed,sendmail,uuencode
function fappend {
    echo "$2">>$1;
}
YYYYMMDD=`date +%Y%m%d`

# CHANGE THESE
TOEMAIL="recipient@email.com";
FREMAIL="crondaemon@65.244.254.144";
SUBJECT="Daily Backup - $YYYYMMDD";
MSGBODY="Hello this is the message body";
ATTACHMENT="/home/joeuser/Untitled.png"
MIMETYPE="image/png" #if not sure, use http://www.webmaster-toolkit.com/mime-types.shtml

# DON'T CHANGE ANYTHING BELOW
TMP="/tmp/tmpfil_123"$RANDOM;
BOUNDARY=`date +%s|md5sum`
BOUNDARY=${BOUNDARY:0:32}
FILENAME=`basename $ATTACHMENT`

rm -rf $TMP;
cat $ATTACHMENT|uuencode --base64 $FILENAME>$TMP;
sed -i -e '1,1d' -e '$d' $TMP;#removes first & last lines from $TMP
DATA=`cat $TMP`

rm -rf $TMP;
fappend $TMP "From: $FREMAIL";
fappend $TMP "To: $TOEMAIL";
fappend $TMP "Reply-To: $FREMAIL";
fappend $TMP "Subject: $SUBJECT";
fappend $TMP "Content-Type: multipart/mixed; boundary=\""$BOUNDARY"\"";
fappend $TMP "";
fappend $TMP "This is a MIME formatted message.  If you see this text it means that your";
fappend $TMP "email software does not support MIME formatted messages.";
fappend $TMP "";
fappend $TMP "--$BOUNDARY";
fappend $TMP "Content-Type: text/plain; charset=ISO-8859-1; format=flowed";
fappend $TMP "Content-Transfer-Encoding: 7bit";
fappend $TMP "Content-Disposition: inline";
fappend $TMP "";
fappend $TMP "$MSGBODY";
fappend $TMP "";
fappend $TMP "";
fappend $TMP "--$BOUNDARY";
fappend $TMP "Content-Type: $MIMETYPE; name=\"$FILENAME\"";
fappend $TMP "Content-Transfer-Encoding: base64";
fappend $TMP "Content-Disposition: attachment; filename=\"$FILENAME\";";
fappend $TMP "";
fappend $TMP "$DATA";
fappend $TMP "";
fappend $TMP "";
fappend $TMP "--$BOUNDARY--";
fappend $TMP "";
fappend $TMP "";
#cat $TMP>out.txt
cat $TMP|sendmail -t;
rm $TMP;


Note: when i put this in a cronjob I usually have to replace 'sendmail' above with '/usr/sbin/sendmail'. To find out where sendmail is on your system type:
[root@server /home] which sendmail

 

 
już nigdy nie usłyszę kochanych twych słów on Jan 18th, 2010 10:46 pm said:
BOUNDARY=`date +%s|md5sum|cut -b1-32` #probably the preferable way of parsing md5sum output, at least a shorthand or an alternative
 

już nigdy do mych ust nie przytulę cię znów on Jan 18th, 2010 10:49 pm said:
TMP=`mktemp`; or TMP=`tempfile`;
 

PM on Feb 15th, 2010 6:55 pm said:
Thanks for the script! An alternativ for base64 coding: ... | openssl base64 This makes the cut-offs with sed obsolet . Cheers PM
 

optimus on Mar 24th, 2010 9:54 pm said:
How to add multiple graph/image attachment with this script. Thanks
 

Rob on Mar 25th, 2010 5:14 pm said:
Great script, the uuencode wouldn't work for me, but the openssl base64 worked great. Optimus, you should be able to copy the sections on adding the file (make sure with the right type) and the part between the 2nd and 3rd boundary as often as you want.. adding multiple files. #!/bin/bash #requires: basename,date,md5sum,sed,sendmail,uuencode function fappend { echo "$2">>$1; } YYYYMMDD=`date +%Y%m%d` # CHANGE THESE TOEMAIL="recipient@email.com"; FREMAIL="crondaemon@65.244.254.144"; SUBJECT="Daily Backup - $YYYYMMDD"; MSGBODY="Hello this is the message body"; ATTACHMENT="/home/joeuser/Untitled.png" MIMETYPE="image/png" #if not sure, use http://www.webmaster-toolkit.com/mime-types.shtml rm -rf $TMP; fappend $TMP "From: $FREMAIL"; fappend $TMP "To: $TOEMAIL"; fappend $TMP "Reply-To: $FREMAIL"; fappend $TMP "Subject: $SUBJECT"; fappend $TMP "Content-Type: multipart/mixed; boundary=""$BOUNDARY"""; fappend $TMP ""; fappend $TMP "This is a MIME formatted message. If you see this text it means that your"; fappend $TMP "email software does not support MIME formatted messages."; fappend $TMP ""; fappend $TMP "--$BOUNDARY"; fappend $TMP "Content-Type: text/plain; charset=ISO-8859-1; format=flowed"; fappend $TMP "Content-Transfer-Encoding: 7bit"; fappend $TMP "Content-Disposition: inline"; fappend $TMP ""; fappend $TMP "$MSGBODY"; fappend $TMP ""; fappend $TMP ""; TMP="/tmp/tmpfil_123"$RANDOM; BOUNDARY=`date +%s|md5sum` BOUNDARY=${BOUNDARY:0:32} FILENAME=`basename $ATTACHMENT` rm -rf $TMP; cat $ATTACHMENT|uuencode --base64 $FILENAME>$TMP; sed -i -e '1,1d' -e '$d' $TMP;#removes first & last lines from $TMP DATA=`cat $TMP` #copy the below part for every attachement fappend $TMP "--$BOUNDARY"; fappend $TMP "Content-Type: $MIMETYPE; name="$FILENAME""; fappend $TMP "Content-Transfer-Encoding: base64"; fappend $TMP "Content-Disposition: attachment; filename="$FILENAME";"; fappend $TMP ""; fappend $TMP "$DATA"; fappend $TMP ""; fappend $TMP ""; fappend $TMP "--$BOUNDARY--"; #and then the send email and cleanup fappend $TMP ""; fappend $TMP ""; #cat $TMP>out.txt cat $TMP|sendmail -t; rm $TMP;
 



 

 



Related Articles
 




home  |  privacy policy  |  terms of use  |  contact  


©2010, Zedwood Digital