带有附件编码的sendmail

时间:2014-12-30 15:34:12

标签: unix ksh

我正在尝试在unix中使用sendmail函数,但似乎无法获得附件......我的代码是以下代码:

export MAILTO="me@myco.com"
export SUBJECT="test mail"
export ATTACH="/home/tstattach"
(
 echo "To: $MAILTO"
 echo "Subject: $SUBJECT"
 echo "MIME-Version: 1.0"
 echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"'
 echo "this is a test message"
 base64 $ATTACH 
) | sendmail $MAILTO

我的电子邮件信息是这样的,没有附件:

this is a test message
dGVzdCBhdHRhY2htZW50
---q1w2e3r4t5--

如何让附件通过?好像它以奇怪的方式对它进行编码...我也尝试过uuencode而不是base64,但后来我发现一个错误,说找不到uuencode ......

1 个答案:

答案 0 :(得分:3)

好的,今天感觉很好。这是一个有效的(测试过):

export MAILTO="me@myco.com"
export SUBJECT="test mail"
export ATTACH="/home/tstattach"
(
    echo "Date: $(date -R)"
    echo "To: $MAILTO"
    echo "Subject: $SUBJECT"
    echo "MIME-Version: 1.0"
    echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"'
    echo
    echo '---q1w2e3r4t5'
    echo 'Content-Type: text/plain; charset=utf-8'
    echo 'Content-Transfer-Encoding: 8bit'
    echo
    echo "this is a test message"
    echo '---q1w2e3r4t5'
    echo 'Content-Type: text/plain; charset=utf-8; name=attach.txt'
    echo 'Content-Transfer-Encoding: base64'
    echo 'Content-Disposition: attachment; filename=attach.txt'
    echo
    base64 <"$ATTACH"
    echo
    echo '---q1w2e3r4t5--'
) | sendmail $MAILTO

但请帮自己一个忙,并阅读电子邮件(RFC822及其替代品)和MIME(RFC约为2047年)。