2

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: no-reply@domain.com
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?

user2656114
  • 949
  • 4
  • 18
  • 35
  • Is [mailx](http://en.wikipedia.org/wiki/Mailx) an option? If so, you can simple use the `-a` switch to send multiple emails. Do you *have* to use vanilla sendmail? – Noufal Ibrahim Jan 20 '14 at 10:18
  • Check the answer on http://stackoverflow.com/questions/19940292/using-uuencode-to-attach-multiple-attachments-from-a-variable-to-an-e-mail-and-s It will give you an idea how to parse a variable containing attachments through uuencode. – Incognito Jan 20 '14 at 10:25

2 Answers2

6

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='john.doe@example.net'

# () 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: no-reply@domain.com
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
AnFi
  • 10,493
  • 3
  • 23
  • 47
  • worked for me but it's sending some noname file as an attachment. how to avoid this? – SNR Apr 04 '21 at 12:47
1

For what its worth, mailx also works well.

mailx -s "Subject" -a attachment1 -a attachement2 -a attachment3 email.address@domain.com < /dev/null
agentsmith
  • 1,226
  • 1
  • 14
  • 27
Don Reid
  • 11
  • 1