troubleshooting Question

Sending multiple email attachments using a shell script

Avatar of Obi79
Obi79 asked on
System Programming
8 Comments1 Solution42526 ViewsLast Modified:
Hi, I have a shell script that send an excel file as an email attachment.

I would like to be able to send more than one excel attachment at any given time.

This is the script that currently send one attachment.



#!/bin/ksh

if test ! -z "${5}"
then
echo ""
echo "Error:"
echo "                Too many arguments, try putting subject line in quotes."
echo ""
echo "                Usage:"
echo "                email-rep <filename> <recipient> <subject> <sender>"
echo ""
echo "                Subject and Sender are not mandatory."
exit 1
else

filepath="${1}"
filename=`basename "${filepath}"`
filextn=${filename##*.}
recipient="${2}"
subject="${3}"

if [ "${4}" = "" ]
 then
   sender="ABC  <abc@abc.com>"
 else  
sender="${4}"
fi

cat <<! | /usr/lib/sendmail -t -n
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.
--_boundarystring
Content-Transfer-Encoding: Base64
Content-Type: application/vnd.ms-excel
Content-Disposition: attachment; filename="${filename}"

`java base64 "${filepath}"`

--_boundarystring--
!
fi

My idea was to ask for the recipients email address then the arguments after that would be the files to be sent. Then use a for loop on the bit right after the "This is a multi-part ....", as this is the bit that needs to be typed out again for multiple attachments.


when I put the do loop in the code, it looked like this:

This is a multi-part message in MIME format.

for i in $3 $4 $5

do

filepath="$i"
filename=`basename "${filepath}"`

--_boundarystring
Content-Transfer-Encoding: Base64
Content-Type: application/vnd.ms-excel
Content-Disposition: attachment; filename="${filename}"


`java base64 "${filepath}"`

--_boundarystring

done

The program didn't seem to recognise the loop.
I am not very good at writing shell scripts and I literally just read up on loops today.
I would appreciate any suggestions or possible alternatives.

Thank you in advance.
ASKER CERTIFIED SOLUTION
taus01

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros