将sendmail用于HTML正文和二进制附件 [英] Using sendmail for HTML body and binary attachment
问题描述
目标:使用HTML正文和二进制附件发送邮件(使用sendmail).
Objective: To send mail (using sendmail) with HTML body and binary attachment.
遵循以下链接中指定的准则
Followed the guidelines specified in the following links
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的二进制附件,都不能正常工作.
It is working to the extent that, either HTML body or the binary attachment with uuencode, but not both.
下面给出的是sendmail的Shell脚本的片段.这样,HTML主体就可以了,但是附件的编码/解码错误,无法查看.
Given below is a snippet of the shell script to sendmail. With this, the HTML body is coming fine, but the attachment is getting encoded/decoded wrongly and unable to view the same.
请告知.
#!/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等的引用.
I am using HP-UX ia64. Have searched through the forum and web and found references mostly to PHP, Python, etc.
推荐答案
将电子邮件中的Content Transfer编码类型从base64更改为uuencode可以解决此问题. 感谢到目前为止的投入.
Changing the Content transfer encoding type within the email from base64 to uuencode resolved the issue. Thanks for the inputs so far.
下面是有效的修改后的脚本.
Given below is the revised script that works.
#!/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
这篇关于将sendmail用于HTML正文和二进制附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!