Using uuencode to attach multiple attachments from a variable to an e-mail and send using sendmail

user2656114 picture user2656114 · Nov 12, 2013 · Viewed 7k times · Source

I have a script that currently does:

cat $body | uuencode $attachment $attachment | sendmail $sender -t

What should I adjust so that $attachment could be multiple attachments? I have come up with the below but it doesn't look correct?

cat $body |
for i in $attachments 
do
uuencode $i $i
done
| sendmail $sender -t

Answer

iruvar picture iruvar · Nov 12, 2013

Typically, you don't want to store a list of file names in a parameter. With default IFS, spaces embedded within file names will give rise to problems. Instead, declare an array with files

a=(file1 file2 file3 file4)
(for file in "${a[@]}"; do uuencode "$file" "$(basename "$file")"; done) |
 sendmail $sender -t