0

I am trying to send an e-mail using Unix that has an HTML Body and an attachment.
The code works without the attachment part, but when I added the lines for the attachment, the mail I received just displayed everything as plain text.
This code does not seem to work, please help:

MAIL_TO="receiver@mail.com"
MAIL_FROM="sender@mail.com"
MAIL_SUBJECT="MAIL SUBJECT"
ATTACHMENT=sample.txt
HTML_FILE=sample.html

(
echo "From: ${MAIL_FROM}"
echo "To: ${MAIL_TO}"
echo "Subject: ${MAIL_SUBJECT}"
echo "MIME-Version: 1.0"
echo "Content-Type: multipart/mixed; boundary="MAIL_BOUNDARY";"
echo "Content-Disposition: inline"

echo "--MAIL-BOUNDARY"
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
cat ${HTML_FILE}

echo "--MAIL-BOUNDARY"
echo "Content-Type: text/plain; name="${ATTACHMENT}""
echo "Content-Disposition: attachment; filename="${ATTACHMENT}""
echo "Content-Transfer-Encoding: base64"
base64 ${ATTACHMENT}

echo "--MAIL-BOUNDARY"
) | /usr/sbin/sendmail "${MAIL_TO}"
Kiran Joshi
  • 1,758
  • 2
  • 11
  • 34
Kayle
  • 103
  • 1
  • 11

1 Answers1

0

You forgot to add trailing close-delimiter after the last MIME boundary.

...
echo "--MAIL_BOUNDARY--"
) | /usr/sbin/sendmail "${MAIL_TO}"
Alexander Zotov
  • 156
  • 1
  • 5