Hi,
Since i liked the script idea and i am always extending my library i completed the script with basic error handling, command line parsing and dynamic inclusion of any number of files. There is still alot missing to make this a real tool but i think this will at least do what you wanted and get you going. If the options are not working try using double quotes (-r "your@email.com").
#!/bin/ksh
#
# Error Message function
#
errorMessage ()
{
cat <<EOF
$1
Usage : email-rep -r <recipient> [options] files ...
options: -s Subject line of the email
-f From line of the email
EOF
exit 1
}
#
# Get Command line options
#
while getopts ":r:s:f:" Options
do
case $Options in
r ) recipient=$OPTARG;;
s ) subject=$OPTARG;;
f ) sender=$OPTARG;;
* ) errorMessage "Error : Unknown option";;
esac
done
#
# Check if recipient option has been set (REQUIRED)
#
if [ ! $recipient ]
then
errorMessage "Error : Missing recipient"
fi
sender=${sender:-"ABC <abc@abc.com>"} # default if sender not set
subject=${subject:-"Your Excel Report"} # default if subject not set
shift $(($OPTIND - 1))
# $1 now references the first non option item supplied on the command line
# $@ now holds all remaining options (in our case the files to include)
#
# Check if filenames are available
#
if [ ! $@ ]
then
errorMessage "Error : Please specify at least one file to attach"
fi
#
# Create the Mail Header in temporary file (tmp.mail)
#
cat <<EOF >tmp.mail
MIME-Version: 1.0
From:${sender}
To: ${recipient}
Subject: ${subject}
Content-Type: multipart/mixed; boundary="_boundarystring"
This is a multi-part message in MIME format.
EOF
#
# Create Multipart attachments
#
for i in $@
do
if [ ! -f $i ]
then
errorMessage "File not found: $i"
fi
filepath="$i"
filename=`basename "${filepath}"`
cat <<EOF >>tmp.mail
--_boundarystring
Content-Transfer-Encoding:
Content-Type: application/vnd.ms-excel
Content-Disposition: attachment; filename="${filename}"
`base64.pl -filename="${filepath}"`
--_boundarystring
EOF
done
#
# Send the mail
#
`/usr/lib/sendmail -t -n <tmp.mail`
if [ $? -ne 0 ]
then
errorMessage "Error : Unable to send email"
fi
exit 0
And the perl script. Of cause you are free to use the java base64 if you want. Just replace it with your original line. Replace the first line with the location of your perl executable (try 'whereis perl').
#!/usr/local/bin/perl
# import module
use Getopt::Long;
# read options
$result = GetOptions ( "filename=s" => \$filename );
use MIME::Base64 qw(encode_base64);
open(FILE, $filename) or die "$!";
while (read(FILE, $buf, 60*57)) {
print encode_base64($buf);
}
If you have any questions to parts of the script feel free to ask.
Sascha
Main Topics
Browse All Topics





by: taus01Posted on 2004-07-27 at 12:38:14ID: 11650187
The loop you made should work fine. I guess you put the loop between
Base64
cat <<! | /usr/lib/sendmail -t -n
... (your loop here?)
!
It won't work that way (unfortunately). I would suggest you create a temporary mail file and then pipe that file to sendmail. Also i would suggest not to use the java base64 encoder sine it does not support streaming and will most likely cause problems on big excel files. I have included a small perl base64 encoder that uses a buffer and should work on any filesize.
first the changes to your script:
...
#!/usr/bin/bash
#
# Create the Mail Header in temporary file (tmp.mail)
#
cat <<EOF >tmp.mail
MIME-Version: 1.0
From:${sender}
To: ${recipient}
Subject: ${subject}
Content-Type: multipart/mixed; boundary="_boundarystring"
This is a multi-part message in MIME format.
EOF
# Create Multipart attachments
#
for i in $3 $4 $5
do
filepath="$i"
filename=`basename "${filepath}"`
cat <<EOF >>tmp.mail
--_boundarystring
Content-Transfer-Encoding:
Content-Type: application/vnd.ms-excel
Content-Disposition: attachment; filename="${filename}"
`base64.pl -filename="${filepath}"`
--_boundarystring
EOF
done
# Send the mail
#
`/usr/lib/sendmail -t -n <tmp.mail`
Here is the perl file to encode in base64 (I sure hope you got perl, if not you can use the java and hope the best):
#!/usr/local/bin/perl
# import module
use Getopt::Long;
# read options
$result = GetOptions ( "filename=s" => \$filename );
use MIME::Base64 qw(encode_base64);
open(FILE, $filename) or die "$!";
while (read(FILE, $buf, 60*57)) {
print encode_base64($buf);
}
I hope this helped, naturally you would like to add error handling and a dynamic loop depending on how many additional files the user specifies.
Sascha