Email attachment

From LinuxSwords Wiki

Jump to: navigation, search

How to send an email-attachment with a shell-script

Open a shell and open a new file (preferably with vim) and enter

#!/bin/bash
from=someone@somewhere.ni
subject="Cron-Bash attachment email script"
msgdate=`date +%Y-%m-%d`  # Leave alone
boundary=GvXjxJ+pjyke8COw            # Leave alone
archdate=`date +%F`                  # Leave alone
attachment=/path/to/where/$attachfile
archattachment="/path/to/attachment-${archdate}.ext"
emailtarget=mjava@bashguru.ubs

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
 
here is the text for the 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" | /usr/sbin/sendmail -t
rm msg.tmp
mv $attachment $archattachment
touch $attachment

now make it executable with

chmod 755 scriptname

and you can run it from a cronjob

Personal tools