Shell脚本(Csh shell)发送附件

时间:2015-09-09 10:02:18

标签: linux shell email-attachments csh

我已经创建了一个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)时!这个顶部的区别(我使用记事本++来查看它)

原始文件(工作!):<​​/ p>

%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)

1 个答案:

答案 0 :(得分:1)

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

这是缺少的一行:

echo ""             >>$TMPFI

在上下文中:

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