如何在Unix中使用sendmail发送带有HTML正文和.txt附件的电子邮件?

时间:2018-07-04 05:56:32

标签: html unix sendmail attachment

我正在尝试使用具有e-mail正文和附件的Unix发送HTML
 该代码无需附件部分即可工作,但是当我添加附件行时,我收到的邮件只是将所有内容显示为纯文本。
 该代码似乎无效,请提供帮助:

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}"

1 个答案:

答案 0 :(得分:0)

您忘了在最后一个MIME边界之后添加结尾close-delimiter

...
echo "--MAIL_BOUNDARY--"
) | /usr/sbin/sendmail "${MAIL_TO}"