使用sendmail进行HTML正文和二进制附件

问题描述 投票:10回答:2

目标:使用HTML正文和二进制附件发送邮件(使用sendmail)。

遵循以下链接中指定的准则

http://www.unix.com/shell-programming-scripting/159522-sendmail-html-body-attachment-2.html

http://www.unix.com/shell-programming-scripting/58448-sendmail-attachment.html

它的工作范围是HTML主体或带有uuencode的二进制附件,但不是两者兼而有之。

下面给出了sendmail的shell脚本片段。有了这个,HTML正文变得很好,但附件被错误编码/解码,无法查看相同。

请指教。

#!/usr/bin/ksh

export MAILFROM="[email protected]"
export MAILTO="[email protected]"
export SUBJECT="Test PDF for Email"
export BODY="email_body.htm"
export ATTACH="file.pdf"
export MAILPART=`uuidgen` ## Generates Unique ID
(
 echo "From: $MAILFROM"
 echo "To: $MAILTO"
 echo "Subject: $SUBJECT"
 echo "MIME-Version: 1.0"
 echo "Content-Type: multipart/mixed; boundary=\"-$MAILPART\""
 echo "---$MAILPART"
 echo "Content-Type: text/html"
 echo "Content-Disposition: inline"
 cat $BODY
 echo "---$MAILPART"
 echo 'Content-Type: application/pdf; name="'$(basename $ATTACH)'"'
 echo "Content-Transfer-Encoding: base64"
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
 uuencode -m $ATTACH $(basename $ATTACH)
 echo "---$MAILPART--"
) | /usr/sbin/sendmail $MAILTO

我正在使用HP-UX ia64。通过论坛和网页搜索,发现主要参考PHP,Python等。

shell unix sendmail ksh uuencode
2个回答
11
投票

将电子邮件中的内容传输编码类型从base64更改为uuencode解决了该问题。感谢到目前为止的输入。

下面给出了修订后的脚本。

#!/usr/bin/ksh

export MAILFROM="[email protected]"
export MAILTO="[email protected]"
export SUBJECT="Test PDF for Email"
export BODY="email_body.htm"
export ATTACH="file.pdf"
export MAILPART=`uuidgen` ## Generates Unique ID
export MAILPART_BODY=`uuidgen` ## Generates Unique ID

(
 echo "From: $MAILFROM"
 echo "To: $MAILTO"
 echo "Subject: $SUBJECT"
 echo "MIME-Version: 1.0"
 echo "Content-Type: multipart/mixed; boundary=\"$MAILPART\""
 echo ""
 echo "--$MAILPART"
 echo "Content-Type: multipart/alternative; boundary=\"$MAILPART_BODY\""
 echo ""
 echo "--$MAILPART_BODY"
 echo "Content-Type: text/plain; charset=ISO-8859-1"
 echo "You need to enable HTML option for email"
 echo "--$MAILPART_BODY"
 echo "Content-Type: text/html; charset=ISO-8859-1"
 echo "Content-Disposition: inline"
 cat $BODY
 echo "--$MAILPART_BODY--"

 echo "--$MAILPART"
 echo 'Content-Type: application/pdf; name="'$(basename $ATTACH)'"'
 echo "Content-Transfer-Encoding: uuencode"
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
 echo ""
 #uuencode -m $ATTACH $(basename $ATTACH)
 uuencode $ATTACH $(basename $ATTACH)
 echo "--$MAILPART--"
) > email_`date '+%Y%m%d_%H%M%S'`.out
| /usr/sbin/sendmail $MAILTO

0
投票

尝试在uuencode之后添加一个新行

并尝试没有-m

© www.soinside.com 2019 - 2024. All rights reserved.