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

user2656114 picture user2656114 · Jan 20, 2014 · Viewed 14.1k times · Source

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?

Answer

AnFi picture AnFi · Jan 20, 2014

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