10

目标:发送带有 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 的二进制附件,但不能同时使用两者。

下面给出了发送邮件的 shell 脚本片段。有了这个,HTML 正文就很好了,但是附件被错误地编码/解码并且无法查看。

请指教。

#!/usr/bin/ksh

export MAILFROM="noreply@site.dom"
export MAILTO="somebody@somesite.com"
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 等的引用。

4

2 回答 2

12

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

下面给出的是有效的修改后的脚本。

#!/usr/bin/ksh

export MAILFROM="noreply@domain.com"
export MAILTO="mail.to@gmail.com"
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
于 2012-07-30T16:09:45.010 回答
0

尝试在之后添加新行uuencode

并尝试不-m

于 2012-06-21T10:58:45.370 回答