Shell脚本(Csh Shell)发送附件



我已经创建了一个shell脚本,可以从linux发送电子邮件。

我正在尝试修改它,以便它可以发送附件(永远不会结束的故事,我知道)。->

脚本接受如下6个参数

1. Subject
2. Recipient
3. CC
4. From
5. html body (from file)
6. attachement

脚本可以按如下方式执行:

mailertest.csh "test subject" "me@something.com" "" "info@something.com" "mailbody.txt" "attachement.pdf"

到目前为止,它确实发送附件,但除了普通的。txt文件之外的任何文件都无法读取。

当我比较文件(原始。pdf,收到。pdf)的唯一!上面这部分的差异(我用notepad++来查看)

原始文档(工作!):

%PDF-1.5
%âãÏÓ
10 0 obj
<</Linearized 1/L 69639/O 12/E 65128/N 1/T 69334/H [ 460 154]>>
endobj

收到的文件(已损坏!):

5128/N 1/T 69334/H [ 460 154]>>
endobj

我确信我就快到了,但是还是少了点东西!

这是整个脚本

#!/bin/csh
set LOGF1=$MXIB_PLOG/mxmail09.log
set SENDSJ="$1"
set SENDTO="$2"
set SENDCC="$3"
set SENDFM="$4"
set MAIFI=$5
set TMPFI=$5_tmp
set ATTACH=$6
set ERRIND=0
if ($?MXIB_MAILPROG == 0) set MXIB_MAILPROG=''
if ($?MXIB_MAILSYFM == 0) set MXIB_MAILSYFM=''
echo "`date` $1 $2 $3 $4 $5"            >>$LOGF1
if ($ERRIND == 0) then
    if ($SENDTO == '') then
        echo "missing mail to address"      >>$LOGF1
        set ERRIND=1
    endif
endif
if ($ERRIND == 0) then
    if ($SENDFM == 'system') then
        if ($MXIB_MAILSYFM == '') then
            set SENDFM=system@`hostname`
        else
            set SENDFM="$MXIB_MAILSYFM"
        endif
    endif

    echo "To: $SENDTO"                  >$TMPFI
    echo "MIME-Version: 1.0"            >>$TMPFI
    echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"' >>$TMPFI
    echo >>$TMPFI
    echo '---q1w2e3r4t5' >>$TMPFI
    echo "Content-Type: text/html"      >>$TMPFI
    echo "Content-Disposition: inline"      >>$TMPFI
    echo "<html>"                   >>$TMPFI
    echo "<body>"                   >>$TMPFI
    echo "<pre style="font: monospace">"        >>$TMPFI
    cat $MAIFI | sed 's/</ /g' | sed 's/>/ /g'  >>$TMPFI
    echo "</pre>"                   >>$TMPFI
    echo "</body>"              >>$TMPFI
    echo "</html>"              >>$TMPFI
    echo "---q1w2e3r4t5"            >>$TMPFI

    echo "Content-Type: application; name=" basename $ATTACH  >>$TMPFI
    echo "Content-Transfer-Encoding: base64"            >>$TMPFI
    echo "Content-Disposition: attachment; filename=$ATTACH"                >>$TMPFI

    base64 $ATTACH  >>$TMPFI

     echo "---q1w2e3r4t5--"                             >>$TMPFI
    if ($MXIB_MAILPROG == 'mutt') then
        if ($SENDCC == '' || $SENDCC == 'none') then
            cat $TMPFI | mutt -s "$1" $SENDTO
        else
            cat $TMPFI | mutt -s "$1" -c $SENDCC $SENDTO
        endif
    else
        ##/usr/lib/sendmail -F $SENDFM -f $SENDFM $SENDTO $SENDCC <$TMPFI
        echo "Subject: $SENDSJ" | cat - $TMPFI | /usr/lib/sendmail -F $SENDFM -f $SENDFM -t $SENDTO
    endif
endif
unset ERRIND
unset SENDFM
unset SENDCC
unset SENDTO
unset SENDSJ
unset MAIFI
unset LOGF1
exit(0)

在base64编码的文件前需要有一个空行

这是缺失的行:

echo ""             >>$TMPFI
在上下文:

echo "Content-Disposition: attachment; filename=$basename"              >>$TMPFI
    echo ""             >>$TMPFI
    base64 $ATTACH  >>$TMPFI

相关内容

  • 没有找到相关文章

最新更新