Send e-mail with multiple attachments using command line and sendmail

17.1k Views Asked by At

Is it possible to send multiple attachments with uuencode and sendmail?

In a script I have a variable containing the files that need to be attached to a single e-mail like:

$attachments=attachment_1.pdf attachment_2.pdf attachment_3.pdf attachment_4.pdf

Also a $template variable like:

$template="Subject: This is the subject
From: [email protected]
To: %s
Content-Type: text/plain

This is the body.
"

I have come up with:

printf "$template" "$recipient" |
sendmail -oi -t

Somewhere within this I must attach everything in the $attachments variable?

2

There are 2 best solutions below

0
On

For what its worth, mailx also works well.

mailx -s "Subject" -a attachment1 -a attachement2 -a attachment3 [email protected] < /dev/null
1
On

uuencode attachemnts and send via sendmail

Sending MIME attachemnts is better.
uuencode is simpler to implement in scripts but email some clients DO NOT support it.

attachments="attachment_1.pdf attachment_2.pdf attachment_3.pdf attachment_4.pdf"
recipient='[email protected]'

# () sub sub-shell should generate email headers and body for sendmail to send
(
# generate email headers and begin of the body asspecified by HERE document 
cat - <<END
Subject: This is the subject
From: [email protected]
To: $recipient
Content-Type: text/plain

This is the body.

END
# generate/append uuencoded attachments
for attachment in $attachments ; do
  uuencode $attachment $attachment
done
) | /usr/sbin/sendmail -i -- $recipient