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

7.4k Views Asked by At

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
3

There are 3 best solutions below

3
On

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
0
On

FILES="/rollovers/DailyCadRpt.* /rollovers/DailyFireRpt.*"

(for f in $FILES ; do uuencode "$f" "$f" ; done ) | mail -s "Subject" [email protected]

The above works in AIX 6.1 for wildcards. But, you must use the 10-pad asterisk. The asterisk above the number eight does not work in AIX. Also, this does not have any body text. But that is done as in the other examples. You may add more files by using a space as the separator, as in my example. Also, you cannot use Daily* with either asterisk. AIX just won't do it. The asterisk must come after a period in the file name. Our reports have the date added to the report name separated by a period. It preserves our archival naming pattern and grabs it every day without needing a specific file name.

0
On

Try the following script:

# specify list of email recipients
recipients=...
# specify envelope sender address
sender=...
( 
  cat $body
  for i in $attachments 
  do
    uuencode $i $i
  done
) | sendmail -f$sender -i -- $recipients
  • $body file must contain message headers (e.g. Subject:) separated by an empty line from message body
  • IMHO it is a better/safer style to specify recipients via command line instead of making sendmail extract them from headers.